需要帮助修复与GUI的对齐

时间:2014-03-03 06:38:17

标签: java swing user-interface gridbaglayout

import java.awt.*;
import javax.swing.*;

public class app extends JFrame{
  public app(String title){
    super(title);

    GridBagLayout layout = new GridBagLayout();
    GridBagConstraints constraints = new GridBagConstraints();
    getContentPane().setLayout(layout);

    JLabel l1 = new JLabel("First Name:");
    constraints.gridx = 0;
    constraints.gridy = 0;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(5,5,5,5);    
    layout.setConstraints(l1, constraints);
    getContentPane().add(l1);

    JLabel l2 = new JLabel("Last Name:");
    constraints.gridx = 0;
    constraints.gridy = 1;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(5,5,5,5);    
    layout.setConstraints(l2, constraints);
    getContentPane().add(l2);

    JLabel l3 = new JLabel("Street:");
    constraints.gridx = 0;
    constraints.gridy = 2;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(5,5,5,5);    
    layout.setConstraints(l3, constraints);
    getContentPane().add(l3);

    JLabel l4 = new JLabel("City:");
    constraints.gridx = 0;
    constraints.gridy = 3;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(5,5,5,5);    
    layout.setConstraints(l4, constraints);
    getContentPane().add(l4);

    JLabel l5 = new JLabel("Province:");
    constraints.gridx = 0;
    constraints.gridy = 4;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(5,5,5,5);    
    layout.setConstraints(l5, constraints);
    getContentPane().add(l5);

    JLabel l6 = new JLabel("Postal Code:");
    constraints.gridx = 0;
    constraints.gridy = 5;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(5,5,5,5);    
    layout.setConstraints(l6, constraints);
    getContentPane().add(l6);

    JLabel l7 = new JLabel("Home Phone:");
    constraints.gridx = 2;
    constraints.gridy = 0;
    constraints.gridwidth = 2;
    constraints.gridheight = 1;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(5,5,5,5);    
    layout.setConstraints(l7, constraints);
    getContentPane().add(l7);

    JLabel l8 = new JLabel("Cell Phone:");
    constraints.gridx = 2;
    constraints.gridy = 1;
    constraints.gridwidth = 2;
    constraints.gridheight = 1;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(5,5,5,5);    
    layout.setConstraints(l8, constraints);
    getContentPane().add(l8);

    JLabel l9 = new JLabel("Email Address:");
    constraints.gridx = 2;
    constraints.gridy = 2;
    constraints.gridwidth = 2;
    constraints.gridheight = 1;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(5,5,5,5);    
    layout.setConstraints(l9, constraints);
    getContentPane().add(l9);

    JLabel l10 = new JLabel("Age:");
    constraints.gridx = 2;
    constraints.gridy = 3;
    constraints.gridwidth = 2;
    constraints.gridheight = 1;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(5,5,5,5);    
    layout.setConstraints(l10, constraints);
    getContentPane().add(l10);

    JLabel l11 = new JLabel("Gender:");
    constraints.gridx = 2;
    constraints.gridy = 4;
    constraints.gridwidth = 2;
    constraints.gridheight = 1;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(5,5,5,5);    
    layout.setConstraints(l11, constraints);
    getContentPane().add(l11);

    JTextField t1 = new JTextField();
    constraints.gridx = 1;
    constraints.gridy = 0;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(5,5,5,5);
    layout.setConstraints(t1, constraints);
    getContentPane().add(t1);

    JTextField t2 = new JTextField();
    constraints.gridx = 1;
    constraints.gridy = 1;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(5,5,5,5);
    layout.setConstraints(t2, constraints);
    getContentPane().add(t2);

    JTextField t3 = new JTextField();
    constraints.gridx = 1;
    constraints.gridy = 2;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(5,5,5,5);
    layout.setConstraints(t3, constraints);
    getContentPane().add(t3);

    JTextField t4 = new JTextField();
    constraints.gridx = 1;
    constraints.gridy = 3;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(5,5,5,5);
    layout.setConstraints(t4, constraints);
    getContentPane().add(t4);

    JTextField t5 = new JTextField();
    constraints.gridx = 1;
    constraints.gridy = 5;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(5,5,5,5);
    layout.setConstraints(t5, constraints);
    getContentPane().add(t5);

    JTextField t6 = new JTextField();
    constraints.gridx = 4;
    constraints.gridy = 0;
    constraints.gridwidth = 3;
    constraints.gridheight = 1;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(5,5,5,5);
    layout.setConstraints(t6, constraints);
    getContentPane().add(t6);

    JTextField t7 = new JTextField();
    constraints.gridx = 4;
    constraints.gridy = 1;
    constraints.gridwidth = 3;
    constraints.gridheight = 1;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(5,5,5,5);
    layout.setConstraints(t7, constraints);
    getContentPane().add(t7);

    JTextField t8 = new JTextField();
    constraints.gridx = 4;
    constraints.gridy = 2;
    constraints.gridwidth = 3;
    constraints.gridheight = 1;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(5,5,5,5);
    layout.setConstraints(t8, constraints);
    getContentPane().add(t8);

    JComboBox c1 = new JComboBox();
    constraints.gridx = 1;
    constraints.gridy = 4;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(5,5,5,5);
    layout.setConstraints(c1, constraints);
    getContentPane().add(c1);

    JSpinner s1 = new JSpinner();
    constraints.gridx = 4;
    constraints.gridy = 3;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(5,5,5,5);
    layout.setConstraints(s1, constraints);
    getContentPane().add(s1);

    JRadioButton r1 = new JRadioButton();
    constraints.gridx = 4;
    constraints.gridy = 4;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(5,5,5,5);
    layout.setConstraints(r1, constraints);
    getContentPane().add(r1);

    JRadioButton r2 = new JRadioButton();
    constraints.gridx = 4;
    constraints.gridy = 5;
    constraints.gridwidth = 2;
    constraints.gridheight = 1;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(5,5,5,5);
    layout.setConstraints(r2, constraints);
    getContentPane().add(r2);

    JButton b1 = new JButton("Clear");
    constraints.gridx = 2;
    constraints.gridy = 6;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.ipadx = 5;
    constraints.ipady = 5;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(5,5,5,5);
    layout.setConstraints(b1, constraints);
    getContentPane().add(b1);

    JButton b2 = new JButton("Submit");
    constraints.gridx = 3;
    constraints.gridy = 6;
    constraints.gridwidth = 3;
    constraints.gridheight = 1;
    constraints.ipadx = 5;
    constraints.ipady = 5;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(5,5,5,5);
    layout.setConstraints(b2, constraints);
    getContentPane().add(b2);

    JButton b3 = new JButton("Quit");
    constraints.gridx = 6;
    constraints.gridy = 6;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.ipadx = 5;
    constraints.ipady = 5;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(5,5,5,5);
    layout.setConstraints(b3, constraints);
    getContentPane().add(b3);

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(560,330);
    setResizable(false);
  }
  public static void main(String[] args){
    new app("Info App").setVisible(true);
  }
}

当前的GUI:

Current GUI

预期的GUI: Intended GUI

我正在尝试将此GridBagLayout对齐,使得左半部分的文本字段宽度与右侧的文本字段相等。另外,有没有办法将侧面展开以更好地贴合?感谢。

5 个答案:

答案 0 :(得分:0)

您可以在pack()Window类中使用Container implemented方法,该类是{{3}}类的子类。

答案 1 :(得分:0)

您是否考虑过使用Miglayout?它的备忘单中显示了许多可能有用的功能。

MigLayout
Mig cheat sheet

答案 2 :(得分:0)

用户te xtfiled.setBounds()传递textfiled的对齐和高度和宽度的参数,并使用Dimension类来设置文本字段的大小 单击此link以获取Dimension类方法

答案 3 :(得分:0)

您可以使用JTextField(int columns)构造函数为JTextFields设置固定大小的列。

答案 4 :(得分:0)

对于初学者,您可以将文本字段设置为默认大小:

textfield.setPreferredSize(new Dimension(200, 20));

或者您可以将GridBagConstraints设置为填充:

constraints.fill = GridBagConstraints.BOTH;

接下来,如果您希望项目扩散到两侧,请尝试将您的weightx和weighty设置为1而不是0:

constraints.weightx = 1;
constraints.weighty = 1;

如果你像这样工作,你应该丢失插图。 有关此问题的详细信息,请尝试the oracle documentation about gridbaglayout

我还建议为帧设置一个优先大小并使其不可调整:

setPreferredSize(new Dimension(400, 400));
setResizable(false);

最后但并非最不重要:每次添加对象时都不必设置约束值,添加内容后它们保持不变,因此设置一次就足够了。
此外,您可以使用以下方法简化添加过程:

getContentPane().add(textfield, constraints);

希望这能解决您的问题