Java Swing 无法向面板添加多个面板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19675854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Swing can't add multiple panels to panel
提问by B13ZT
I'm trying to add 2 panels to one panel. Now the problem is, when I add the first or the second one alone, I can see them in the panel. But when I add both off them (put //add(panel2, BorderLayout.SOUTH); out of comment) I don't see any of them. The Weblabel("Test") is always showing. Now both panels have a onclick method and when you can't see them (if they are both added) the onclick still works if you click where the panels are supposed to be.
我正在尝试将 2 个面板添加到一个面板中。现在的问题是,当我单独添加第一个或第二个时,我可以在面板中看到它们。但是当我将它们都添加时(put //add(panel2, BorderLayout.SOUTH); out of comment)我没有看到它们中的任何一个。Weblabel("Test") 始终显示。现在两个面板都有一个 onclick 方法,当你看不到它们时(如果它们都被添加了),如果你点击面板应该在的地方,onclick 仍然有效。
I already tried other layoutmanagers but without succes. Does anyone know where this problem could be comming from?
我已经尝试过其他布局管理器,但没有成功。有谁知道这个问题可能来自哪里?
setLayout(new BorderLayout());
add(panel1,BorderLayout.NORTH);
add(new WebLabel("Test"), BorderLayout.CENTER);
//add(panel2,BorderLayout.SOUTH);
I initialize both panels in my constructor.
我在构造函数中初始化了两个面板。
public MultipleFloorPlanEntityPanel(int xCoordinate, int yCoordinate, FloorPlanEntityPanel panel1, FloorPlanEntityPanel panel2){
this.xCoordinate = xCoordinate;
this.yCoordinate = yCoordinate;
this.panel1 = panel1;
this.panel2 = panel2;
layoutComponents();
}
Solved: reinitialized the extra panels and that worked :)
解决了:重新初始化了额外的面板并且有效:)
采纳答案by laksys
As you need panel contain two panel? try this
由于您需要面板包含两个面板?尝试这个
JPanel top = new JPanel(new GridLayout(1,1))
JPanel left = new JPanel();
JPanel right = new JPanel();
top.add(left);
top.add(right);
JFrame frame = new JFrame();
frame.add(top);
frame.setSize(400,400);
frame.setVisible(true);