回答編集履歴

6

追記

2018/08/20 17:42

投稿

umyu
umyu

スコア5846

test CHANGED
@@ -22,17 +22,23 @@
22
22
 
23
23
  // 前略
24
24
 
25
- b1.setActionCommand("図1");
25
+ // 変数b1~b8を配列化
26
26
 
27
- b2.setActionCommand("図2");
27
+ final JButton[] Buttons = new JButton[] { new JButton("好況"), new JButton("不況"), new JButton("正の外部性"),
28
28
 
29
- b3.setActionCommand("図3");
29
+ new JButton("負の外部性"), new JButton("好 況"), new JButton("不 況"), new JButton("大不況"), new JButton("成長戦略") };
30
30
 
31
- b4.setActionCommand("図4");
31
+ final String[] graphs = { "図1", "図2", "図1", "図1", "図1", "図2", "図3", "図4" };
32
32
 
33
- for(JButton btn:new JButton[] {b1, b2, b3, b4}) {
33
+ for (int i = 0; i < Buttons.length; i++) {
34
34
 
35
+ final JButton button = Buttons[i];
36
+
37
+ button.setActionCommand(graphs[i]);
38
+
35
- btn.addActionListener(this);
39
+ button.addActionListener(this);
40
+
41
+ panel.add(button);
36
42
 
37
43
  }
38
44
 

5

括弧は不要なので削除。

2018/08/20 17:42

投稿

umyu
umyu

スコア5846

test CHANGED
@@ -5,6 +5,10 @@
5
5
  ```Java
6
6
 
7
7
  import java.awt.CardLayout;
8
+
9
+ import java.awt.event.ActionEvent;
10
+
11
+ import java.awt.event.ActionListener;
8
12
 
9
13
 
10
14
 
@@ -58,7 +62,7 @@
58
62
 
59
63
  System.out.println(e.getActionCommand());
60
64
 
61
- final CardLayout card = (CardLayout) (graphPanel.getLayout());
65
+ final CardLayout card = (CardLayout) graphPanel.getLayout();
62
66
 
63
67
  card.show(graphPanel, e.getActionCommand());
64
68
 

4

閉じブラケットを追加。

2018/08/17 19:23

投稿

umyu
umyu

スコア5846

test CHANGED
@@ -64,6 +64,8 @@
64
64
 
65
65
  }
66
66
 
67
+ }
68
+
67
69
  ```
68
70
 
69
71
 

3

追記

2018/08/17 18:53

投稿

umyu
umyu

スコア5846

test CHANGED
@@ -44,6 +44,8 @@
44
44
 
45
45
  // 親フレームにパネルを追加。
46
46
 
47
+ // ※この部分は質問のコードを全部読んでないので、違うかもです。
48
+
47
49
  add(graphPanel);
48
50
 
49
51
  }

2

変数名をimagePanelから具体的なgraphPanelに、コンパイルエラーが発生するので、修正。

2018/08/17 18:52

投稿

umyu
umyu

スコア5846

test CHANGED
@@ -10,37 +10,57 @@
10
10
 
11
11
  public class Haikei extends JFrame implements ActionListener {
12
12
 
13
- // 間違って代入されないようにfinalで宣言する。
13
+ // 間違って代入されないようにfinalで宣言する。
14
14
 
15
- private final CardLayout cardLayout = new CardLayout();
15
+ private final JPanel graphPanel = new JPanel(new CardLayout());
16
16
 
17
- Haikei(){
17
+ Haikei(){
18
18
 
19
- // 前略
19
+ // 前略
20
20
 
21
- JPanel imagePanel = new JPanel();
21
+ b1.setActionCommand("図1");
22
22
 
23
- imagePanel.setLayout(cardLayout);
23
+ b2.setActionCommand("図2");
24
24
 
25
- cardLayout.add(label1, "図1");
25
+ b3.setActionCommand("図3");
26
26
 
27
- // 4ボタンなのに2が質問文のコードにはないのですが。その点問題ないでしょうか?
27
+ b4.setActionCommand("4");
28
28
 
29
- cardLayout.add(label3, "図3");
29
+ for(JButton btn:new JButton[] {b1, b2, b3, b4}) {
30
30
 
31
- cardLayout.add(label4, "図4");
31
+ btn.addActionListener(this);
32
32
 
33
- }
33
+ }
34
34
 
35
- // オーバーライドアノテーションを付け
35
+ // 第二引数の文字列はJButton#setActionCommandの文字列と一致す必要があります。
36
36
 
37
- @Override
37
+ graphPanel.add(label1, "図1");
38
38
 
39
- public void actionPerformed(ActionEvent e){
39
+ // 4ボタンなのに図2が質問文のコードにはないのですが。その点問題ないでしょうか?
40
40
 
41
- cardLayout.show(cardPanel, e.getActionCommand());
41
+ graphPanel.add(label3, "図3");
42
42
 
43
+ graphPanel.add(label4, "図4");
44
+
45
+ // 親フレームにパネルを追加。
46
+
47
+ add(graphPanel);
48
+
43
- }
49
+ }
50
+
51
+ // オーバーライドアノテーションを付ける
52
+
53
+ @Override
54
+
55
+ public void actionPerformed(ActionEvent e) {
56
+
57
+ System.out.println(e.getActionCommand());
58
+
59
+ final CardLayout card = (CardLayout) (graphPanel.getLayout());
60
+
61
+ card.show(graphPanel, e.getActionCommand());
62
+
63
+ }
44
64
 
45
65
  ```
46
66
 

1

追記

2018/08/17 18:49

投稿

umyu
umyu

スコア5846

test CHANGED
@@ -4,21 +4,43 @@
4
4
 
5
5
  ```Java
6
6
 
7
- JPanel imagePanel = new JPanel();
8
-
9
- CardLayout cardLayout = new CardLayout();
10
-
11
- imagePanel.setLayout(cardLayout);
7
+ import java.awt.CardLayout;
12
8
 
13
9
 
14
10
 
15
- cardLayout.add(label1, "図1");
11
+ public class Haikei extends JFrame implements ActionListener {
16
12
 
17
- // 4ボタン図2が質問文のコードにはないのですその点問題ないでしょうか?
13
+ // 間違って代入されいようfinal宣言
18
14
 
19
- cardLayout.add(label3, "図3");
15
+ private final CardLayout cardLayout = new CardLayout();
20
16
 
17
+ Haikei(){
18
+
19
+ // 前略
20
+
21
+ JPanel imagePanel = new JPanel();
22
+
23
+ imagePanel.setLayout(cardLayout);
24
+
25
+ cardLayout.add(label1, "図1");
26
+
27
+ // 4ボタンなのに図2が質問文のコードにはないのですが。その点問題ないでしょうか?
28
+
29
+ cardLayout.add(label3, "図3");
30
+
21
- cardLayout.add(label4, "図4");
31
+ cardLayout.add(label4, "図4");
32
+
33
+ }
34
+
35
+ // オーバーライドアノテーションを付ける
36
+
37
+ @Override
38
+
39
+ public void actionPerformed(ActionEvent e){
40
+
41
+ cardLayout.show(cardPanel, e.getActionCommand());
42
+
43
+ }
22
44
 
23
45
  ```
24
46