我想要这个:
我试过这个:
// Vertically center
formatbp.setLayout (new GridBagLayout()); // formatbp is a JPanel
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = GridBagConstraints.RELATIVE;
rbpanel.setLayout(new BoxLayout(rbpanel, BoxLayout.PAGE_AXIS)); // rbpanel is also a JPanel
rb = new ButtonGroup();
rbpanel.add(new JLabel("Words are seperated by: "));
rbLinesOrTabs.setSelected(true);
rb.add(rbLinesOrTabs);
rbpanel.add(rbLinesOrTabs);
rbLinesOrTabs.addActionListener(this);
rbotherpanel = new JPanel(new FlowLayout());
rb.add(rbOther);
rbpanel.add(rbOther);
rbOther.addActionListener(this);
othercharfield.setEnabled(false); // Is going to be enabled when rbOther gets selected (and disabled again when rbLinesOrTabs is selected again), that is where the actionlisteners are for
rbotherpanel.add(othercharfield);
rbpanel.add(rbotherpanel);
formatbp.add(rbpanel,gbc);
formatbp.add(formatb,gbc); // formatb is the button
(大多数在代码中较早初始化的对象)
但这是结果:
我究竟做错了什么?
编辑:我发现我在这里犯了一个错误:
rbpanel.add(rbOther);
那应该是:
rbotherpanel.add(rbOther);
现在我得到:
更好,但其他项目未正确对齐。(如您所见,它有点偏右)