0

我决定尝试将我的 JWindow 与我的主菜单的 JPanel 分开,但现在 JPanel 没有显示。当/如果我尝试调用我的 JWindow 类 (mainWindow) 将面板添加到其中时,我必须单击按钮两次,因为它第一次只是创建一个空的 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

1

尝试 :

frame.getContentPane().add(panel2, BorderLayout.CENTER);
于 2013-09-17T10:10:33.857 回答