2017-03-03-GUi计算器(未完成)
Xplorist Lv6

2017-03-03-GUi计算器(未完成)

实例1:

用GUI写一个简单的计算器

思路:GUI的实现步骤,基本上分为以下几步,先把界面的布局设置好,窗口类,容器类,组件,窗口和容器的布局,然后就是按钮注册事件监听,核心的功能都是通过事件响应来实现的。

核心代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package com.share.mainboard;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

@SuppressWarnings("serial")
public class MainFrame extends JFrame {
    /** 输入的第1个数 */
    private double num1 = 0;
    /** 输入的第2个数 */
    private double num2 = 0;
    /** 运算的结果 */
    private double result = 0;
    // 创建1~0的按钮对象
    private JButton bt1 = new JButton("1");
    private JButton bt2 = new JButton("2");
    private JButton bt3 = new JButton("3");
    private JButton bt4 = new JButton("4");
    private JButton bt5 = new JButton("5");
    private JButton bt6 = new JButton("6");
    private JButton bt7 = new JButton("7");
    private JButton bt8 = new JButton("8");
    private JButton bt9 = new JButton("9");
    private JButton bt0 = new JButton("0");
    // 创建各种运算符的按钮对象
    private JButton btadd = new JButton("+");
    private JButton btsub = new JButton("-");
    private JButton btmul = new JButton("*");
    private JButton btdiv = new JButton("/");
    private JButton btequ = new JButton("=");
    private JButton btdot = new JButton(".");
    // 创建面板容器对象
    private JPanel pa0 = new JPanel();
    private JPanel pa1 = new JPanel();
    private JPanel pa2 = new JPanel();
    // 创建输入区域对象
    private JTextArea input = new JTextArea();
    private JTextArea output = new JTextArea();
    // 创建标签对象
    private JLabel lab1 = new JLabel("计算:");
    private JLabel lab2 = new JLabel("结果:");

    // 图像用户接口类的构造方法
    public MainFrame(String str) {
        super(str);
        // 设置窗口的大小
        setSize(300, 300);
        // 设置窗口的位置
        setLocation(300, 300);
        // 设置窗口的布局和窗口中添加面板容器
        setLayout(new GridLayout(3, 1));
        add(pa0);
        add(pa1);
        add(pa2);

        // 面板容器中设置布局和添加组件
        pa0.setLayout(new GridLayout(2, 1));
        pa0.add(lab1);
        pa0.add(input);

        pa1.setLayout(new GridLayout(4, 4));
        pa1.add(bt1);
        pa1.add(bt2);
        pa1.add(bt3);
        pa1.add(btadd);

        pa1.add(bt4);
        pa1.add(bt5);
        pa1.add(bt6);
        pa1.add(btsub);

        pa1.add(bt7);
        pa1.add(bt8);
        pa1.add(bt9);
        pa1.add(btmul);

        pa1.add(bt0);
        pa1.add(btdot);
        pa1.add(btequ);
        pa1.add(btdiv);

        pa2.setLayout(new GridLayout(2, 1));
        pa2.add(lab2);
        pa2.add(output);

        // 给按键注册事件监听
        bt1.addActionListener(new bt1ActionListener());
        bt2.addActionListener(new bt2ActionListener());
        bt3.addActionListener(new bt3ActionListener());
        bt4.addActionListener(new bt4ActionListener());
        bt5.addActionListener(new bt5ActionListener());
        bt6.addActionListener(new bt6ActionListener());
        bt7.addActionListener(new bt7ActionListener());
        bt8.addActionListener(new bt8ActionListener());
        bt9.addActionListener(new bt9ActionListener());
        bt0.addActionListener(new bt0ActionListener());

        btadd.addActionListener(new btaddActionListener());
        btsub.addActionListener(new btsubActionListener());
        btmul.addActionListener(new btmulActionListener());
        btdiv.addActionListener(new btdivActionListener());
        btdot.addActionListener(new btdotActionListener());
        btequ.addActionListener(new btequActionListener());

        setVisible(true);
    }

    class bt1ActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            input.append("1");
        }

    }

    class bt2ActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            input.append("2");
        }

    }

    class bt3ActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            input.append("3");
        }

    }

    class bt4ActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            input.append("4");
        }

    }

    class bt5ActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            input.append("5");
        }

    }

    class bt6ActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            input.append("6");
        }

    }

    class bt7ActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            input.append("7");
        }

    }

    class bt8ActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            input.append("8");
        }

    }

    class bt9ActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            input.append("9");
        }

    }

    class bt0ActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            input.append("0");
        }

    }

    class btaddActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            input.append("+");
        }

    }

    class btsubActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            input.append("-");
        }

    }

    class btmulActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            input.append("*");
        }

    }

    class btdivActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            input.append("/");
        }

    }

    class btdotActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            input.append(".");
        }

    }

    class btequActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            String inStr = input.getText();
            inStr.indexOf("+");

            input.append("=");
            String[] strArray = new String[2];
            if (inStr.contains("+")) {
                strArray[0]=inStr.substring(0, inStr.indexOf("+"));
                strArray[1]=inStr.substring(inStr.indexOf("+")+1);
                num1 = Double.parseDouble(strArray[0]);
                num2 = Double.parseDouble(strArray[1]);

                result = num1 + num2;
            } else if (inStr.contains("-")) {
                strArray[0]=inStr.substring(0, inStr.indexOf("-"));
                strArray[1]=inStr.substring(inStr.indexOf("-")+1);
                num1 = Double.parseDouble(strArray[0]);
                num2 = Double.parseDouble(strArray[1]);

                result = num1 - num2;
            } else if (inStr.contains("*")) {
                strArray[0]=inStr.substring(0, inStr.indexOf("*"));
                strArray[1]=inStr.substring(inStr.indexOf("*")+1);
                num1 = Double.parseDouble(strArray[0]);
                num2 = Double.parseDouble(strArray[1]);
                result = num1 * num2;
            } else if (inStr.contains("/")) {
                strArray[0]=inStr.substring(0, inStr.indexOf("/"));
                strArray[1]=inStr.substring(inStr.indexOf("/")+1);
                num1 = Double.parseDouble(strArray[0]);
                num2 = Double.parseDouble(strArray[1]);
                if (num2 == 0) {
                    result = Double.NaN;
                } else {
                    result = num1 / num2;
                }
            }

            output.setText(Double.toString(result));
        }

    }

    // 主方法
    public static void main(String[] args) {
        new MainFrame("Calculator");
    }

}
 评论