JPanel中的JPanels
我有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个元素。
是否有可能使它工作?
是的,它通常被称为复合布局。
您遇到的问题是您正在尝试将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);
请查看在容器中布置组件以获取更多详细信息……
以示例更新
简单的可运行示例,使用标签作为占位符。
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
永远不应该有负值
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));