我有一个扩展JFrame的类,通过在BoxLayout按钮中添加2个面板,在中心添加一个JTabbedPane来显示图形。
我希望其中一个按钮删除框架中的所有当前组件并添加新组件。
以下是使用的方法。
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方法,因为" Made it to Intro"字符串输出到终端。
我已经在标签和框架本身上尝试过invalidate(),validate(),revalidate(),repaint()的各种组合。真的很沮丧,因为我不知道我是怎么能够显示3个不同的屏幕来来回切换,而实际上只是一次显示一个。
感谢您的时间。