laying out components in GridBagLayout not as expected

瘦欲@ 提交于 2019-12-12 03:12:24

问题


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();


}
}

In this I have added a few labels,radio buttons,textfields.The radio buttons and labels are getting displayed as I want them but the text fields are not getting displayed properly. Can anyone tell me how to get the text fields to cover more space?


回答1:


You should probably consider setting the two following values on your constraints for the textfields:

GridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
GridBagConstraints.weightx = 1.0;// If there is extra room horizontally, the textfields will receive it



回答2:


Since your JTextFields do not have their column size properties set, their preferredSize will be quite small. Consider giving your JTextFields default column size:

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);
      // ....


来源:https://stackoverflow.com/questions/10163501/laying-out-components-in-gridbaglayout-not-as-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!