我正在尝试使用Anchor来设置组件的对齐方式。 这是我的代码:
public void intiConOpt()
{
/*********************************** connection options ****************************************/
conOptGBC.insets = new Insets(5, 5, 5, 5);
conOptGBC.weightx = 1.0;
conOptGBC.weighty = 1.0;
conOptGBC.anchor = GridBagConstraints.NORTHWEST;
conOpt = new JPanel();
conOpt.setLayout(new GridBagLayout());
//////////////////////////////////////////cycle time////////////////////////////////////////////
readPeriodLabel = new JLabel("Node read cycle time:");
conOptGBC.gridx = 0;
conOptGBC.gridy = 0;
conOpt.add(readPeriodLabel, conOptGBC);
readPeriodNumberModel = new SpinnerNumberModel();
readPeriodSpinner = new JSpinner(readPeriodNumberModel);
JSpinner.NumberEditor readPeriodEditor = new JSpinner.NumberEditor(readPeriodSpinner,"###");
readPeriodSpinner.setEditor(readPeriodEditor);
readPeriodNumberModel.setValue(TopologyMain.settings.getSettingsList().get("nodeReadingCycleTime"));
readPeriodSpinner.setPreferredSize(new Dimension(40, 20));
conOptGBC.gridx = 1;
conOptGBC.gridy = 0;
conOpt.add(readPeriodSpinner, conOptGBC);
readPeriodLabel2 = new JLabel("<html><font size=2>(4-180 sec)</font></html>");
conOptGBC.gridx = 2;
conOptGBC.gridy = 0;
conOpt.add(readPeriodLabel2, conOptGBC);
///////////////////////////////////////////time out////////////////////////////////////////////
cliTimeoutLabel = new JLabel("CLI response timeout:");
conOptGBC.gridx = 0;
conOptGBC.gridy = 1;
conOpt.add(cliTimeoutLabel, conOptGBC);
cliTimeoutNumberModel = new SpinnerNumberModel();
cliTimeoutSpinner = new JSpinner(cliTimeoutNumberModel);
JSpinner.NumberEditor cliTimeoutEditor = new JSpinner.NumberEditor(cliTimeoutSpinner,"###");
cliTimeoutSpinner.setEditor(cliTimeoutEditor);
cliTimeoutNumberModel.setValue(TopologyMain.settings.getSettingsList().get("cliTimeout") /10);
cliTimeoutSpinner.setPreferredSize(new Dimension(40, 20));
conOptGBC.gridx = 1;
conOptGBC.gridy = 1;
conOpt.add(cliTimeoutSpinner, conOptGBC);
cliTimeoutLabel2 = new JLabel("<html><font size=2>(8-999 sec)</font></html>");
conOptGBC.gridx = 2;
conOptGBC.gridy = 1;
conOpt.add(cliTimeoutLabel2, conOptGBC);
///////////////////////////////////////busy response//////////////////////////////////////////////
cliBusySleepLabel = new JLabel("CLI busy check time:");
conOptGBC.gridx = 0;
conOptGBC.gridy = 2;
conOpt.add(cliBusySleepLabel, conOptGBC);
cliBusySleepNumberModel = new SpinnerNumberModel();
cliBusySleepSpinner = new JSpinner(cliBusySleepNumberModel);
JSpinner.NumberEditor cliBusySleepEditor = new JSpinner.NumberEditor(cliBusySleepSpinner,"###");
cliBusySleepSpinner.setEditor(cliBusySleepEditor);
cliBusySleepNumberModel.setValue(TopologyMain.settings.getSettingsList().get("cliBusySleep") /1000);
cliBusySleepSpinner.setPreferredSize(new Dimension(40, 20));
conOptGBC.gridx = 1;
conOptGBC.gridy = 2;
conOpt.add(cliBusySleepSpinner, conOptGBC);
cliBusySleepLabel2 = new JLabel("<html><font size=2>(1-999 sec)</font></html>");
conOptGBC.gridx = 2;
conOptGBC.gridy = 2;
conOpt.add(cliBusySleepLabel2, conOptGBC);
conOpt.setVisible(false);
contentPanel.add(conOpt);
}
我的问题是GridBagConstraints锚没有效果,我的组件仍然在面板的中心。
屏幕截图:http://postimg.org/image/5nopucusj/
有什么想法吗?谢谢!
答案 0 :(得分:3)
anchor
属性工作正常,您遇到的问题是......
conOptGBC.weightx = 1.0;
conOptGBC.weighty = 1.0;
问题是,您已经要求GridBagLayout
为每列分配相等数量的空间,因此它们将占用,在这种情况下,它们是第三列,这是可以考虑的。
删除你得到的conOptGBC.weightx = 1.0;
...
有点难以看到,但如果我们在...中添加一些指南
你可以看到这些字段开始与西北方向对齐......