在GridBagLayout中布置组件的方式与预期不符 [英] laying out components in GridBagLayout not as expected

查看:95
本文介绍了在GridBagLayout中布置组件的方式与预期不符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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?

推荐答案

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

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

这篇关于在GridBagLayout中布置组件的方式与预期不符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆