GridBagLayout 没有按预期安排 JPanel
GridBagLayout not arranging JPanels as expected
我有一个简单的 Swing 应用程序,如下所示:
周围有边框的每组组件都是一个 JPanel
。我正在使用 GridBagLayout
。我遇到的问题是它们没有按预期排列。我想要以下布局:
我正在使用以下代码:
public class Window extends JFrame
{
public Window()
{
// set window properties
this.setTitle("My Automotive");
this.setSize(this.WINDOW_WIDTH, this.WINDOW_HEIGHT);
this.setResizable(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this._panel = new JPanel();
this._panel.setLayout(new GridBagLayout());
this._oilAndLubePanel.setBorder(BorderFactory.createTitledBorder("Oil and Lube"));
this._oilAndLubePanel.setLayout(new BoxLayout(this._oilAndLubePanel, BoxLayout.Y_AXIS));
this._oilAndLubePanel.add(this._oilChangeCb);
this._oilAndLubePanel.add(this._lubeJobCb);
addGroup(this._oilAndLubePanel, 0, 0);
this._flushesPanel.setBorder(BorderFactory.createTitledBorder("Flushes"));
this._flushesPanel.setLayout(new BoxLayout(this._flushesPanel, BoxLayout.Y_AXIS));
this._flushesPanel.add(this._radiatorFlushCb);
this._flushesPanel.add(this._transmissionFlushCb);
addGroup(this._flushesPanel, 1, 0);
this._miscPanel.setBorder(BorderFactory.createTitledBorder("Misc"));
this._miscPanel.setLayout(new BoxLayout(this._miscPanel, BoxLayout.Y_AXIS));
this._miscPanel.add(this._inspectionCb);
this._miscPanel.add(this._replaceMufflerCb);
this._miscPanel.add(this._tireRotationCb);
addGroup(this._miscPanel, 0, 1);
this._partsAndLabourPanel.setBorder(BorderFactory.createTitledBorder("Parts and Labour"));
this._partsAndLabourPanel.setLayout(new BoxLayout(this._partsAndLabourPanel, BoxLayout.Y_AXIS));
this._partsAndLabourPanel.add(this._partsFieldLbl);
this._partsAndLabourPanel.add(this._partsField);
this._partsAndLabourPanel.add(this._labourFieldLbl);
this._partsAndLabourPanel.add(this._labourField);
addGroup(this._partsAndLabourPanel, 1, 1);
this._summaryPanel.setBorder(BorderFactory.createTitledBorder("Summary"));
this._summaryPanel.setLayout(new GridBagLayout());
this.addComponentToSummaryPanel(this._servicesAndLabourFieldLbl, 0, 0);
this._servicesAndLabourField.setEditable(false);
this.addComponentToSummaryPanel(this._servicesAndLabourField, 1, 0);
this.addComponentToSummaryPanel(this._partsSummaryFieldLbl, 0, 1);
this._partsSummaryField.setEditable(false);
this.addComponentToSummaryPanel(this._partsSummaryField, 1, 1);
this.addComponentToSummaryPanel(this._taxFielLbl, 0, 2);
this._taxField.setEditable(false);
this.addComponentToSummaryPanel(this._taxField, 1, 2);
this.addComponentToSummaryPanel(this._totalFeesFieldLbl, 0, 3);
this._totalFeesField.setEditable(false);
this.addComponentToSummaryPanel(this._totalFeesField, 1, 3);
addGroup(this._summaryPanel, 0, 2);
this.add(this._panel);
this.setVisible(true);
}
private void addGroup(JPanel panel,
int gridx, int gridy)
{
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1; // use all horizontal space available
gbc.weighty = 1; // use all vertical space available
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.insets = new Insets(2,2,2,2);
gbc.anchor = GridBagConstraints.CENTER;
this._panel.add(panel);
}
}
为什么没有实现所需的布局,如何解决?
添加组件时不定义约束条件是不行的!所以:
this._panel.add(panel);
应该是:
this._panel.add(panel, gbc);
我有一个简单的 Swing 应用程序,如下所示:
周围有边框的每组组件都是一个 JPanel
。我正在使用 GridBagLayout
。我遇到的问题是它们没有按预期排列。我想要以下布局:
我正在使用以下代码:
public class Window extends JFrame
{
public Window()
{
// set window properties
this.setTitle("My Automotive");
this.setSize(this.WINDOW_WIDTH, this.WINDOW_HEIGHT);
this.setResizable(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this._panel = new JPanel();
this._panel.setLayout(new GridBagLayout());
this._oilAndLubePanel.setBorder(BorderFactory.createTitledBorder("Oil and Lube"));
this._oilAndLubePanel.setLayout(new BoxLayout(this._oilAndLubePanel, BoxLayout.Y_AXIS));
this._oilAndLubePanel.add(this._oilChangeCb);
this._oilAndLubePanel.add(this._lubeJobCb);
addGroup(this._oilAndLubePanel, 0, 0);
this._flushesPanel.setBorder(BorderFactory.createTitledBorder("Flushes"));
this._flushesPanel.setLayout(new BoxLayout(this._flushesPanel, BoxLayout.Y_AXIS));
this._flushesPanel.add(this._radiatorFlushCb);
this._flushesPanel.add(this._transmissionFlushCb);
addGroup(this._flushesPanel, 1, 0);
this._miscPanel.setBorder(BorderFactory.createTitledBorder("Misc"));
this._miscPanel.setLayout(new BoxLayout(this._miscPanel, BoxLayout.Y_AXIS));
this._miscPanel.add(this._inspectionCb);
this._miscPanel.add(this._replaceMufflerCb);
this._miscPanel.add(this._tireRotationCb);
addGroup(this._miscPanel, 0, 1);
this._partsAndLabourPanel.setBorder(BorderFactory.createTitledBorder("Parts and Labour"));
this._partsAndLabourPanel.setLayout(new BoxLayout(this._partsAndLabourPanel, BoxLayout.Y_AXIS));
this._partsAndLabourPanel.add(this._partsFieldLbl);
this._partsAndLabourPanel.add(this._partsField);
this._partsAndLabourPanel.add(this._labourFieldLbl);
this._partsAndLabourPanel.add(this._labourField);
addGroup(this._partsAndLabourPanel, 1, 1);
this._summaryPanel.setBorder(BorderFactory.createTitledBorder("Summary"));
this._summaryPanel.setLayout(new GridBagLayout());
this.addComponentToSummaryPanel(this._servicesAndLabourFieldLbl, 0, 0);
this._servicesAndLabourField.setEditable(false);
this.addComponentToSummaryPanel(this._servicesAndLabourField, 1, 0);
this.addComponentToSummaryPanel(this._partsSummaryFieldLbl, 0, 1);
this._partsSummaryField.setEditable(false);
this.addComponentToSummaryPanel(this._partsSummaryField, 1, 1);
this.addComponentToSummaryPanel(this._taxFielLbl, 0, 2);
this._taxField.setEditable(false);
this.addComponentToSummaryPanel(this._taxField, 1, 2);
this.addComponentToSummaryPanel(this._totalFeesFieldLbl, 0, 3);
this._totalFeesField.setEditable(false);
this.addComponentToSummaryPanel(this._totalFeesField, 1, 3);
addGroup(this._summaryPanel, 0, 2);
this.add(this._panel);
this.setVisible(true);
}
private void addGroup(JPanel panel,
int gridx, int gridy)
{
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1; // use all horizontal space available
gbc.weighty = 1; // use all vertical space available
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.insets = new Insets(2,2,2,2);
gbc.anchor = GridBagConstraints.CENTER;
this._panel.add(panel);
}
}
为什么没有实现所需的布局,如何解决?
添加组件时不定义约束条件是不行的!所以:
this._panel.add(panel);
应该是:
this._panel.add(panel, gbc);