gpt4 book ai didi

java - JFrame 显示空面板

转载 作者:行者123 更新时间:2023-12-01 14:06:40 24 4
gpt4 key购买 nike

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

}

再次感谢您的帮助。

最佳答案

尝试:

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

关于java - JFrame 显示空面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18846845/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com