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

質問編集履歴

2

手元のコードを入力しました

2020/02/05 01:36

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -12,6 +12,88 @@
12
12
  ゲーム画面に画面遷移したいです。
13
13
  その時ゲーム画面にはボタンを非表示に指定したいと思っています。
14
14
 
15
+
16
+
17
+
18
+
19
+ 手元のコード
20
+
21
+ import javax.swing.*;
22
+ import java.awt.CardLayout;
23
+ import java.awt.BorderLayout;
24
+ import java.awt.event.*;
25
+
26
+ public class CardLayoutTest3 extends JFrame implements ActionListener{
27
+
28
+ JPanel cardPanel;
29
+ CardLayout layout;
30
+
31
+ public static void main(String[] args){
32
+ CardLayoutTest3 frame = new CardLayoutTest3();
33
+
34
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
35
+ frame.setBounds(10, 10, 300, 200);
36
+ frame.setTitle("タイトル");
37
+ frame.setVisible(true);
38
+ }
39
+
40
+ CardLayoutTest3(){
41
+ /* カード1 */
42
+ JPanel card1 = new JPanel();
43
+ card1.add(new JButton("button"));
44
+
45
+ /* カード2 */
46
+ JPanel card2 = new JPanel();
47
+ card2.add(new JLabel("label"));
48
+ card2.add(new JTextField("", 10));
49
+
50
+ /* カード3 */
51
+ JPanel card3 = new JPanel();
52
+ card3.add(new JCheckBox("checkbox1"));
53
+ card3.add(new JCheckBox("checkbox2"));
54
+
55
+ cardPanel = new JPanel();
56
+ layout = new CardLayout();
57
+ cardPanel.setLayout(layout);
58
+
59
+ cardPanel.add(card1, "button");
60
+ cardPanel.add(card2, "label");
61
+ cardPanel.add(card3, "checkbox");
62
+
63
+ /* カード移動用ボタン */
64
+ JButton button1 = new JButton("button");
65
+ button1.addActionListener(this);
66
+ button1.setActionCommand("button");
67
+
68
+ JButton button2 = new JButton("label");
69
+ button2.addActionListener(this);
70
+ button2.setActionCommand("label");
71
+
72
+ JButton button3 = new JButton("checkbox");
73
+ button3.addActionListener(this);
74
+ button3.setActionCommand("checkbox");
75
+
76
+ JPanel btnPanel = new JPanel();
77
+ btnPanel.add(button1);
78
+ btnPanel.add(button2);
79
+ btnPanel.add(button3);
80
+
81
+ getContentPane().add(cardPanel, BorderLayout.CENTER);
82
+ getContentPane().add(btnPanel, BorderLayout.PAGE_END);
83
+ }
84
+
85
+ public void actionPerformed(ActionEvent e){
86
+ String cmd = e.getActionCommand();
87
+
88
+ layout.show(cardPanel, cmd);
89
+ }
90
+ }
91
+
92
+
93
+
94
+
95
+
96
+
15
97
  ↓ちなみにソースコードはこのサイトを参考にしています
16
98
  https://www.javadrive.jp/tutorial/cardlayout/index3.html
17
99
 

1

URLが間違っていたので変更したのと、「可能か」という文章を変えました。

2020/02/05 01:35

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -4,8 +4,7 @@
4
4
  javaでマリオのようなゲームを作成しようとしています。
5
5
  そこでステージ選択画面をcard layoutで作りたいのですが、
6
6
  画像の下のボタン、[button][label][checkbox]のいずれかを押したときに、
7
- 押したボタン以外のボタンを非表示にしたいですが、
7
+ 押したボタン以外のボタンを非表示にしたいです
8
- それは可能ですか?
9
8
 
10
9
  自分のやりたいイメージとしては、
11
10
  画像の[button][label][checkbox]ボタンを選ぶステージに見立てて、
@@ -14,9 +13,8 @@
14
13
  その時ゲーム画面にはボタンを非表示に指定したいと思っています。
15
14
 
16
15
  ↓ちなみにソースコードはこのサイトを参考にしています
17
- https://www.javadrive.jp/tutorial/cardlayout/index2.html
16
+ https://www.javadrive.jp/tutorial/cardlayout/index3.html
18
17
 
19
-
20
18
  ![](1758341bc9c8f263a01f92c49c178ff1.png)
21
19
 
22
20
  ![](4f9386ad07b911229af7635a5f130498.png)