本文實(shí)例講述了java實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器類。分享給大家供大家參考。具體如下:
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; //計(jì)算區(qū)域 private JButton buttonBk,buttonC; //退格鍵和清零鍵 private JButton button[]; //數(shù)字鍵組 private JButton buttonDot,buttonAddSub,buttonAdd,buttonSub,buttonMul,buttonDiv,button1,button2,button3,buttonEqual; private double result; //計(jì)算結(jié)果 private final short ADD = 1 ; private final short SUB = 2 ; private final short MUL = 3 ; private final short DIV = 4 ; private short operator = - 1 ; //運(yùn)算符 public Calculator(){ frame = new JFrame( "計(jì)算機(jī)" ); frame.setSize( 400 , 250 ); panel = new JPanel(); //全局面板 panel.setVisible( true ); frame.setVisible( true ); frame.getContentPane().add(panel); ActionListener listener = new ComputerActionListener(); //按鍵監(jiān)聽(tīng)器 //計(jì)算區(qū) 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( "" ); } //數(shù)字鍵 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 ; } //符號(hào)鍵 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(); } } |
希望本文所述對(duì)大家的java程序設(shè)計(jì)有所幫助。