首先...我想说我对此使用卡片布局不感兴趣...除非有必要(这意味着不使用卡片布局将导致不必要的解决方法和复杂的问题)代码)。毕竟这是出于学习目的,无论如何我很快就会研究卡片布局......
好吧,我想我的问题是非常基本的 GUI 布局。我的代码无法正常工作,整个布局让我很困惑......
我无法像这样在 JPanel 之间进行转换:
我有一个窗口。我按下一个按钮,旧窗口被另一个窗口取代。我按下一个按钮,该窗口将被另一个窗口取代。
我想补充一点,我在下面的示例中跳过了很多不相关的代码...
我从 JFrame 开始:
public class StartWindow extends JFrame{
public StartWindow(){
//Add JButton & ActionListener
//When the button is pressed:
add(new NextWindow());
}
public static void main(String [] args){
new StartWindow();
}
}
然后我有几个 JPanel...
public class NextWindow extends JPanel{
public NextWindow(){
//Add a JButton & ActionListener
//When the button is pressed:
add(new NextWindow2());
remove(this);
//This does not work. Nothing happens.
}
}
public class NextWindow2 extends JPanel{ // Stuff and so on}
所以,我想知道处理这种情况的正确方法!
最佳答案
您正在向其自身添加一个面板。您需要从 JFrame 中删除面板,向其中添加新面板,然后在 JFrame 上调用 revalidate()。
关于java - JPanel 之间的转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20411783/