Java JFrame不会加载新屏幕
我有一个扩展JFrame的类,它通过添加两个带有BoxLayout按钮的面板和一个在中间显示图形的JTabbedPane来工作 我希望其中一个按钮删除框架中的所有当前组件并添加新组件 以下是使用的方法Java JFrame不会加载新屏幕,java,swing,validation,class,jframe,Java,Swing,Validation,Class,Jframe,我有一个扩展JFrame的类,它通过添加两个带有BoxLayout按钮的面板和一个在中间显示图形的JTabbedPane来工作 我希望其中一个按钮删除框架中的所有当前组件并添加新组件 以下是使用的方法 private void createAndShowGraphs() { ImageIcon createImageIcon(lsuLettersPath); //simple png file to fill one tab final JTabbedPane jtp = ne
private void createAndShowGraphs() {
ImageIcon createImageIcon(lsuLettersPath); //simple png file to fill one tab
final JTabbedPane jtp = new JTabbedPane();
JLabel iconLabel = new JLabel();
iconLabel.setOpaque(true);
jtp.addTab(null, icon, iconLabel);
//Here is where the errors begin
JPanel menu = new JPanel();
menu.setLayout(new BoxLayout(menu, BoxLayout.Y_AXIS));
//I want this button to remove all components currently in the JFrame and replace them with new components specified in the createAndShowIntro() method
menu.add(new JButton(new AbstractAction("Intro Pane") {
public void actionPerformed(ActionEvent e) {
//I've also tried putting removeAll in the Intro method
removeAll();
createAndShowIntro();
}
}));
add(jtp, BorderLayout.CENTER);
add(menu, BorderLayout.WEST);
pack();
setVisible(true);
}
private void createAndShowIntro() {
System.out.println("Made it to Intro");
//all I want is a blank JLabel with the String "test" to show up
JPanel test = new JPanel();
test.setLayout(new BorderLayout());
JLabel label = new JLabel();
label.setText("test");
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setVerticalAlignment(SwingConstants.CENTER);
test.add(label);
add(test, BorderLayout.CENTER);
test.revalidate();
label.revalidate();
validate();
test.repaint();
label.repaint();
repaint();
pack();
setVisible(true);
}
当我在main()中调用createAndShowGraphs()并点击“Intro”按钮时,所有内容都会冻结,实际上没有任何内容被删除。我知道它之所以成为Intro方法是因为“madeittointro”字符串输出到终端
我在标签和框架本身上尝试了各种组合,包括invalidate()、validate()、revalidate()、repaint()。真的很沮丧,因为我不知道如何才能显示3个不同的屏幕来来回切换,而实际上一次只能显示一个
谢谢您的时间。不要在
JFrame
上调用removeAll
,它会删除包含所有其他内容的JRootPane
。改用getContentPane#removeAll
。请查看以了解更多详细信息W。我很难说我为此挣扎了多久。这正好回答了我的问题。非常感谢你!