0

JWindow をメイン メニューの JPanel から切り離すことにしましたが、現在は JPanel が表示されていません。JWindow クラス (mainWindow) を呼び出してパネルを追加しようとすると、ボタンを 2 回クリックする必要があります。最初は単に空の JWindow を作成するだけです。パネルを切り替えたいだけです。ボタンに目的の効果を得る方法についてのアイデアはありますか? どんな助けでも大歓迎です。

これが私のメインメニューパネルクラスです:

import java.awt.BorderLayout;
import java.awt.Window;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JWindow;
import javax.swing.JLabel;

public class Database{
    JComponent DatabasePanel() throws IOException {

        // GridBagLayout/Constraint
        GridBagConstraints gridbagcons= new GridBagConstraints();
        gridbagcons.insets = new Insets(15,15,15,15);


        //final String name = window.getName();

        final JComponent window = new JLabel(new ImageIcon(ImageIO.read(new File("res/FinalBG.png")))); 
        window .setLayout(new GridBagLayout());

        BufferedImage buttonIcon = ImageIO.read(new File("res/PlayGame.png"));
        JButton button = new JButton ("", new ImageIcon(buttonIcon));

        //Play Game button Action Listener

        button.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                try{

                    panel.setVisible(false);
                    Database = new Database ();
                    JComponent Database = Databasepanel.Database ();
                    Database.setVisible(true);

                    }   catch(IOException e1){
                    e1.printStackTrace();
                }
            }
        });

        BufferedImage buttonIcon2 = ImageIO.read(new File("res/Scoreboard.png"));
        JButton buttonTwo = new JButton ("", new ImageIcon(buttonIcon2));

        BufferedImage buttonIcon3 = ImageIO.read(new File("res/Instructions.png"));
        JButton buttonThree = new JButton ("",new ImageIcon(buttonIcon3));

        // Instructions button ActionListener
        buttonThree.addActionListener(new ActionListener(){
                    @Override
                    public void actionPerformed(ActionEvent e){
                                }

                });

        BufferedImage buttonIcon1b = ImageIO.read(new File("res/PlayGameHigh.png"));
        button.setRolloverIcon(new ImageIcon(buttonIcon1b));

        BufferedImage buttonIcon2b = ImageIO.read(new File("res/ScoreboardHigh.png"));
        buttonTwo.setRolloverIcon(new ImageIcon(buttonIcon2b));

        BufferedImage buttonIcon3b = ImageIO.read(new File("res/InstructionsHigh.png"));
        buttonThree.setRolloverIcon(new ImageIcon(buttonIcon3b));

        // Setting up GridBagConstraints for each JButton
        gridbagcons.weightx=1;
        gridbagcons.weighty=0;
        gridbagcons.gridx=0;
        gridbagcons.gridy=0;
        gridbagcons.anchor = GridBagConstraints.CENTER;

        panel.add(button, gridbagcons); //PLAY GAME

        gridbagcons.weightx=1;
        gridbagcons.weighty=0;
        gridbagcons.anchor = GridBagConstraints.CENTER;
        gridbagcons.gridx=0;
        gridbagcons.gridy=1;

        panel.add(buttonTwo,gridbagcons); //SCOREBOARD

        gridbagcons.weightx=1;
        gridbagcons.weighty=0;
        gridbagcons.anchor = GridBagConstraints.CENTER;
        gridbagcons.gridx=0;
        gridbagcons.gridy=2;

        panel.add(buttonThree,gridbagcons); //INSTRUCTIONS
        // JButton icon details 
        button.setBorder(BorderFactory.createEmptyBorder());
        button.setContentAreaFilled(false);


        buttonTwo.setBorder(BorderFactory.createEmptyBorder());
        buttonTwo.setContentAreaFilled(false);


        buttonThree.setBorder(BorderFactory.createEmptyBorder());
        buttonThree.setContentAreaFilled(false);

        return panel;
        }
}

JWindow クラス:

import java.awt.BorderLayout;
import java.awt.Window;
import java.io.IOException;

import javax.swing.JComponent;
import javax.swing.JWindow;
import javax.swing.JPanel;

public class mainWindow {
         public static void main(String[] args) throws IOException {

                windowmain gui = new windowmain ();
                gui.window();
            } 

         public JWindow Window() throws IOException {

             MainWindow passme = new MainMenu();
             JComponent panel2 = passme.mainPanel();

             JWindow window = new JWindow("Lohn Jocke and the Quest for Qualia"); //Had to set window to final for the action listener
             window.add(panel2);
             window.getContentPane().add(BorderLayout.CENTER, panel2 );
             window.setSize(860,500);
             window.setLocationRelativeTo(null);
             window.setDefaultCloseOperation(JWindow.EXIT_ON_CLOSE);
             window.setResizable(false);
             window.setVisible(true);

             return window;
         }

}

このクラスには、最初のクラスで切り替えたいパネルが含まれています。

import java.awt.BorderLayout;
import java.awt.Window;
import java.io.IOException;

import javax.swing.JComponent;
import javax.swing.JWindow;
import javax.swing.JPanel;


public class thewindow {
         public static void main(String[] args) throws IOException {

                thewindow gui = new thewindow ();
                gui.Window();
            } 

         public JWindow Window() throws IOException {

             MainMenu passme = new MainMenu();
             JComponent window = return.mainPanel();

             JWindow window = new JWindow("Lohn Jocke and the Quest for Qualia"); //Had to set window to final for the action listener
             window.add(panel2);
             window.getContentPane().add(BorderLayout.CENTER, panel2 );
             window.setSize(860,500);
             window.setLocationRelativeTo(null);
             window.setDefaultCloseOperation(JWindow.EXIT_ON_CLOSE);
             window.setResizable(false);
             window.setVisible(true);

             return window;
         }

}

繰り返しますが、助けてくれてありがとう。

4

1 に答える 1