如何布局按钮和组合框中间?

时间:2014-03-04 06:46:20

标签: java swing combobox layout-manager

我是java swing的新手,我写了一个启动程序来编写文本,但我对布局感到困惑, 结果如下: enter image description here

我想要组合框并且按钮放在ctrlPanel的中间,组合框不应该被拉伸

 public class MainFrame extends JFrame {
private static final long serialVersionUID = 7553142908344084288L;

private static String[] formats = new String[] {
    "JSON",
    "XML",
    "YAML"
};

public MainFrame() {
    super("jValidator");
    Panel mainPanel = new Panel();
    mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS));
    setContentPane(mainPanel);

    JTextArea fromTextArea = new JTextArea(20, 40);
    JScrollPane fromTextAreaScrollPanel = new JScrollPane(fromTextArea);
    fromTextAreaScrollPanel.setPreferredSize(new Dimension(300, 300));
    fromTextAreaScrollPanel.setBorder(BorderFactory.createEmptyBorder(15, 5, 15, 5));
    mainPanel.add(fromTextAreaScrollPanel);

    JButton fmtButton = new JButton("Format >>");
    JComboBox jComboBox = new JComboBox(formats);
    jComboBox.setBorder(BorderFactory.createTitledBorder("Text Format"));

    JPanel ctrPanel = new JPanel();
    ctrPanel.setLayout(new BoxLayout(ctrPanel, BoxLayout.Y_AXIS));
    ctrPanel.setAlignmentY(Component.CENTER_ALIGNMENT);
    ctrPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));

    ctrPanel.add(jComboBox);
    ctrPanel.add(Box.createRigidArea(new Dimension(50, 15)));
    ctrPanel.add(fmtButton);
    mainPanel.add(ctrPanel);

    JTextArea toTextArea = new JTextArea(20, 40);
    JScrollPane toTextAreaScrollPanel = new JScrollPane(toTextArea);
    toTextAreaScrollPanel.setPreferredSize(new Dimension(300, 300));
    toTextAreaScrollPanel.setBorder(BorderFactory.createEmptyBorder(15, 5, 15, 5));
    mainPanel.add(toTextAreaScrollPanel);

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    pack();
    setVisible(true);
}

public static void main(String[] args) {
    new MainFrame();
}
 }

2 个答案:

答案 0 :(得分:3)

您可以使用GridBagLayout代替BoxLayout ...

JPanel ctrPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
ctrPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));

gbc.fill = GridBagConstraints.HORIZONTAL;
ctrPanel.add(jComboBox, gbc);
ctrPanel.add(Box.createRigidArea(new Dimension(50, 15)), gbc);
gbc.fill = GridBagConstraints.NONE;
ctrPanel.add(fmtButton, gbc);

enter image description here

请查看Laying Out Components Within a Container了解详情

答案 1 :(得分:3)

出于这个目的,我建议您使用其他LayoutManager,例如GridBagLayout创建ctrPanel,例如下一个:

JButton fmtButton = new JButton("Format >>");
JComboBox jComboBox = new JComboBox(formats);
jComboBox.setBorder(BorderFactory.createTitledBorder("Text Format"));

JPanel ctrPanel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx=0;
c.gridy=1;
ctrPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
ctrPanel.add(fmtButton,c);
c.gridy=0;
c.fill = GridBagConstraints.HORIZONTAL;
ctrPanel.add(jComboBox,c);
mainPanel.add(ctrPanel);

它看起来像:

enter image description here