?????????????
??????java??????????????????????????£?
1 /*CJSCalculator.java 2014.8.4 by cjs
2  *????????м?????????????????????????????????
3  *?????“OK”???????????12+2????????????????????????
4  *?????????????????????????У??????????????????????????
5  *?????????汾????????????????????????????????????
6 **/
7
8 import java.awt.*;
9 import javax.swing.*;
10 import java.awt.event.*;
11 public class CjsCalculator extends JFrame implements ActionListener {
12     /* ???Jframe ??? ActionListener ???*/
13
14         //Э????????
15         private class WindowCloser extends WindowAdapter {
16         public void windowClosing(WindowEvent we) {
17             System.exit(0);
18         }
19     }
20     //strings for operator buttons.
21
22     private String[] str = { "+"?? "-"?? "*"?? "/"?? "OK"};
23
24     //build buttons.
25
26     JButton[] Obuttons = new JButton[str.length];
27         //reset button
28     JButton Rbutton = new JButton("reset");
29
30         //build textfield to show num and result
31
32     private JTextField display = new JTextField("0");
33     private JTextField Fnum = new JTextField("");
34     private JTextField Snum = new JTextField("");
35     private JTextField Otext = new JTextField("");
36     private JTextField Deng = new JTextField("=");
37
38     int i = 0;
39
40     //?????????????
41     public CjsCalculator() {
42
43         Deng.setEditable(false);
44         display.setEditable(false);
45     Otext.setEditable(false);
46     //super ????
47     //    super("Calculator");
48
49         //panel ???????
50         JPanel panel1 = new JPanel(new GridLayout(1??5));
51         for (i = 0; i < str.length; i++) {
52             Obuttons[i] = new JButton(str[i]);
53                 Obuttons[i].setBackground(Color.YELLOW);
54             panel1.add(Obuttons[i]);
55         }
56
57         JPanel panel2 = new JPanel(new GridLayout(1??5));
58         panel2.add(Fnum);
59         panel2.add(Otext);
60         panel2.add(Snum);
61         panel2.add(Deng);
62         panel2.add(display);
63
64         JPanel panel3 = new JPanel(new GridLayout(1??1));
65         panel3.add(Rbutton);
66                 //?????????
67         getContentPane().setLayout(new BorderLayout());
68         getContentPane().add("North"??panel2);
69         getContentPane().add("Center"??panel1);
70         getContentPane().add("South"??panel3);
71         //Add listener for Obuttons.
72         for (i = 0; i < str.length; i++)
73             Obuttons[i].addActionListener(this);
74
75         display.addActionListener(this);
76             Rbutton.addActionListener(this);
77         setSize(8000??8000);//don't use ???
78
79         setVisible(true);//???
80                 //???????С
81                 setResizable(false);
82         //?????????
83         pack();
84     }
85
86         //??????????performed????
87     public void actionPerformed(ActionEvent e) {
88                 Object happen = e.getSource();
89         //
90         String label = e.getActionCommand();
91
92         if ("+-*/".indexOf(label) >= 0)
93             getOperator(label);
94         else if (label == "OK")
95             getEnd(label);
96         else if ("reset".indexOf(label) >= 0)
97              // display.setText("reset");
98                       resetAll(label);
99     }