0
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class GridBagLayoutDemo extends JFrame
{
private JFrame frame;
private JPanel panel;
private JLabel l1,l2,l3,l4,l5,l6,l7,l8;
private JRadioButton r1,r2,r3,r4,r5,r6,r7,r8;
private JTextField t1,t2,t3,t4;
private JComboBox c1;
GridBagConstraints gbc;
GridBagLayout  gbl;


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

public GridBagLayoutDemo()
{
    frame=new JFrame();
    panel=new JPanel();
    panel.setBackground(Color.YELLOW);
    gbl   = new GridBagLayout();  

    gbc = new GridBagConstraints(); 

    panel.setLayout(gbl);

    l1=new JLabel("passport");
    gbc.anchor = gbc.EAST;  
    gbc.gridx=0;
    gbc.gridy=0;
    gbc.gridwidth=1;
    gbc.gridheight=1;
    //gbc.weightx=1.0;
    panel.add(l1,gbc);

    l2=new JLabel("pass no.");
    gbc.gridx=0;
    gbc.gridy=1;
    gbc.gridwidth=1;
    gbc.gridheight=1;
    panel.add(l2,gbc);


    l3=new JLabel("Valid Upto");
    gbc.gridx=0;
    gbc.gridy=2;
    gbc.gridwidth=1;
    gbc.gridheight=1;
    panel.add(l3,gbc);

    l4=new JLabel("Identification");
    gbc.gridx=0;
    gbc.gridy=3;
    gbc.gridwidth=1;
    gbc.gridheight=1;
    panel.add(l4,gbc);

    l5=new JLabel("NO.");
    gbc.gridx=0;
    gbc.gridy=4;
    gbc.gridwidth=1;
    gbc.gridheight=1;
    panel.add(l5,gbc);

    l6=new JLabel("Marital Status");
    gbc.gridx=0;
    gbc.gridy=5;
    gbc.gridwidth=1;
    gbc.gridheight=1;
    panel.add(l6,gbc);

    l7=new JLabel("Nationality");
    gbc.gridx=0;
    gbc.gridy=6;
    gbc.gridwidth=1;
    gbc.gridheight=1;
    panel.add(l7,gbc);

    l8=new JLabel("Type of Position");
    gbc.gridx=0;
    gbc.gridy=7;
    gbc.gridwidth=1;
    gbc.gridheight=1;
    panel.add(l8,gbc);

    r1=new JRadioButton("Yes");
    gbc.gridx=2;
    gbc.gridy=0;
    gbc.gridwidth=1;
    gbc.gridheight=1;
    panel.add(r1,gbc);

    r2=new JRadioButton("No");
    gbc.gridx=4;
    gbc.gridy=0;
    gbc.gridwidth=1;
    gbc.gridheight=1;
    panel.add(r2,gbc);

    r3=new JRadioButton("Married");
    gbc.gridx=2;
    gbc.gridy=5;
    gbc.gridwidth=1;
    gbc.gridheight=1;
    panel.add(r3,gbc);

    r4=new JRadioButton("Unmarried");
    gbc.gridx=4;
    gbc.gridy=5;
    gbc.gridwidth=1;
    gbc.gridheight=1;
    panel.add(r4,gbc);

    r5=new JRadioButton("Full time");
    gbc.gridx=2;
    gbc.gridy=7;
    gbc.gridwidth=1;
    gbc.gridheight=1;
    panel.add(r5,gbc);

    r6=new JRadioButton("Part Time");
    gbc.gridx=3;
    gbc.gridy=7;
    gbc.gridwidth=1;
    gbc.gridheight=1;
    panel.add(r6,gbc);

    r7=new JRadioButton("Contract Basis");
    gbc.gridx=4;
    gbc.gridy=7;
    gbc.gridwidth=1;
    gbc.gridheight=1;
    panel.add(r7,gbc);

    r8=new JRadioButton("Intern");
    gbc.gridx=2;
    gbc.gridy=8;
    gbc.gridwidth=1;
    gbc.gridheight=1;
    panel.add(r8,gbc);


    t1=new JTextField();
    gbc.gridx=2;
    gbc.gridy=1;
    gbc.gridwidth=3;
    gbc.gridheight=1;
    panel.add(t1,gbc);

    t2=new JTextField();
    gbc.gridx=2;
    gbc.gridy=2;
    gbc.gridwidth=3;
    gbc.gridheight=1;
    panel.add(t2,gbc);

    t3=new JTextField();
    gbc.gridx=2;
    gbc.gridy=4;
    gbc.gridwidth=3;
    gbc.gridheight=1;
    panel.add(t3,gbc);

    t4=new JTextField();
    gbc.gridx=2;
    gbc.gridy=6;
    gbc.gridwidth=3;
    gbc.gridheight=1;
    panel.add(t4,gbc);





    frame.add(panel);

    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    //frame.setSize(300,200);
    frame.pack();

    //c1=new JComboBox();
    //frame1();


}
}

在此我添加了一些标签、单选按钮、文本字段。单选按钮和标签正在按我想要的方式显示,但文本字段没有正确显示。谁能告诉我如何让文本字段覆盖更多空间?

这就是gui的显示方式

4

2 回答 2

2

由于您的 JTextField 没有设置其列大小属性,因此它们的 preferredSize 将非常小。考虑给您的 JTextFields 默认列大小:

public class GridBagLayoutDemo extends JFrame {
   private static final int TF_COLS = 10;

   // ....

   public GridBagLayoutDemo() {
      t1 = new JTextField(TF_COLS);
      t2 = new JTextField(TF_COLS);
      t3 = new JTextField(TF_COLS);
      t4 = new JTextField(TF_COLS);
      // ....
于 2012-04-15T15:49:46.480 回答
2

您可能应该考虑为文本字段的约束设置以下两个值:

GridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
GridBagConstraints.weightx = 1.0;// If there is extra room horizontally, the textfields will receive it
于 2012-04-15T15:52:29.440 回答