我遇到了Java JPanels的问题。我想把两个不同布局的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都是类扩展为JPanels,无论我更改或评论B总是出现在1行。 我有4个元素添加到A和14到B(JLabels和JTextAreas),并且它们中只有添加和计算的代码不多。
问题可能出在JFrame中,我试图放置大型JPanel。
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,对齐=中心]
如果我在B中设置布局:
setLayout(new GridLayout(7, 2));
信息是正确的:
java.awt.GridLayout中[hgap指定= 0,vgap = 0,行= 7,COLS = 2]
但是只有2个JTextAreas易碎,应该是14个元素。
答案 0 :(得分:1)
甚至可以使其有效吗?
是的,它通常被称为复合布局。
您遇到的问题是您正在尝试将Layout
作为约束提供给当前容器布局管理器(在您的情况下,您很幸运它不会期待任何)...
相反,将布局管理器直接提供给子面板......类似于......
setLayout(new GridLayout(5, 1)); //The big JPanel
JPanel a = new JPanel(new FlowLayout(4));
JPanel b = new JPanel(new GridLayout(7,2));
add(a);
add(b);
请查看Laying Out Components Within a Container了解更多详情......
使用示例更新
简单的可运行示例,使用标签作为占位符。
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;
public class ExampleLayout {
public static void main(String[] args) {
new ExampleLayout();
}
public ExampleLayout() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JPanel mainPane = new JPanel(new GridLayout(5, 1));
mainPane.add(new APanel());
mainPane.add(new BPanel());
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(mainPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class APanel extends JPanel {
public APanel() {
setLayout(new FlowLayout(4));
for (int index = 0; index < 4; index++) {
add(new JLabel(Integer.toString(index)));
}
setBorder(new LineBorder(Color.RED));
}
}
public class BPanel extends JPanel {
public BPanel() {
setLayout(new GridLayout(7, 2));
for (int index = 0; index < 14; index++) {
add(new JLabel(Integer.toString(index)));
}
setBorder(new LineBorder(Color.BLUE));
}
}
}
此setBorder(new EmptyBorder(3,-160,0,0));
看起来也不对。 EmptyBorder
永远不应该有负值
答案 1 :(得分:0)
Is it even possible to make it work?
是的,可以这样做。在以下一行
JFrame.this.add(new BigP(),BorderLayout.CENTER);
我建议做
mainFrame.getContentPane.add(new BigP());
并在此
之前设置mainFrame的布局mainFrame.setLayout(new GridLayout(5, 1));