回答編集履歴
1
数字ボタンの件を追加
test
CHANGED
@@ -77,3 +77,63 @@
|
|
77
77
|
button14.addActionListener(calcActionListener);
|
78
78
|
|
79
79
|
```
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
---
|
84
|
+
|
85
|
+
なお, やたらと並んでいる数字ボタン(オリジナルの NumberButton )も, 同様にできます.
|
86
|
+
|
87
|
+
恐らく ActionListener を抜き出す際に JButton.getText が使えなくなって全部個別に並べることになったものと思います.
|
88
|
+
|
89
|
+
```java
|
90
|
+
|
91
|
+
public class NumberActionListener implements ActionListener {
|
92
|
+
|
93
|
+
public void actionPerformed(ActionEvent evt) {
|
94
|
+
|
95
|
+
//String keyNumber = this.getText();
|
96
|
+
|
97
|
+
String keyNumber = ((JButton)evt.getSource()).getText(); //ボタンの名前を取り出す
|
98
|
+
|
99
|
+
appendResult(keyNumber); //ボタンの名前をテキストフィールドにつなげる
|
100
|
+
|
101
|
+
}
|
102
|
+
|
103
|
+
/* テキストフィールドに引数の文字列をつなげる */
|
104
|
+
|
105
|
+
public void appendResult(String c) {
|
106
|
+
|
107
|
+
if (!afterCalc) //演算子ボタンを押した直後でないなら
|
108
|
+
|
109
|
+
result.setText(result.getText() + c); //押したボタンの名前をつなげる
|
110
|
+
|
111
|
+
else {
|
112
|
+
|
113
|
+
result.setText(c); //押したボタンの文字列だけを設定する(いったんクリアしたかに見える)
|
114
|
+
|
115
|
+
afterCalc = false;
|
116
|
+
|
117
|
+
}
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
```
|
124
|
+
|
125
|
+
```java
|
126
|
+
|
127
|
+
ActionListener numberActionListener = new NumberActionListener ();
|
128
|
+
|
129
|
+
button1.addActionListener(numberActionListener);
|
130
|
+
|
131
|
+
button2.addActionListener(numberActionListener);
|
132
|
+
|
133
|
+
button3.addActionListener(numberActionListener);
|
134
|
+
|
135
|
+
button4.addActionListener(numberActionListener);
|
136
|
+
|
137
|
+
(以下略)
|
138
|
+
|
139
|
+
```
|