teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

6

追記

2018/08/20 17:42

投稿

umyu
umyu

スコア5846

answer CHANGED
@@ -10,12 +10,15 @@
10
10
  private final JPanel graphPanel = new JPanel(new CardLayout());
11
11
  Haikei(){
12
12
  // 前略
13
+ // 変数b1~b8を配列化
14
+ final JButton[] Buttons = new JButton[] { new JButton("好況"), new JButton("不況"), new JButton("正の外部性"),
15
+ new JButton("負の外部性"), new JButton("好 況"), new JButton("不 況"), new JButton("大不況"), new JButton("成長戦略") };
16
+ final String[] graphs = { "図1", "図2", "図1", "図1", "図1", "図2", "図3", "図4" };
17
+ for (int i = 0; i < Buttons.length; i++) {
18
+ final JButton button = Buttons[i];
13
- b1.setActionCommand("図1");
19
+ button.setActionCommand(graphs[i]);
14
- b2.setActionCommand("図2");
15
- b3.setActionCommand("図3");
16
- b4.setActionCommand("図4");
17
- for(JButton btn:new JButton[] {b1, b2, b3, b4}) {
18
- btn.addActionListener(this);
20
+ button.addActionListener(this);
21
+ panel.add(button);
19
22
  }
20
23
  // 第二引数の文字列はJButton#setActionCommandの文字列と一致する必要があります。
21
24
  graphPanel.add(label1, "図1");

5

括弧は不要なので削除。

2018/08/20 17:42

投稿

umyu
umyu

スコア5846

answer CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  ```Java
4
4
  import java.awt.CardLayout;
5
+ import java.awt.event.ActionEvent;
6
+ import java.awt.event.ActionListener;
5
7
 
6
8
  public class Haikei extends JFrame implements ActionListener {
7
9
  // 間違って代入されないようにfinalで宣言する。
@@ -28,7 +30,7 @@
28
30
  @Override
29
31
  public void actionPerformed(ActionEvent e) {
30
32
  System.out.println(e.getActionCommand());
31
- final CardLayout card = (CardLayout) (graphPanel.getLayout());
33
+ final CardLayout card = (CardLayout) graphPanel.getLayout();
32
34
  card.show(graphPanel, e.getActionCommand());
33
35
  }
34
36
  }

4

閉じブラケットを追加。

2018/08/17 19:23

投稿

umyu
umyu

スコア5846

answer CHANGED
@@ -31,6 +31,7 @@
31
31
  final CardLayout card = (CardLayout) (graphPanel.getLayout());
32
32
  card.show(graphPanel, e.getActionCommand());
33
33
  }
34
+ }
34
35
  ```
35
36
 
36
37
  ---

3

追記

2018/08/17 18:53

投稿

umyu
umyu

スコア5846

answer CHANGED
@@ -21,6 +21,7 @@
21
21
  graphPanel.add(label3, "図3");
22
22
  graphPanel.add(label4, "図4");
23
23
  // 親フレームにパネルを追加。
24
+ // ※この部分は質問のコードを全部読んでないので、違うかもです。
24
25
  add(graphPanel);
25
26
  }
26
27
  // オーバーライドアノテーションを付ける

2

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

2018/08/17 18:52

投稿

umyu
umyu

スコア5846

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

1

追記

2018/08/17 18:49

投稿

umyu
umyu

スコア5846

answer CHANGED
@@ -1,14 +1,25 @@
1
1
  切り替えるには[CardLayout](https://www.javadrive.jp/tutorial/cardlayout/index3.html)とActionListenerがと使えるかと。
2
2
 
3
3
  ```Java
4
- JPanel imagePanel = new JPanel();
5
- CardLayout cardLayout = new CardLayout();
6
- imagePanel.setLayout(cardLayout);
4
+ import java.awt.CardLayout;
7
5
 
6
+ public class Haikei extends JFrame implements ActionListener {
7
+ // 間違って代入されないようにfinalで宣言する。
8
+ private final CardLayout cardLayout = new CardLayout();
9
+ Haikei(){
10
+ // 前略
11
+ JPanel imagePanel = new JPanel();
12
+ imagePanel.setLayout(cardLayout);
8
- cardLayout.add(label1, "図1");
13
+ cardLayout.add(label1, "図1");
9
- // 4ボタンなのに図2が質問文のコードにはないのですが。その点問題ないでしょうか?
14
+ // 4ボタンなのに図2が質問文のコードにはないのですが。その点問題ないでしょうか?
10
- cardLayout.add(label3, "図3");
15
+ cardLayout.add(label3, "図3");
11
- cardLayout.add(label4, "図4");
16
+ cardLayout.add(label4, "図4");
17
+ }
18
+ // オーバーライドアノテーションを付ける
19
+ @Override
20
+ public void actionPerformed(ActionEvent e){
21
+ cardLayout.show(cardPanel, e.getActionCommand());
22
+ }
12
23
  ```
13
24
 
14
25
  ---