前提条件
ボタンを使ってシーンを切り替えをするプログラムを作っています。参考で一部ネットにあるものを使いました。切り替えのプログラムは参考と同じなのですが、シーン切り替えがうまくいきません。ご教授願えないでしょうか。
Java
1package test; 2import javax.swing.*; 3import java.awt.Graphics; 4import java.awt.Insets; 5import java.awt.Color; 6 7 8public class a{ 9static JButton button1; 10 public static void main(String[] args) throws InterruptedException{ 11 GameWindow gw = new GameWindow("テストウィンドウ",400,300); 12 gw.setVisible(true); 13 gw.change(new Scene1()); 14 Thread.sleep(1000); 15 //シーン切り替えの文章 16 button1.addActionListener(e -> { 17 gw.change(new Scene2()); 18 }); 19 } 20} 21 22class GameWindow extends JFrame{ 23 private Thread th = null; 24 25 public GameWindow(String title, int width, int height) { 26 super(title); 27 setDefaultCloseOperation(EXIT_ON_CLOSE); 28 setSize(width,height); 29 setLocationRelativeTo(null); 30 setResizable(false); 31 } 32 33 //画面切り替え用メソッド 34 public void change(JPanel panel) { 35 getContentPane().removeAll(); 36 37 super.add(panel);//画面追加 38 validate();//更新 39 repaint();//再描画 40 } 41 42} 43 44 45class Scene1 extends JPanel{ 46 public void paintComponent(Graphics g) { 47 super.paintComponent(g); 48 g.setColor(Color.WHITE); 49 g.fillRect(0,0,400,300); 50 51 JButton button1 = new JButton("View1"); 52 add(button1); 53 54 g.setColor(Color.BLACK); 55 g.drawString("タイトル画面", 170, 50); 56 } 57} 58 59 60 61class Scene2 extends JPanel{ 62 public void paintComponent(Graphics g) { 63 super.paintComponent(g); 64 g.setColor(Color.BLACK); 65 g.fillRect(0,0,400,300); 66 67 g.setColor(Color.WHITE); 68 g.drawString("ゲーム画面", 170, 50); 69 } 70} 71 72 73 74class Scene3 extends JPanel{ 75 public void paintComponent(Graphics g){ 76 super.paintComponent(g); 77 g.drawImage(); 78 g.fillRect(0,0,400,300); 79 } 80} 81 82 83 84class Scene4 extends JPanel{ 85 public void paintComponent(Graphics g){ 86 super.paintComponent(g); 87 g.drawImage(); 88 g.fillRect(0,0,400,300); 89 } 90} 91
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。