【发布时间】:2013-08-13 06:48:48
【问题描述】:
我对 Java JPanel 有疑问。我想将 2 个具有不同布局的 JPanel 放入一个也具有布局的 JPanel 中。有没有可能让它发挥作用?
BibP()
setLayout(new GridLayout(5, 1)); //The big JPanel
add(new A(), new FlowLayout(4));
add(new B(), new GridLayout(7,2));
A 和 B 都是作为 JPanel 扩展的类,无论我更改或评论什么,B 总是出现在 1 行中。 我在 A 中添加了 4 个元素,在 B 中添加了 14 个元素(JLabels 和 JTextAreas),其中没有太多代码,只有添加和一些计算。
问题可能出在我试图放置大 JPanel 的 JFrame 中。
JFrame.this.add(new BigP(),BorderLayout.CENTER);
编辑:
public class BigP extends JPanel{
//Labels and TextAres
public class A extends JPanel{
public A(){
setBorder(new EmptyBorder(0, -50, 0, 0));
//get date and add to textareas
//add the label and textareas
}
}
public class B extends JPanel{
public B (){
setBorder(new EmptyBorder(0, -50, 0, 0));
setBackground(Color.red);
//colum longs for text areas less then 5
//add labels and textareas
}
}
public BigP(){
setLayout(new GridLayout(5, 1));
setBorder(new EmptyBorder(3,-160,0,0));
add(new A(), new FlowLayout(4));
add(new B(), new GridLayout(7,2));
}
}
感谢您的帮助。
经过几次尝试:
如果我用这个:
add(new B(), new GridLayout(7,2));
当我打印布局时,我在 B 中得到了这个:
java.awt.FlowLayout[hgap=5,vgap=5,align=center]
如果我在 B 中设置布局:
setLayout(new GridLayout(7, 2));
信息正确:
java.awt.GridLayout[hgap=0,vgap=0,rows=7,cols=2]
但是只有 2 个可见的 JTextAreas 应该是 14 个元素。
【问题讨论】:
-
代码似乎缺少关键部分。如果我在精神上完成它,那么一切看起来都很好。请确保您输入的内容准确。显然是有问题的。
-
如需尽快获得更好的帮助,请发帖SSCCE。
标签: java swing layout jpanel mixing