回答編集履歴
2
extends JFrame について追記
answer
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
ActionListener を実装している Button のインスタンス a が, JButton のインスタンス button に登録されていないようです.
|
2
2
|
|
3
3
|
他にも...
|
4
|
+
Button クラスは JFrame としては機能させていませんので extends JFrame は無駄です.
|
4
5
|
Button クラスのコンストラクタでのみ count を加算していては何時までたっても 10 にはなりません.
|
5
6
|
同じくコンストラクタで, img1.png のラベルを生成しても使われておりません.
|
6
7
|
actionPerformed 内で, ```Container contentPane=new Container();``` としても, JFrame の contentPane とは無関係ですので, 何の操作をしても意味はありません.
|
1
コード等追加
answer
CHANGED
@@ -1,1 +1,58 @@
|
|
1
|
-
ActionListener を実装している Button のインスタンス a が, JButton のインスタンス button に登録されていないようです.
|
1
|
+
ActionListener を実装している Button のインスタンス a が, JButton のインスタンス button に登録されていないようです.
|
2
|
+
|
3
|
+
他にも...
|
4
|
+
Button クラスのコンストラクタでのみ count を加算していては何時までたっても 10 にはなりません.
|
5
|
+
同じくコンストラクタで, img1.png のラベルを生成しても使われておりません.
|
6
|
+
actionPerformed 内で, ```Container contentPane=new Container();``` としても, JFrame の contentPane とは無関係ですので, 何の操作をしても意味はありません.
|
7
|
+
|
8
|
+
```java
|
9
|
+
import java.awt.BorderLayout;
|
10
|
+
import java.awt.event.ActionEvent;
|
11
|
+
import java.awt.event.ActionListener;
|
12
|
+
|
13
|
+
import javax.swing.Icon;
|
14
|
+
import javax.swing.ImageIcon;
|
15
|
+
import javax.swing.JButton;
|
16
|
+
import javax.swing.JFrame;
|
17
|
+
import javax.swing.JLabel;
|
18
|
+
|
19
|
+
public class LoveSquid extends JFrame {
|
20
|
+
public static void main(String[] args) {
|
21
|
+
new LoveSquid().setVisible(true);
|
22
|
+
}
|
23
|
+
LoveSquid() {
|
24
|
+
super("イカちゃん");
|
25
|
+
setSize(700,460);
|
26
|
+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
27
|
+
|
28
|
+
JLabel label = new JLabel("イカちゃんを愛でよう!あ、拡大はしないで欲しいでゲソ");
|
29
|
+
label.setHorizontalAlignment(JLabel.CENTER);
|
30
|
+
add(label,BorderLayout.NORTH);
|
31
|
+
|
32
|
+
ImageIcon icon1 = new ImageIcon("C:/イカ愛で/img1.png");
|
33
|
+
JLabel picture = new JLabel(icon1);
|
34
|
+
add(picture,BorderLayout.CENTER);
|
35
|
+
|
36
|
+
JButton button = new JButton("愛でる");
|
37
|
+
ImageIcon icon4 = new ImageIcon("C:/イカ愛で/img4.png");
|
38
|
+
button.addActionListener(new LoveActionListener(picture, 10, icon4));
|
39
|
+
add(button,BorderLayout.SOUTH);
|
40
|
+
}
|
41
|
+
static class LoveActionListener implements ActionListener {
|
42
|
+
private JLabel label;
|
43
|
+
private int countMax;
|
44
|
+
private Icon icon;
|
45
|
+
private int count = 0;
|
46
|
+
LoveActionListener(JLabel label, int countMax, Icon icon) {
|
47
|
+
this.label = label;
|
48
|
+
this.countMax = countMax;
|
49
|
+
this.icon = icon;
|
50
|
+
}
|
51
|
+
public void actionPerformed(ActionEvent e) {
|
52
|
+
if(++count >= countMax) {
|
53
|
+
label.setIcon(icon);
|
54
|
+
}
|
55
|
+
}
|
56
|
+
}
|
57
|
+
}
|
58
|
+
```
|