[英]Java - GridLayout resize JPanel
我正在尝试调整另一个具有GridLayout的JPanel中每个JPanel的高度。
这是我到目前为止的内容:
JPanel store = new JPanel();
store.setLayout(new GridLayout(4, 0));
JPanel computerPanel = new JPanel();
computerPanel.setBorder(BorderFactory.createTitledBorder("Computers"));
computerPanel.setPreferredSize(new Dimension(20, 30));
JPanel cablesPanel = new JPanel();
cablesPanel.setBorder(BorderFactory.createTitledBorder("Cables"));
JPanel accessoriesPanel = new JPanel();
accessoriesPanel.setBorder(BorderFactory.createTitledBorder("Accessories"));
JPanel repairPanel = new JPanel();
repairPanel.setBorder(BorderFactory.createTitledBorder("Repair"));
store.add(computerPanel);
store.add(cablesPanel);
store.add(accessoriesPanel);
store.add(repairPanel);
JPanel view = new JPanel();
view.setLayout(new BorderLayout());
view.add(store, BorderLayout.CENTER);
add(view);
这是GUI的输出:
我想调整JPanel存储中每个JPanel的大小(computerPanel,cablelPanel,accessoriesPanel,repairPanel)。
我努力了:
computerPanel.setPrefferedSize()
但是不起作用...
如果您希望能够调整每个JPanel的大小,则说明您没有使用适当的LayoutManager。 GridLayout(您现在正在使用)明确不允许这种大小调整,而是将每个单元格固定为完全相同的大小。
考虑使用其他LayoutManager,例如BoxLayout或GridBagLayout。
请参阅此处以确定哪种LayoutManager最适合您的需求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.