回答編集履歴
2
extends JFrame について追記
test
CHANGED
@@ -3,6 +3,8 @@
|
|
3
3
|
|
4
4
|
|
5
5
|
他にも...
|
6
|
+
|
7
|
+
Button クラスは JFrame としては機能させていませんので extends JFrame は無駄です.
|
6
8
|
|
7
9
|
Button クラスのコンストラクタでのみ count を加算していては何時までたっても 10 にはなりません.
|
8
10
|
|
1
コード等追加
test
CHANGED
@@ -1 +1,115 @@
|
|
1
1
|
ActionListener を実装している Button のインスタンス a が, JButton のインスタンス button に登録されていないようです.
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
他にも...
|
6
|
+
|
7
|
+
Button クラスのコンストラクタでのみ count を加算していては何時までたっても 10 にはなりません.
|
8
|
+
|
9
|
+
同じくコンストラクタで, img1.png のラベルを生成しても使われておりません.
|
10
|
+
|
11
|
+
actionPerformed 内で, ```Container contentPane=new Container();``` としても, JFrame の contentPane とは無関係ですので, 何の操作をしても意味はありません.
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
```java
|
16
|
+
|
17
|
+
import java.awt.BorderLayout;
|
18
|
+
|
19
|
+
import java.awt.event.ActionEvent;
|
20
|
+
|
21
|
+
import java.awt.event.ActionListener;
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
import javax.swing.Icon;
|
26
|
+
|
27
|
+
import javax.swing.ImageIcon;
|
28
|
+
|
29
|
+
import javax.swing.JButton;
|
30
|
+
|
31
|
+
import javax.swing.JFrame;
|
32
|
+
|
33
|
+
import javax.swing.JLabel;
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
public class LoveSquid extends JFrame {
|
38
|
+
|
39
|
+
public static void main(String[] args) {
|
40
|
+
|
41
|
+
new LoveSquid().setVisible(true);
|
42
|
+
|
43
|
+
}
|
44
|
+
|
45
|
+
LoveSquid() {
|
46
|
+
|
47
|
+
super("イカちゃん");
|
48
|
+
|
49
|
+
setSize(700,460);
|
50
|
+
|
51
|
+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
JLabel label = new JLabel("イカちゃんを愛でよう!あ、拡大はしないで欲しいでゲソ");
|
56
|
+
|
57
|
+
label.setHorizontalAlignment(JLabel.CENTER);
|
58
|
+
|
59
|
+
add(label,BorderLayout.NORTH);
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
ImageIcon icon1 = new ImageIcon("C:/イカ愛で/img1.png");
|
64
|
+
|
65
|
+
JLabel picture = new JLabel(icon1);
|
66
|
+
|
67
|
+
add(picture,BorderLayout.CENTER);
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
JButton button = new JButton("愛でる");
|
72
|
+
|
73
|
+
ImageIcon icon4 = new ImageIcon("C:/イカ愛で/img4.png");
|
74
|
+
|
75
|
+
button.addActionListener(new LoveActionListener(picture, 10, icon4));
|
76
|
+
|
77
|
+
add(button,BorderLayout.SOUTH);
|
78
|
+
|
79
|
+
}
|
80
|
+
|
81
|
+
static class LoveActionListener implements ActionListener {
|
82
|
+
|
83
|
+
private JLabel label;
|
84
|
+
|
85
|
+
private int countMax;
|
86
|
+
|
87
|
+
private Icon icon;
|
88
|
+
|
89
|
+
private int count = 0;
|
90
|
+
|
91
|
+
LoveActionListener(JLabel label, int countMax, Icon icon) {
|
92
|
+
|
93
|
+
this.label = label;
|
94
|
+
|
95
|
+
this.countMax = countMax;
|
96
|
+
|
97
|
+
this.icon = icon;
|
98
|
+
|
99
|
+
}
|
100
|
+
|
101
|
+
public void actionPerformed(ActionEvent e) {
|
102
|
+
|
103
|
+
if(++count >= countMax) {
|
104
|
+
|
105
|
+
label.setIcon(icon);
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
}
|
110
|
+
|
111
|
+
}
|
112
|
+
|
113
|
+
}
|
114
|
+
|
115
|
+
```
|