###前提・実現したいこと
(例)Java(UIはJFrame)でゲームを作っているのですが、JPanelの切り替えをJFrameで行おうとしています。
しかし、JPanelの切り替えがうまくいきません。
なお、切り替えにはこのサイトのような切り替え方法を使っています。
###発生している問題
切り替え自体はどうやら成功しているようだが、パネルのボタンの表示がされない
###該当のソースコード
※作成中のゲームのため、一部関数を改変し、コメントはすべて除去した
Java
package panels; import java.awt.BorderLayout; import java.awt.Container; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; public class MainFrame extends JFrame{ public String[] PanelNames = {"First","Second","Third"}; FirstPanel fp = new FirstPanel(PanelNames[0]); SecondPanel sp = new SecondPanel(PanelNames[1]); ThirdPanel tp = new ThirdPanel(PanelNames[2]); public JPanel[] jps = {fp,sp,tp}; int i; private static final int VERTICAL = 768; private static final int HORIZONTAL = 1024; public MainFrame(){ super("Test"); this.add(fp);fp.setVisible(true); this.add(sp);sp.setVisible(false); this.add(tp);tp.setVisible(false); setBounds(100, 100, 300, 250); setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); addWindowListener(new WindowClosing()); setSize(HORIZONTAL,VERTICAL); setResizable(false); Container contentPane = getContentPane(); contentPane.add(jps[i], BorderLayout.CENTER); setVisible(true); } class WindowClosing extends WindowAdapter{ public void windowClosing(WindowEvent e) { int ans = JOptionPane.showConfirmDialog (MainFrame.this, "本当に終了しますか?"); if(ans == JOptionPane.YES_OPTION) { System.exit(0); } } } public void changeKey(JPanel jp, String str){ System.out.println(jp.getName()); String name = jp.getName(); if (name==PanelNames[0]){ fp = (FirstPanel)jp; fp.setVisible(false); System.out.println("〇"); } else if (name==PanelNames[1]){ sp = (SecondPanel)jp; sp.setVisible(false); System.out.println("●"); } else if (name==PanelNames[2]){ tp = (ThirdPanel)jp; tp.setVisible(false); System.out.println("◎"); } else { System.out.println("huh?"); } if (str==PanelNames[0]){ fp.setVisible(true); //iで切り替え! i = 0; System.out.println("△"); } else if (str==PanelNames[1]){ sp.setVisible(true); i = 1; System.out.println("▼"); } else if (str==PanelNames[2]){ tp.setVisible(true); i = 2; System.out.println("▲"); } else { System.out.println("huh?"); } } } package panels; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JPanel; package panels; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; public class FirstPanel extends JPanel implements ActionListener{ String str; public FirstPanel(String s){ str = s; this.setName("First"); //setLocationRelativeTo(null); setLayout(null); JLabel titleLabel = new JLabel("FirstPanel"); titleLabel.setBounds(270, 100, 512, 100); titleLabel.setFont(new Font("MS ゴシック", Font.BOLD, 64)); titleLabel.setHorizontalTextPosition(JLabel.CENTER); JButton buttonOne = new JButton("One"); buttonOne.setBounds(256, 350, 512, 50); buttonOne.addActionListener(this); buttonOne.setActionCommand("One"); JButton buttonTwo = new JButton("Two"); buttonTwo.setBounds(256, 400, 512, 50); buttonTwo.addActionListener(this); buttonTwo.setActionCommand("Two"); JButton buttonThree = new JButton("Three"); buttonThree.setBounds(256, 4050, 512, 50); buttonThree.addActionListener(this); buttonThree.setActionCommand("Three"); JButton buttonFour = new JButton("Four"); buttonFour.setBounds(256, 500, 512, 50); buttonFour.addActionListener(this); buttonFour.setActionCommand("Four"); JButton buttonFive = new JButton("Five"); buttonFive.setBounds(256, 550, 512, 50); buttonFive.addActionListener(this); buttonFive.setActionCommand("Five"); add(titleLabel); add(buttonOne); add(buttonTwo); add(buttonThree); add(buttonFour); add(buttonFive); } @Override public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); System.out.println(cmd); switch(cmd){ case "One": break; case "Two": break; case "Three": break; case "Four": break; case "Five": System.exit(0); break; default: //何もない場合はreturnで切る return; } } } ※SecondPanel、ThirdPanelはこれと同じなので省略
###試したこと
- SecondPanelのみをMainにして起動→動作する
- MainFrameのFirstPanelをfalseにしてSecondPanelをtrueにする→動作しない(ThirdPanelでも動作しない)
- 切り替え時にコンソール上に文字を表記→切り替え時の文字は表記される
- FirstPanelからFirstPanelに転送→表示される
###補足情報(言語/FW/ツール等のバージョンなど)
Java8(Eclipse)
まだ回答がついていません
会員登録して回答してみよう