質問編集履歴
1
マークダウンの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -3,13 +3,10 @@
|
|
3
3
|
・numActionでは、1~9のボタンのどれかを押したとき、選択されているテキストフィールドに出力したい(135とおしたら135で出力)
|
4
4
|
・PmActionでテキストフィールドに入力された数字の符号を入れ替え、またテキストフィールドに出力したい
|
5
5
|
|
6
|
-
|
7
6
|
### 前提
|
8
7
|
|
9
8
|
JavaEclipseで電卓のプログラムを作っています。
|
10
9
|
二点上手くいかない部分がありますが原因は共通していると思われます。
|
11
|
-
|
12
|
-
|
13
10
|
|
14
11
|
### 発生している問題・エラーメッセージ
|
15
12
|
|
@@ -19,26 +16,173 @@
|
|
19
16
|
|
20
17
|
### 該当のソースコード
|
21
18
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
19
|
+
```lang-java
|
20
|
+
|
21
|
+
public class Calculator extends JFrame {
|
22
|
+
|
23
|
+
JTextField input,input2,way, answer;
|
24
|
+
JButton plus, minus, multiple, divide, result, clear, pm, percent, del, few, num, zero;
|
25
|
+
double num1, num2;
|
26
|
+
// String operator = "";
|
27
|
+
JTextField currentFocusField; // フォーカスされているフィールドを追跡する変数
|
28
|
+
|
29
|
+
public static void main(String[] args) {
|
30
|
+
JFrame w = new Calculator("Calculator");
|
31
|
+
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
32
|
+
w.setSize(350, 400);
|
33
|
+
w.setVisible(true);
|
34
|
+
}
|
35
|
+
|
36
|
+
public Calculator(String title) {
|
37
|
+
super(title);
|
38
|
+
JPanel pane = (JPanel) getContentPane();
|
39
|
+
pane.setLayout(new BorderLayout());
|
40
|
+
|
41
|
+
JPanel tfpanel=new JPanel();
|
42
|
+
tfpanel.setLayout(new GridLayout(1,2));
|
43
|
+
input = new JTextField(5);
|
44
|
+
input.setBorder(new TitledBorder("数1"));
|
45
|
+
input.setDocument(new NumericDocument());
|
46
|
+
input.addFocusListener(new FocusAdapter() {
|
47
|
+
@Override
|
48
|
+
public void focusGained(FocusEvent e) {
|
49
|
+
currentFocusField = input; // inputがフォーカスされた時
|
50
|
+
}
|
51
|
+
});
|
52
|
+
tfpanel.add(input);
|
53
|
+
way = new JTextField(1);
|
54
|
+
way.setBorder(new TitledBorder("計算方法"));
|
55
|
+
way.setDocument(new NumericDocument2());
|
56
|
+
tfpanel.add(way);
|
57
|
+
input2 = new JTextField(5);
|
58
|
+
input2.setBorder(new TitledBorder("数2"));
|
59
|
+
input2.setDocument(new NumericDocument());
|
60
|
+
input2.addFocusListener(new FocusAdapter() {
|
61
|
+
@Override
|
62
|
+
public void focusGained(FocusEvent e) {
|
63
|
+
currentFocusField = input2; // input2がフォーカスされた時
|
64
|
+
}
|
65
|
+
});
|
66
|
+
tfpanel.add(input2);
|
67
|
+
|
68
|
+
pane.add(tfpanel, BorderLayout.NORTH);//数式を入力するフィールドの設置
|
69
|
+
|
70
|
+
answer = new JTextField(5);
|
71
|
+
answer.setBorder(new TitledBorder("結果"));
|
72
|
+
pane.add(answer, BorderLayout.SOUTH);//計算結果を表示するフィールドの設置
|
73
|
+
|
74
|
+
JPanel leftpanel = new JPanel();
|
75
|
+
leftpanel.setLayout(new GridLayout(5, 3));
|
76
|
+
leftpanel.setPreferredSize(new Dimension(270, 400));
|
77
|
+
|
78
|
+
clear = new JButton(new ClearAction());
|
79
|
+
leftpanel.add(clear);//クリアボタン
|
80
|
+
|
81
|
+
pm = new JButton(new PmAction());
|
82
|
+
leftpanel.add(pm);//+-切り替えボタン
|
83
|
+
|
84
|
+
percent = new JButton(new PercentAction());
|
85
|
+
leftpanel.add(percent);//%表示切り替えボタン
|
86
|
+
|
87
|
+
for (int i = 1; i <= 9; i++) {
|
88
|
+
num = new JButton(new numAction(Integer.toString(i)));
|
89
|
+
leftpanel.add(num);
|
90
|
+
}//1~9の番号ボタン
|
91
|
+
|
92
|
+
del = new JButton(new DelAction());
|
93
|
+
leftpanel.add(del);//削除ボタン
|
94
|
+
|
95
|
+
zero=new JButton(new ZeroAction());
|
96
|
+
leftpanel.add(zero);
|
97
|
+
|
98
|
+
pane.add(leftpanel, BorderLayout.CENTER);
|
99
|
+
|
100
|
+
JPanel rightpanel = new JPanel();
|
101
|
+
rightpanel.setLayout(new GridLayout(5, 1));
|
102
|
+
|
103
|
+
plus = new JButton(new PlusAction());
|
104
|
+
rightpanel.add(plus);
|
105
|
+
minus = new JButton(new MinusAction());
|
106
|
+
rightpanel.add(minus);
|
107
|
+
multiple = new JButton(new MultipleAction());
|
108
|
+
rightpanel.add(multiple);
|
109
|
+
divide = new JButton(new DivideAction());
|
110
|
+
rightpanel.add(divide);
|
111
|
+
result = new JButton(new ResultAction());
|
112
|
+
rightpanel.add(result);
|
113
|
+
|
114
|
+
pane.add(rightpanel, BorderLayout.EAST);
|
115
|
+
input.requestFocus(); // ここでinputに初期フォーカスを設定
|
116
|
+
}
|
117
|
+
|
118
|
+
// Arithmetic Operations
|
119
|
+
class PlusAction extends AbstractAction {
|
120
|
+
PlusAction() {
|
121
|
+
putValue(Action.NAME, "+");
|
122
|
+
putValue( Action.SHORT_DESCRIPTION, "");
|
123
|
+
}
|
124
|
+
public void actionPerformed(ActionEvent e) {
|
125
|
+
way.setText("+");
|
126
|
+
}
|
127
|
+
}
|
128
|
+
|
129
|
+
class MinusAction extends AbstractAction {
|
130
|
+
MinusAction() {
|
131
|
+
putValue(Action.NAME, "-");
|
132
|
+
}
|
133
|
+
public void actionPerformed(ActionEvent e) {
|
134
|
+
way.setText("-");
|
135
|
+
}
|
136
|
+
}
|
137
|
+
|
138
|
+
class MultipleAction extends AbstractAction {
|
139
|
+
MultipleAction() {
|
140
|
+
putValue(Action.NAME, "*");
|
141
|
+
}
|
142
|
+
public void actionPerformed(ActionEvent e) {
|
143
|
+
way.setText("*");
|
144
|
+
}
|
145
|
+
}
|
146
|
+
|
147
|
+
class DivideAction extends AbstractAction {
|
148
|
+
DivideAction() {
|
149
|
+
putValue(Action.NAME, "/");
|
150
|
+
}
|
151
|
+
public void actionPerformed(ActionEvent e) {
|
152
|
+
way.setText("/");
|
153
|
+
}
|
154
|
+
}
|
155
|
+
|
156
|
+
class ResultAction extends AbstractAction {
|
157
|
+
ResultAction() {
|
158
|
+
putValue(Action.NAME, "=");
|
159
|
+
}
|
160
|
+
public void actionPerformed(ActionEvent e) {
|
161
|
+
|
162
|
+
}
|
163
|
+
}
|
164
|
+
|
165
|
+
// Clear button
|
166
|
+
class ClearAction extends AbstractAction {
|
167
|
+
ClearAction() {
|
168
|
+
putValue(Action.NAME, "AC");
|
169
|
+
}
|
170
|
+
public void actionPerformed(ActionEvent e) {
|
171
|
+
}
|
172
|
+
}
|
173
|
+
|
174
|
+
// Delete last character button
|
175
|
+
class DelAction extends AbstractAction {
|
176
|
+
DelAction() {
|
177
|
+
putValue(Action.NAME, "Del");
|
178
|
+
}
|
179
|
+
public void actionPerformed(ActionEvent e) {
|
180
|
+
|
181
|
+
}
|
182
|
+
}
|
183
|
+
|
184
|
+
// Plus/Minus action button
|
185
|
+
class PmAction extends AbstractAction {
|
42
186
|
PmAction() {
|
43
187
|
putValue(Action.NAME, "+/-");
|
44
188
|
}
|
@@ -64,35 +208,92 @@
|
|
64
208
|
input2.setText("");
|
65
209
|
}
|
66
210
|
}
|
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
|
-
System.out.println(input2num
|
93
|
-
}
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
211
|
+
}
|
212
|
+
}
|
213
|
+
|
214
|
+
class PercentAction extends AbstractAction {
|
215
|
+
double num=0;
|
216
|
+
PercentAction() {
|
217
|
+
putValue(Action.NAME, "%");
|
218
|
+
}
|
219
|
+
public void actionPerformed(ActionEvent e) {
|
220
|
+
}
|
221
|
+
}
|
222
|
+
|
223
|
+
class numAction extends AbstractAction {
|
224
|
+
String number;
|
225
|
+
numAction(String number) {
|
226
|
+
this.number = number;
|
227
|
+
putValue(Action.NAME, number);
|
228
|
+
}
|
229
|
+
public void actionPerformed(ActionEvent e) {
|
230
|
+
String inputnum=input.getText();
|
231
|
+
String input2num=input2.getText();
|
232
|
+
if (currentFocusField == input) {
|
233
|
+
input.setText(inputnum + number);
|
234
|
+
} else if (currentFocusField==input2) {
|
235
|
+
input2.setText(input2num+number);
|
236
|
+
System.out.println(input2num+number);
|
237
|
+
}
|
238
|
+
}
|
239
|
+
}
|
240
|
+
class ZeroAction extends AbstractAction{
|
241
|
+
ZeroAction() {
|
242
|
+
putValue(Action.NAME, "0");
|
243
|
+
}
|
244
|
+
public void actionPerformed(ActionEvent e) {
|
245
|
+
}
|
246
|
+
}
|
247
|
+
|
248
|
+
class NumericDocument extends PlainDocument {
|
249
|
+
String validValues = "0123456789.+-%";
|
250
|
+
|
251
|
+
public void insertString(int offset, String str, AttributeSet a) {
|
252
|
+
if (validValues.indexOf(str) == -1) {
|
253
|
+
return;
|
254
|
+
}
|
255
|
+
try {
|
256
|
+
super.insertString(offset, str, a);
|
257
|
+
} catch (BadLocationException e) {
|
258
|
+
System.out.println(e);
|
259
|
+
}
|
260
|
+
}
|
261
|
+
}
|
262
|
+
|
263
|
+
class NumericDocument2 extends PlainDocument {
|
264
|
+
String validValues = "+-*/%";
|
265
|
+
|
266
|
+
public void insertString(int offset, String str, AttributeSet a) {
|
267
|
+
if( validValues.indexOf( str ) == -1 ){
|
268
|
+
return;
|
269
|
+
}
|
270
|
+
try{
|
271
|
+
super.insertString( offset, str, a );
|
272
|
+
}
|
273
|
+
catch( BadLocationException e ) {
|
274
|
+
System.out.println( e );
|
275
|
+
}
|
276
|
+
}
|
277
|
+
|
278
|
+
}
|
279
|
+
|
280
|
+
class NumericDocument3 extends PlainDocument {
|
281
|
+
String validValues = "0123456789.+-";
|
282
|
+
|
283
|
+
public void insertString(int offset, String str, AttributeSet a) {
|
284
|
+
if( validValues.indexOf( str ) == -1 ){
|
285
|
+
return;
|
286
|
+
}
|
287
|
+
try{
|
288
|
+
super.insertString( offset, str, a );
|
289
|
+
}
|
290
|
+
catch( BadLocationException e ) {
|
291
|
+
System.out.println( e );
|
292
|
+
}
|
293
|
+
}
|
294
|
+
|
295
|
+
}
|
296
|
+
}
|
297
|
+
|
298
|
+
```
|
299
|
+
|