[英]Which Layout Manager Should I use to achieve the following?
我有一個JFrame
和三個JPanel
。 在框架上,我使用BorderLayout
。 在框架的CENTER
,我放置了outerPanel
。 在我的outerPanel
我使用了MigLayout
。 另外兩個面板添加到outerPanel
。 這兩個面板的大小相等,其寬度加起來等於outerPanel
的寬度- 我希望將 outerPanel
分為兩半 。 下面是此代碼:
public class ControlPanel extends JFrame {
// components
public JPanel outerPanel;
public JPanel innerPanel1;
public JPanel innerPanel2;
public ControlPanel() {
this.createUI();
}
public void createUI() {
// form properties
this.setSize(new java.awt.Dimension(300, 300));
this.setVisible(true);
this.setLayout(new java.awt.BorderLayout());
this.outerPanel = new JPanel();
this.outerPanel.setPreferredSize(new java.awt.Dimension(260, 250));
this.outerPanel.setLayout(new net.miginfocom.swing.MigLayout());
this.outerPanel.setBorder(BorderFactory.createEtchedBorder());
this.add(new javax.swing.JLabel("North"), BorderLayout.NORTH);
this.add(this.outerPanel, BorderLayout.CENTER);
this.innerPanel1 = new JPanel();
this.innerPanel1.setPreferredSize(new java.awt.Dimension(130, 150));
this.innerPanel1.setLayout(new net.miginfocom.swing.MigLayout());
this.innerPanel1.setBorder(BorderFactory.createTitledBorder("Panel1"));
this.innerPanel2 = new JPanel();
this.innerPanel2.setPreferredSize(new java.awt.Dimension(130, 150));
this.innerPanel2.setLayout(new net.miginfocom.swing.MigLayout());
this.innerPanel2.setBorder(BorderFactory.createTitledBorder("Panel2"));
this.outerPanel.add(this.innerPanel1);
this.outerPanel.add(this.innerPanel2);
this.pack();
}
public static void main(String[] args) {
ControlPanel cp = new ControlPanel();
}
}
問題 :運行程序時,在調整窗口大小之前出現的GUI很好; 但是, 當我調整窗口的大小以 innerPane1
它時 , innerPane1
和innerPanel2
保持相同的大小,而沒有調整大小以占據可用空間 。
問題 :我們如何使兩個面板innerPannel1
和innerPanel2
與窗口同時調整大小,以便它們可以平均共享可用空間? 是否有任何特定的布局管理器可用於將面板分為兩個相等的部分,這些部分可以與窗口同時調整大小?
顯示輸出的圖像。
我建議您使用new GridLayout(1, 2)
。 這會將面板分成1行和2(相等大小)列。
因此,只需更改
this.outerPanel = new JPanel();
至
this.outerPanel = new JPanel(new GridLayout(1, 2));
應該做。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.