本文实例讲述了java实现简单的计算器类。分享给大家供大家参考。具体如下:
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
|
package chap;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator {
private JFrame frame;
private JPanel panel,panelKeys,panelKeys_up,panelKeys_down;
private JTextField textComputer; //计算区域
private JButton buttonBk,buttonC; //退格键和清零键
private JButton button[]; //数字键组
private JButton buttonDot,buttonAddSub,buttonAdd,buttonSub,buttonMul,buttonDiv,button1,button2,button3,buttonEqual;
private double result; //计算结果
private final short ADD = 1 ;
private final short SUB = 2 ;
private final short MUL = 3 ;
private final short DIV = 4 ;
private short operator = - 1 ; //运算符
public Calculator(){
frame = new JFrame( "计算机" );
frame.setSize( 400 , 250 );
panel = new JPanel(); //全局面板
panel.setVisible( true );
frame.setVisible( true );
frame.getContentPane().add(panel);
ActionListener listener = new ComputerActionListener(); //按键监听器
//计算区
textComputer = new JTextField( 15 );
textComputer.setText( "" );
textComputer.setEditable( false );
textComputer.setBackground( new Color( 255 , 255 , 255 ));
//功能键上半部分
panelKeys_up = new JPanel();
panelKeys_up.setLayout( new FlowLayout(FlowLayout.RIGHT));
buttonBk = new JButton( "Backspace" );
buttonBk.setForeground( new Color( 255 , 0 , 0 ));
buttonC = new JButton( "C" );
buttonC.setForeground( new Color( 255 , 0 , 0 ));
buttonBk.addActionListener(listener);
buttonC.addActionListener(listener);
panelKeys_up.add(buttonBk);
panelKeys_up.add(buttonC);
//功能键下半部分
panelKeys_down = new JPanel();
panelKeys_down.setLayout( new GridLayout( 4 , 5 ));
button = new JButton[ 10 ];
for ( int i = 0 ;i < button.length;i++){
button[i] = new JButton(Integer.toString(i));
button[i].setForeground( new Color( 255 , 0 , 0 ));
}
buttonAddSub = new JButton( "+/-" );
buttonAddSub.setForeground( new Color( 255 , 0 , 0 ));
buttonAdd = new JButton( "+" );
buttonAdd.setForeground( new Color( 255 , 0 , 0 ));
buttonSub = new JButton( "-" );
buttonSub.setForeground( new Color( 255 , 0 , 0 ));
buttonMul = new JButton( "*" );
buttonMul.setForeground( new Color( 255 , 0 , 0 ));
buttonDiv = new JButton( "/" );
buttonDiv.setForeground( new Color( 255 , 0 , 0 ));
button1 = new JButton();
button2 = new JButton();
button3 = new JButton();
button1.setForeground( new Color( 255 , 0 , 0 ));
button2.setForeground( new Color( 255 , 0 , 0 ));
button3.setForeground( new Color( 255 , 0 , 0 ));
buttonEqual = new JButton( "=" );
buttonEqual.setForeground( new Color( 255 , 0 , 0 ));
buttonAddSub.addActionListener(listener);
buttonAdd.addActionListener(listener);
buttonSub.addActionListener(listener);
buttonMul.addActionListener(listener);
buttonDiv.addActionListener(listener);
buttonEqual.addActionListener(listener);
for ( int i = 0 ; i <= 9 ; i++){
button[i].addActionListener(listener);
}
for ( int i = 0 ; i <= 9 ; i++){
panelKeys_down.add(button[i]);
}
panelKeys_down.add(buttonAddSub);
panelKeys_down.add(buttonAdd);
panelKeys_down.add(buttonSub);
panelKeys_down.add(buttonMul);
panelKeys_down.add(buttonDiv);
panelKeys_down.add(buttonEqual);
panel.setLayout( new BorderLayout());
panel.add(textComputer,BorderLayout.NORTH);
panel.add(panelKeys_up,BorderLayout.CENTER);
panel.add(panelKeys_down,BorderLayout.SOUTH);
}
class ComputerActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent event) {
// TODO Auto-generated method stub
Object keyButton = event.getSource();
String text = textComputer.getText();
DecimalFormat df = new DecimalFormat( "0.###########" );
//Backspace
if (keyButton == buttonBk && text.length() > 0 ){
textComputer.setText(text.substring( 0 ,text.length()- 1 ));
}
//C键
if (keyButton == buttonC){
result = 0 ;
textComputer.setText( "" );
}
//数字键
for ( int i= 0 ;i< 10 ;i++){
if (keyButton == button[i]){
textComputer.setText(text+i);
}
}
if (keyButton == buttonAdd){
operator = 1 ;
}
if (keyButton == buttonSub){
operator = 2 ;
}
if (keyButton == buttonMul){
operator = 3 ;
}
if (keyButton == buttonDiv){
operator = 4 ;
}
//符号键
if (keyButton == buttonAdd || keyButton == buttonSub || keyButton == buttonMul || keyButton == buttonDiv || keyButton == buttonEqual){
switch (operator){
case ADD:
result += Double.parseDouble(text);
break ;
case SUB:
result -=Double.parseDouble(text);
break ;
case MUL:
result *=Double.parseDouble(text);
break ;
case DIV:
result /=Double.parseDouble(text);
break ;
default :
result = Double.parseDouble(text);
}
textComputer.setText( "" );
}
if (keyButton == buttonEqual){
textComputer.setText(String.valueOf(result));
}
}
}
public static void main(String args[]){
new Calculator();
}
}
|
希望本文所述对大家的java程序设计有所帮助。