[英]How to get this GUI layout right?
我想要这个:
我试过这个:
// 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);
现在我得到:
更好,但其他项目没有正确对齐。 (你可以看到右侧有点)
您正在向rbPanel添加四个内容,这意味着您将获得四行(就像您的屏幕截图示例一样)。 其他和文本字段需要位于同一行,因此您应将它们放在自己的面板中,或使用GridbagConstraints正确定位所有组件。
使用GridX和GridY而不是RELATIVE,因为这使代码更容易理解。
使用MigLayout的一体化方法(是的,它真的是我目前最喜欢的:-)
MigLayout layout = new MigLayout("debug", "[][]");
JComponent content = new JPanel(layout);
content.add(new JLabel("Words are separated by: "), "span");
JRadioButton radio = new JRadioButton("Lines or tabs");
content.add(radio, "wrap");
// split the cell so it will contain both the other button
// and the textfield
content.add(new JRadioButton("Other:"), "split 2");
// get the right margin of the upper radiobutton
int rightMargin = radio.getInsets().right;
content.add(new JTextField(), "grow, wrap, " +
// remove the gap to the preceding radiobutton
"gapx 0, " +
// set the padding to compensate the right edge
"pad 0 0 0 -" + rightMargin + "px");
content.add(new JButton("Format"), "span, center");
showInFrame(content, "align to button text");
共享单元格中精细调整的视觉结果有点依赖于LAF。 在Windows中看起来不错,在Nimbus中看起来不太好(后者在没有任何补偿的情况下看起来最好),所以你需要进行一些实验。
试试下面的代码,
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
JFrame frame = new JFrame();
frame.setBounds(0, 0, 300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel formatbp = new JPanel();
formatbp.setLayout (new GridBagLayout()); // formatbp is a JPanel
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = GridBagConstraints.RELATIVE;
JPanel rbpanel = new JPanel();
rbpanel.setLayout(new BoxLayout(rbpanel, BoxLayout.Y_AXIS)); // rbpanel is also a JPanel
ButtonGroup rb = new ButtonGroup();
rbpanel.add(new JLabel("Words are seperated by: "));
JRadioButton rbLinesOrTabs = new JRadioButton("line or tabs");
rbLinesOrTabs.setSelected(true);
rb.add(rbLinesOrTabs);
rbpanel.add(rbLinesOrTabs);
// rbLinesOrTabs.addActionListener(this);
JRadioButton rbOther = new JRadioButton("others:");
JPanel rbotherpanel = new JPanel();
rb.add(rbOther);
rbotherpanel.add(rbOther);
// rbOther.addActionListener(this);
JTextField othercharfield = new JTextField("test", 4);
// othercharfield.setColumns(800);
// 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);
JButton formatb = new JButton("format");
formatbp.add(formatb,gbc); // formatb is the button
frame.getContentPane().add(formatbp);
frame.pack();
frame.setVisible(true);
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.