【问题标题】:JFrame displaying empty panelJFrame 显示空面板
【发布时间】:2013-09-17 10:02:08
【问题描述】:

我决定尝试将我的 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;
         }

}

再次感谢您的帮助。

【问题讨论】:

  • 为什么不使用CardLayout,这个东西?一个相关的example :-) 此外,为什么要使用类似final JComponent panel = new JLabel(...) 的东西,使代码难以阅读,创建一个JLabel 并将其称为panel :-)
  • 有人告诉我 GridBagLayout 是最通用的,所以我开始学习它,但我现在肯定会研究 CardLayout 并更恰当地命名我的变量,谢谢!
  • 如需尽快获得更好的帮助,请发帖SSCCE
  • @Icy100 : GridBagLayout 无疑是Swing 提供的最好的Layout Manager 之一。您可以使用Nested Layout,从某种意义上说,基地将使用CardLayout,它只会用前一个View替换新的View,尽管每个View都有自己的Layout,就像GridBagLayout一样显示它的内容。

标签: java swing jpanel


【解决方案1】:

试试:

frame.getContentPane().add(panel2, BorderLayout.CENTER);

【讨论】:

  • 将它添加到我的框架类中,仍然一无所获:/.. 不过还是谢谢你!
  • 从JDK5开始就不需要使用getContentPane()了。框架的 add 方法会将组件添加到内容窗格中。
猜你喜欢
  • 2019-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多