import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Calculater extends JFrame implements ActionListener { private JPanel jPanel; private JPanel jPanel1; private Button button[] = new Button[24]; private TextField TF; private String st = ""; private JScrollPane jscrollPane; private JLabel label; public void Define() { JFrame jFrame = new JFrame("计算器"); Container pane = jFrame.getContentPane(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jPanel = new JPanel(new GridLayout(6,4,5,5)); jPanel1 = new JPanel(new GridLayout(2,1,5,5)); TF = new TextField(st); button[0] = new Button("sin"); button[0].addActionListener(this); button[1] = new Button("cos"); button[1].addActionListener(this); button[2] = new Button("tan"); button[2].addActionListener(this); button[3] = new Button("e^x"); button[3].addActionListener(this); button[4] = new Button("c"); button[4].addActionListener(this); button[5] = new Button("÷"); button[5].addActionListener(this); button[6] = new Button("×"); button[6].addActionListener(this); button[7] = new Button("<-"); button[7].addActionListener(this); button[8] = new Button("7"); button[8].addActionListener(this); button[9] = new Button("8"); button[9].addActionListener(this); button[10] = new Button("9"); button[10].addActionListener(this); button[11] = new Button("-"); button[11].addActionListener(this); button[12] = new Button("4"); button[12].addActionListener(this); button[13] = new Button("5"); button[13].addActionListener(this); button[14] = new Button("6"); button[14].addActionListener(this); button[15] = new Button("+"); button[15].addActionListener(this); button[16] = new Button("1"); button[16].addActionListener(this); button[17] = new Button("2"); button[17].addActionListener(this); button[18] = new Button("3"); button[18].addActionListener(this); button[19] = new Button("log"); button[19].addActionListener(this); button[20] = new Button("%"); button[20].addActionListener(this); button[21] = new Button("0"); button[21].addActionListener(this); button[22] = new Button("."); button[22].addActionListener(this); button[23] = new Button("="); button[23].addActionListener(this); jPanel.add(button[0]); jPanel.add(button[1]); jPanel.add(button[2]); jPanel.add(button[3]); jPanel.add(button[4]); jPanel.add(button[5]); jPanel.add(button[6]); jPanel.add(button[7]); jPanel.add(button[8]); jPanel.add(button[9]); jPanel.add(button[10]); jPanel.add(button[11]); jPanel.add(button[12]); jPanel.add(button[13]); jPanel.add(button[14]); jPanel.add(button[15]); jPanel.add(button[16]); jPanel.add(button[17]); jPanel.add(button[18]); jPanel.add(button[19]); jPanel.add(button[20]); jPanel.add(button[21]); jPanel.add(button[22]); jPanel.add(button[23]); jPanel1.add(TF); jPanel1.add(jPanel); jscrollPane = new JScrollPane(label,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); pane.add(jPanel1,BorderLayout.WEST); pane.add(jscrollPane,BorderLayout.EAST); jFrame.pack(); jFrame.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getActionCommand() == "0") { st = st + "0"; this.TF.setText(st); } if(e.getActionCommand() == "1") { st = st + "1"; this.TF.setText(st); } if(e.getActionCommand() == "2") { st = st + "2"; this.TF.setText(st); } if(e.getActionCommand() == "3") { st = st + "3"; this.TF.setText(st); } if(e.getActionCommand() == "4") { st = st + "4"; this.TF.setText(st); } if(e.getActionCommand() == "5") { st = st + "5"; this.TF.setText(st); } if(e.getActionCommand() == "6") { st = st + "6"; this.TF.setText(st); } if(e.getActionCommand() == "7") { st = st + "7"; this.TF.setText(st); } if(e.getActionCommand() == "8") { st = st + "8"; this.TF.setText(st); } if(e.getActionCommand() == "9") { st = st + "9"; this.TF.setText(st); } if(e.getActionCommand() == ".") { st = st + "."; this.TF.setText(st); } if(e.getActionCommand() == "+") { st = st + "+"; this.TF.setText(st); } if(e.getActionCommand() == "-") { st = st + "-"; this.TF.setText(st); } if(e.getActionCommand() == "×") { st = st + "×"; this.TF.setText(st); } if(e.getActionCommand() == "÷") { st = st + "÷"; this.TF.setText(st); } if(e.getActionCommand() == "sin") { st = st + "sin"; this.TF.setText(st); } if(e.getActionCommand() == "cos") { st = st + "cos"; this.TF.setText(st); } if(e.getActionCommand() == "tan") { st = st + "tan"; this.TF.setText(st); } if(e.getActionCommand() == "%") { st = st + "%"; this.TF.setText(st); } if(e.getActionCommand() == "e^x") { st = st + "e^"; this.TF.setText(st); } if(e.getActionCommand() == "log") { st = st + "log"; this.TF.setText(st); } if(e.getActionCommand() == "c") { st = ""; this.TF.setText(st); } if(e.getActionCommand() == "=") { st = "你的算法"; this.TF.setText(st); } } public static void main(String arg[]) { Calculater A = new Calculater(); A.Define(); } }