1 / 1 / 2
Регистрация: 01.03.2017
Сообщений: 15
|
|
1
|
Пропадает интерфейс в калькуляторе
01.03.2017, 20:34. Показов 972. Ответов 2
Приветствую! Написал GUI калькулятор. У меня работает отлично, а вот на другом компьютере иногда отказываются появляться кнопки. Не знаю в чем проблема, возможно из-за кривого и не оптимизированного кода, а может я что-то просмотрел.
Буду очень благодарен за любую помощь!
Калькулятор в нормальном виде:
Калькулятор на другом компьютере:
Код:
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
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
| //Made by SharpSided
//Date 26.02.2017
package com.sharpsided.main;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
@SuppressWarnings("serial")
public class WindowCalc extends JFrame{
private JButton previusButton;
private int numberOne;
private int numberTwo;
private JTextField numberField = new JTextField(20);
private Font font1 = new Font("Arial", Font.BOLD, 20);
private Font font2 = new Font("Arial", Font.BOLD, 15);
private JButton submitButton = new JButton("=");
private JButton plusButton = new JButton("+");
private JButton minusButton = new JButton("-");
private JButton delButton = new JButton("/");
private JButton myltiButton = new JButton("x");
private JButton clearButton = new JButton("Clear");
private JButton button1 = new JButton("1");
private JButton button2 = new JButton("2");
private JButton button3 = new JButton("3");
private JButton button4 = new JButton("4");
private JButton button5 = new JButton("5");
private JButton button6 = new JButton("6");
private JButton button7 = new JButton("7");
private JButton button8 = new JButton("8");
private JButton button9 = new JButton("9");
private JButton button0 = new JButton("0");
ActionTaker taker = new ActionTaker();
public WindowCalc(){
super("Calculator by Yury");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(240, 290);
setResizable(false);
setLocationRelativeTo(null);
}
public void start(){
JPanel panel2 = new JPanel();
panel2.setLayout(new FlowLayout());
panel2.add(clearButton);
clearButton.setFont(font2);
clearButton.setForeground(Color.red);
clearButton.addActionListener(taker);
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(5,3,10,5));
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.add(numberField);
setContentPane(panel);
panel.add(panel1);
panel.add(panel2);
submitButton.setFont(font1);
submitButton.setForeground(Color.red);
submitButton.addActionListener(taker);
plusButton.setFont(font1);
plusButton.setForeground(Color.red);
plusButton.addActionListener(taker);
minusButton.setFont(font1);
minusButton.setForeground(Color.red);
minusButton.addActionListener(taker);
delButton.setFont(font1);
delButton.setForeground(Color.red);
delButton.addActionListener(taker);
myltiButton.setFont(font1);
myltiButton.setForeground(Color.red);
myltiButton.addActionListener(taker);
button1.setFont(font1);
button1.addActionListener(taker);
button2.setFont(font1);
button2.addActionListener(taker);
button3.setFont(font1);
button3.addActionListener(taker);
button4.setFont(font1);
button4.addActionListener(taker);
button5.setFont(font1);
button5.addActionListener(taker);
button6.setFont(font1);
button6.addActionListener(taker);
button7.setFont(font1);
button7.addActionListener(taker);
button8.setFont(font1);
button8.addActionListener(taker);
button9.setFont(font1);
button9.addActionListener(taker);
button0.setFont(font1);
button0.addActionListener(taker);
panel1.add(button1);
panel1.add(button2);
panel1.add(button3);
panel1.add(button4);
panel1.add(button5);
panel1.add(button6);
panel1.add(button7);
panel1.add(button8);
panel1.add(button9);
panel1.add(button0);
panel1.add(submitButton);
panel1.add(plusButton);
panel1.add(minusButton);
panel1.add(delButton);
panel1.add(myltiButton);
}
class ActionTaker implements ActionListener{
public void actionPerformed(ActionEvent event){
if(event.getSource() == button1) {
String s = numberField.getText().concat("1");
numberField.setText(s);
}
if(event.getSource() == button2) {
String s = numberField.getText().concat("2");
numberField.setText(s);
}
if(event.getSource() == button3) {
String s = numberField.getText().concat("3");
numberField.setText(s);
}
if(event.getSource() == button4) {
String s = numberField.getText().concat("4");
numberField.setText(s);
}
if(event.getSource() == button5) {
String s = numberField.getText().concat("5");
numberField.setText(s);
}
if(event.getSource() == button6) {
String s = numberField.getText().concat("6");
numberField.setText(s);
}
if(event.getSource() == button7) {
String s = numberField.getText().concat("7");
numberField.setText(s);
}
if(event.getSource() == button8) {
String s = numberField.getText().concat("8");
numberField.setText(s);
}
if(event.getSource() == button9) {
String s = numberField.getText().concat("9");
numberField.setText(s);
}
if(event.getSource() == button0) {
String s = numberField.getText().concat("0");
numberField.setText(s);
}
if(event.getSource() == clearButton) {
numberField.setText("");
numberOne = 0;
numberTwo = 0;
}
if(event.getSource() == myltiButton) {
try {
String text = numberField.getText();
numberOne = Integer.parseInt(text);
numberField.setText(null);
previusButton = myltiButton;
} catch (NumberFormatException ex){
numberField.setText("");
}
}
if(event.getSource() == delButton) {
try {
String text = numberField.getText();
numberOne = Integer.parseInt(text);
numberField.setText(null);
previusButton = delButton;
} catch (NumberFormatException ex){
numberField.setText("");
}
}
if(event.getSource() == minusButton) {
try {
String text = numberField.getText();
numberOne = Integer.parseInt(text);
numberField.setText(null);
previusButton = minusButton;
} catch (NumberFormatException ex){
numberField.setText("");
}
}
if(event.getSource() == plusButton) {
try {
String text = numberField.getText();
numberOne = Integer.parseInt(text);
numberField.setText(null);
previusButton = plusButton;
} catch (NumberFormatException ex){
numberField.setText("");
}
}
if(event.getSource() == submitButton) {
try {
String text = numberField.getText();
numberTwo = Integer.parseInt(text);
int result = 0;
if(previusButton == plusButton){
result = numberOne + numberTwo;
}
if(previusButton == minusButton){
result = numberOne - numberTwo;
}
if(previusButton == delButton){
result = numberOne / numberTwo;
}
if(previusButton == myltiButton){
result = numberOne * numberTwo;
}
String strResult = String.valueOf(result);
numberField.setText(strResult);
} catch (NumberFormatException ex) {
numberField.setText("");
}
}
}
}
} |
|
Класс Main:
Java | 1
2
3
4
5
6
7
8
9
10
11
12
13
| //Made by SharpSided
//Date 26.02.2017
package com.sharpsided.main;
public class Main {
public static void main (String[] args){
WindowCalc window = new WindowCalc();
window.setVisible(true);
window.start();
}
} |
|
0
|