标签:java
实现整数加减法的简单计算器
package jisuan; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Font; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.GridLayout; import java.awt.Label; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class jisuan{ private JPanel panel0; private JPanel panel2; private JLabel text1, text2; private int sum, tmp; private int flag; String s1, s2; JFrame frame; public jisuan(){ s1 = s2 = ""; sum = tmp = flag = 0; frame = new JFrame("科学计算器"); frame.setLayout(new GridLayout(2,1,3,3)); Container tainer = frame.getContentPane(); panel0 = new JPanel(new GridLayout(2,1,3,3)); text1 = new JLabel(""); text1.setFont(new Font("隶书",1,20)); text1.setHorizontalAlignment(SwingConstants.RIGHT); text1.setBorder(BorderFactory.createLineBorder(Color.black, 2)); text2 = new JLabel(""); text2.setFont(new Font("隶书",1,20)); text2.setHorizontalAlignment(SwingConstants.RIGHT); text2.setBorder(BorderFactory.createLineBorder(Color.black, 2)); panel0.add(text1); panel0.add(text2); frame.add(panel0); JButton b[]=new JButton[16]; panel2 = new JPanel(new GridLayout(4,4)); String str[]={"1", "2", "3", "+", "4", "5", "6", "-", "7", "8", "9", "*", "0", "/", "=", "C"}; for (int i=0; i<16; i++) { b[i] = new JButton(str[i]); if (i==3 || i==7 || i==11 || i==13 ||i==14||i==15) b[i].setForeground(Color.red); panel2.add(b[i]); b[i].addActionListener(new ButtonListen());//为每一个按钮添加监听 } panel2.setSize(300, 300); tainer.add(panel2); frame.setResizable(false);//设置窗口不可以调节大小 frame.setSize(300,300); frame.setVisible(true); } public static void main(String[] args){ new jisuan(); } public class ButtonListen implements ActionListener{ public void actionPerformed(ActionEvent e) { switch(e.getActionCommand()){ case "1": s2 += "1"; tmp = tmp*10 + 1; text2.setText(s2); break; case "2": s2 += "2"; tmp = tmp*10 + 2; text2.setText(s2); break; case "3": s2 += "3"; tmp = tmp*10 + 3; text2.setText(s2); break; case "4": s2 += "4"; tmp = tmp*10 + 4; text2.setText(s2); break; case "5": s2 += "5"; tmp = tmp*10 + 5; text2.setText(s2); break; case "6": s2 += "6"; tmp = tmp*10 + 6; text2.setText(s2); break; case "7": s2 += "7"; tmp = tmp*10 + 7; text2.setText(s2); break; case "8": s2 += "8"; tmp = tmp*10 + 8; text2.setText(s2); break; case "9": s2 += "9"; tmp = tmp*10 + 9; text2.setText(s2); break; case "0": s2 += "0"; tmp = tmp*10; text2.setText(s2); break; case "+": s1 =s1 + s2 + "+"; slove(); geshi(); flag = 1; break; case "-": s1 += s2 + "-"; slove(); geshi(); flag = 2; break; case "*": s1 += s2 + "*"; slove(); geshi(); flag = 3; break; case "/": s1 += s2 + "/"; slove(); geshi(); flag = 4; break; case "C": s1 = s2 = ""; sum = tmp = 0; text1.setText(s1); text2.setText(s2); break; case "=": slove(); s1 += s2; s2 = sum+""; text1.setText(s1); text2.setText(s2); s1 = s2 = ""; sum = tmp = 0; flag = 0; break; } } } public void slove(){ switch (flag){ case 0 : sum = tmp;break; case 1 : sum += tmp;break; case 2 : sum -= tmp;break; case 3 : sum *= tmp;break; case 4 : sum /= tmp;break; } } public void geshi(){ tmp = 0; text1.setText(s1); if (flag!=0){ s2 = sum + ""; text2.setText(s2); } s2 = ""; } }
标签:java
原文地址:http://blog.csdn.net/u010499449/article/details/38495611