(Java GUI)JPanel未显示在removeAll()方法之后通过方法返回
下面,我创建了一个JFrame类(只在下面的类中发布了重要的部分)。在创建UserInterface对象时,除了激活eventListener之外,其他一切都可以正常工作。本节代码:(Java GUI)JPanel未显示在removeAll()方法之后通过方法返回,java,swing,jpanel,Java,Swing,Jpanel,下面,我创建了一个JFrame类(只在下面的类中发布了重要的部分)。在创建UserInterface对象时,除了激活eventListener之外,其他一切都可以正常工作。本节代码: getContentPane().removeAll(); getContentPane().add(gameScreen()); getContentPane().validate(); getContentPane().repaint(); 似乎没有达到预期的效果。removeAll()方法起作用,但将gam
getContentPane().removeAll();
getContentPane().add(gameScreen());
getContentPane().validate();
getContentPane().repaint();
似乎没有达到预期的效果。removeAll()方法起作用,但将gameScreen方法(返回面板)添加到JFrame中不会起任何作用。知道为什么吗
public class UserInterface extends JFrame{
public UserInterface(){
super("idk");
setName("idk");
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBackground(Color.WHITE);
setResizable(false);
setLocationRelativeTo(null);
add(startScreen());
setVisible(true);
}//UserInterface
private class beginButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
getContentPane().removeAll();
getContentPane().add(gameScreen());
getContentPane().validate();
getContentPane().repaint();
}
}//beginButtonListener
public JPanel gameScreen(){
JPanel gamePanel = new JPanel();
gamePanel.setName("gamePanel");
gamePanel.setLayout(null);
JLabel fadeShade = new JLabel();
fadeShade.setName("fadeShade");
fadeShade.setBackground(Color.BLUE);
fadeShade.setBounds(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
gamePanel.add(fadeShade);
return gamePanel;
}//gameScreen
}//UserInterface
是的,你的游戏面板可能会被添加,但你永远不会看到它。在标签上不添加任何文本的情况下向其添加JLabel,并在不使标签不透明的情况下设置标签的背景色。您还使用空布局,这是一件非常危险的事情 如果这是我的密码,我会
- 使用CardLayout交换组件
- 避免像瘟疫一样的
布局,而是让布局管理员为我做繁重的布局提升null
- 确保设置任何需要显示文本的JLabel的文本。设置JLabel的name属性可能对我帮助不大
- 使用CardLayout交换组件
- 避免像瘟疫一样的
布局,而是让布局管理员为我做繁重的布局提升null
- 确保设置任何需要显示文本的JLabel的文本。设置JLabel的name属性可能对我帮助不大