[英]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.