gpt4 book ai didi

java - 为什么卡片没有出现在 JPanel CardLayout 上?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:51:25 26 4
gpt4 key购买 nike

我的应用程序包含两个 JPanel,它们将成为 MainPanel 上的卡片。代码如下。由于某种原因,面板没有出现。非常感谢您的帮助:)

主面板.java:

public class MainPanel extends JPanel{

private final static String PANEL1 = "PANEL1";
private final static String PANEL2 = "PANEL2";

private static Panel1 panel1;
private static Panel2 panel2;

//private static CardLayout layout;

public MainPanel() {

super(new CardLayout());

panel1 = new Panel1();
panel2 = new Panel2();

getLayout().addLayoutComponent(PANEL1, panel1);
getLayout().addLayoutComponent(PANEL2, panel2);


((CardLayout) getLayout()).show(this, PANEL1);
}

}

主.java:

public class Main {

private static JFrame window;

public static void main(String[] args) {

window = new JFrame();

window.setContentPane(new MainPanel());
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(600,400);
window.setVisible(true);
}

}

最佳答案

这个

getLayout().addLayoutComponent(PANEL1, panel1);
getLayout().addLayoutComponent(PANEL2, panel2);

应该是

add(panel1, PANEL1);
add(panel2, PANEL2);

有关详细信息,请参阅 How to Use CardLayout .

关于java - 为什么卡片没有出现在 JPanel CardLayout 上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12183658/

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