这个计算器程序是对Swing的一个练习,逻辑比较简单 它只能进行单次运算(不能进行多项式运算),考虑了除数不能为0的情况 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Calculator extends JFrame implements ActionListener{ JTextField jtf = new JTextField(); JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b0; JButton badd,bsub,bmul,bdiv,bequal,bdot; int select = 0; JPanel jp1 = new JPanel(); //分别代表0~9数字和加减乘除等于和点符号 double temp = 0.0; double num = 0.0; public Calculator(){ //生成两个区域,上面区域为一个文本域,下面为计算器的按钮 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = this.getContentPane(); jtf.setHorizontalAlignment(JTextField.RIGHT); c.add(jtf,BorderLayout.NORTH); c.add(jp1,BorderLayout.CENTER); jp1.setLayout(new GridLayout(4,4)); b1 = new JButton("1"); b1.addActionListener(this); jp1.add(b1); b2 = new JButton("2"); b2.addActionListener(this); jp1.add(b2); b3 = new JButton("3"); b3.addActionListener(this); jp1.add(b3); badd = new JButton("+"); badd.addActionListener(this); jp1.add(badd); b4 = new JButton("4"); b4.addActionListener(this); jp1.add(b4); b5 = new JButton("5"); b5.addActionListener(this); jp1.add(b5); b6 = new JButton("6"); b6.addActionListener(this); jp1.add(b6); bsub = new JButton("-"); bsub.addActionListener(this); jp1.add(bsub); b7 = new JButton("7"); b7.addActionListener(this); jp1.add(b7); b8 = new JButton("8"); b8.addActionListener(this); jp1.add(b8); b9 = new JButton("9"); b9.addActionListener(this); jp1.add(b9); bmul = new JButton("*"); bmul.addActionListener(this); jp1.add(bmul); b0 = new JButton("0"); b0.addActionListener(this); jp1.add(b0); bdot = new JButton("."); bdot.addActionListener(this); jp1.add(bdot); bequal = new JButton("="); bequal.addActionListener(this); jp1.add(bequal); bdiv = new JButton("//"); bdiv.addActionListener(this); jp1.add(bdiv); } public void actionPerformed(ActionEvent e){ //jtf.setText(jtf.getText()+e.getActionCommand()); if(e.getSource() == b1){ jtf.setText(jtf.getText()+e.getActionCommand()); } if(e.getSource() == b2){ jtf.setText(jtf.getText()+e.getActionCommand()); } if(e.getSource() == b3){ jtf.setText(jtf.getText()+e.getActionCommand()); } if(e.getSource() == b4){ jtf.setText(jtf.getText()+e.getActionCommand()); } if(e.getSource() == b5){ jtf.setText(jtf.getText()+e.getActionCommand()); } if(e.getSource() == b6){ jtf.setText(jtf.getText()+e.getActionCommand()); } if(e.getSource() == b7){ jtf.setText(jtf.getText()+e.getActionCommand()); } if(e.getSource() == b8){ jtf.setText(jtf.getText()+e.getActionCommand()); } if(e.getSource() == b9){ jtf.setText(jtf.getText()+e.getActionCommand()); } if(e.getSource() == b0){ jtf.setText(jtf.getText()+e.getActionCommand()); } if(e.getSource() == badd){ temp = Double.parseDouble(jtf.getText()); jtf.setText(""); select = 1; } if(e.getSource() == bsub){ temp = Double.parseDouble(jtf.getText()); jtf.setText(""); select = 2; } if(e.getSource() == bmul){ temp = Double.parseDouble(jtf.getText()); jtf.setText(""); select = 3; } if(e.getSource() == bdiv){ temp = Double.parseDouble(jtf.getText()); jtf.setText(""); select = 4; } if(e.getSource() == bequal){ if(select == 1){ num = Double.parseDouble(jtf.getText()); jtf.setText((num = temp+num)+""); } else if(select == 2){ num = Double.parseDouble(jtf.getText()); jtf.setText((num = temp-num)+""); } else if(select == 3){ num = Double.parseDouble(jtf.getText()); jtf.setText((num = temp*num)+""); } else if(select == 4){ num = Double.parseDouble(jtf.getText()); if(num == 0){ JOptionPane.showMessageDialog(jp1, "除数不能为0"); } jtf.setText((num = temp/num)+""); } } } public static void main(String[] args) { Calculator mainFrame = new Calculator(); mainFrame.setSize(400,400); mainFrame.setTitle("calculator by jk"); mainFrame.setVisible(true); } }