一、界面学习
用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 }
|
00 public void resetAll(String key) {
101 Fnum.setText("");
102 Snum.setText("");
103 display.setText("");
104 Otext.setText("");
105 }
106 public void getOperator(String key) {
107 Otext.setText(key);
108 }
109
110 public void getEnd(String label) {
111 if( (countDot(Fnum.getText()) > 1) || (countDot(Snum.getText())>1) || (Fnum.getText().length()==0) ||
(Snum.getText().length() == 0)) {
112 display.setText("error");
113 }
114 else if(checkNum(Fnum.getText())==false || checkNum(Snum.getText())==false){
115 display.setText("error");
116 }
117 else {
118 double Fnumber = Double.parseDouble(Fnum.getText().trim());
119 double Snumber = Double.parseDouble(Snum.getText().trim());
120 if (Fnum.getText() != "" && Snum.getText() != "") {
121 if (Otext.getText().indexOf("+") >= 0) {
122 double CjsEnd = Fnumber + Snumber;
123 display.setText(String.valueOf(CjsEnd));
124 }
125 else if (Otext.getText().indexOf("-")>=0) {
126 double CjsEnd = Fnumber - Snumber;
127 display.setText(String.valueOf(CjsEnd));
128 }
129 else if (Otext.getText().indexOf("*")>=0) {
130 double CjsEnd = Fnumber * Snumber;
131 display.setText(String.valueOf(CjsEnd));
132 }
133 else if (Otext.getText().indexOf("/")>=0) {
134 double CjsEnd = Fnumber / Snumber;
135 display.setText(String.valueOf(CjsEnd));
136 }
137 else
138 display.setText("error");
139
140 }
141 else
142 display.setText("num is null");
143 }
144
145 }
146 public int countDot(String str) {
147 int count = 0;
148 for (char c:str.toCharArray()) {
149 if (c == '.')
150 count++;
151 }
152 return count;
153 }
154 public boolean checkNum(String str) {
155 boolean tmp = true;
156 for (char c:str.toCharArray()) {
157 if (Character.isDigit(c) || (c == '.'));
158 else {
159 tmp = false;
160 break;
161 }
162 }
163 return tmp;
164 }
165 public static void main(String[] args) {
166 new CjsCalculator();
167 }
168 }
|
终端运行该java文件,结果如图所示:
最新内容请见作者的GitHub页:http://qaseven.github.io/