我想做一个像上图一样的界面,以下是我写的代码import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;public class Ex6_1 extends Applet
{
Label lblTitle = new Label("个人资料");
Label lblName = new Label("真实姓名");
Label lblSex = new Label("性别");
Label lblBirthday = new Label("出生日期");
Label lblYear = new Label("年");
Label lblMonth = new Label("月");
Label lblDay = new Label("日");
Label lblKind = new Label("证件类型");
Label lblNumber = new Label("证件号码");
TextField tfName = new TextField(20);
TextField tfYear = new TextField("1970", 4);
TextField tfMonth = new TextField("00", 2);
TextField tfDay = new TextField("00", 2);
TextField tfNumber = new TextField(20);
CheckboxGroup cbgSex = new CheckboxGroup();
Checkbox cbMale = new Checkbox("男", true, cbgSex);
Checkbox cbFemale = new Checkbox("女", true, cbgSex);
Choice chKind = new Choice();
Button btnSub = new Button("提交");
Button btnRefill = new Button("重填全部信息"); public void init()
{
cbgSex.setSelectedCheckbox(cbMale);
chKind.add("身份证");
chKind.add("军人证");
chKind.add("学生证");
chKind.add("护照");
chKind.select(0); Panel pTitle = new Panel();
pTitle.add(lblTitle);
Panel pName = new Panel();
pName.add(lblName);
pName.add(tfName);
Panel pSex = new Panel();
pSex.add(lblSex);
pSex.add(cbMale);
pSex.add(cbFemale);
Panel p1 = new Panel(new BorderLayout());
p1.add(pTitle, BorderLayout.NORTH);
p1.add(pName, BorderLayout.CENTER);
p1.add(pSex, BorderLayout.SOUTH); Panel pBirthday = new Panel();
pBirthday.add(lblBirthday);
pBirthday.add(tfYear);
pBirthday.add(lblYear);
pBirthday.add(tfMonth);
pBirthday.add(lblMonth);
pBirthday.add(tfDay);
pBirthday.add(lblDay);
Panel pKind = new Panel();
pKind.add(lblKind);
pKind.add(chKind);
Panel pNumber = new Panel();
pNumber.add(lblNumber);
pNumber.add(tfNumber);
Panel p2 = new Panel(new BorderLayout());
p2.add(pBirthday, BorderLayout.NORTH);
p2.add(pKind, BorderLayout.CENTER);
p2.add(pNumber, BorderLayout.SOUTH); Panel pButton = new Panel();
pButton.add(btnSub);
pButton.add(btnRefill); Panel p3 = new Panel(new BorderLayout());
p3.add(p1, BorderLayout.NORTH);
p3.add(p2, BorderLayout.CENTER);
p3.add(pButton, BorderLayout.SOUTH); add(p3); } public void start()
{
} public static void main(String[] args)
{
Frame frame = new Frame("个人资料");
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
});
Ex6_1 t = new Ex6_1();
t.init();
frame.add("Center", t);
frame.setSize(400, 300);
frame.setVisible(true);
t.start();
}
};但是做出来的界面却如下图所示请问各位高手们,Panel应该怎么写才能够使做出来的界面如第一张图那样整齐

解决方案 »

  1.   

    用netbean做吧,,想怎么对齐就怎么对齐。。
    PS:俺不是来做广告的,,布局太累
      

  2.   

    import java.awt.*;
    import java.awt.event.*;
    import java.applet.Applet;public class Ex6_1 extends Applet {
    Label lblTitle = new Label("个人资料");
    Label lblName = new Label("真实姓名");
    Label lblSex = new Label("性别");
    Label lblBirthday = new Label("出生日期");
    Label lblYear = new Label("年");
    Label lblMonth = new Label("月");
    Label lblDay = new Label("日");
    Label lblKind = new Label("证件类型");
    Label lblNumber = new Label("证件号码");
    TextField tfName = new TextField(20);
    TextField tfYear = new TextField("1970", 4);
    TextField tfMonth = new TextField("00", 2);
    TextField tfDay = new TextField("00", 2);
    TextField tfNumber = new TextField(20);
    CheckboxGroup cbgSex = new CheckboxGroup();
    Checkbox cbMale = new Checkbox("男", true, cbgSex);
    Checkbox cbFemale = new Checkbox("女", true, cbgSex);
    Choice chKind = new Choice();
    Button btnSub = new Button("提交");
    Button btnRefill = new Button("重填全部信息"); public void init() {
    cbgSex.setSelectedCheckbox(cbMale);
    chKind.add("身份证");
    chKind.add("军人证");
    chKind.add("学生证");
    chKind.add("护照");
    chKind.select(0); Panel pTitle = new Panel();
    pTitle.add(lblTitle);
    Panel pName = new Panel(new FlowLayout(FlowLayout.LEFT));
    pName.add(lblName);
    pName.add(tfName);
    Panel pSex = new Panel(new FlowLayout(FlowLayout.LEFT));
    pSex.add(lblSex);
    pSex.add(cbMale);
    pSex.add(cbFemale);
    Panel p1 = new Panel(new BorderLayout());
    p1.add(pTitle, BorderLayout.NORTH);
    p1.add(pName, BorderLayout.CENTER);
    p1.add(pSex, BorderLayout.SOUTH); Panel pBirthday = new Panel(new FlowLayout(FlowLayout.LEFT));
    pBirthday.add(lblBirthday);
    pBirthday.add(tfYear);
    pBirthday.add(lblYear);
    pBirthday.add(tfMonth);
    pBirthday.add(lblMonth);
    pBirthday.add(tfDay);
    pBirthday.add(lblDay);
    Panel pKind = new Panel(new FlowLayout(FlowLayout.LEFT));
    pKind.add(lblKind);
    pKind.add(chKind);
    Panel pNumber = new Panel(new FlowLayout(FlowLayout.LEFT));
    pNumber.add(lblNumber);
    pNumber.add(tfNumber);
    Panel p2 = new Panel(new BorderLayout());
    p2.add(pBirthday, BorderLayout.NORTH);
    p2.add(pKind, BorderLayout.CENTER);
    p2.add(pNumber, BorderLayout.SOUTH); Panel pButton = new Panel();
    pButton.add(btnSub);
    pButton.add(btnRefill); Panel p3 = new Panel(new BorderLayout());
    p3.add(p1, BorderLayout.NORTH);
    p3.add(p2, BorderLayout.CENTER);
    p3.add(pButton, BorderLayout.SOUTH); add(p3); } public void start() {

    } public static void main(String[] args) {
    Frame frame = new Frame("个人资料");
    frame.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent evt) {
    System.exit(0);
    }
    });
    Ex6_1 t = new Ex6_1();
    t.init();
    frame.add("Center", t);
    frame.setSize(400, 300);
    frame.setVisible(true);
    t.start();
    }
    };楼主应该好好学一下布局管理器,JFrame的contentPane默认布局管理器是BorderLayout,JPanel的默认布局管理器是FlowLayout,而且是居中对齐,这是做Swing应该知道的基本常识。
      

  3.   

    顺路说一下,上面的代码我是改成左对齐了,但是“性别”没右对齐,也简单
    Label lblSex = new Label("  性别");
    两个全角空格就对齐了。至于netbeans,楼主很明显是在学习,用netbeans简直就是扼杀楼主学习的动力。netbeans生成的一大堆add连成一长串,看得我眼晕,还不能直接修改代码,这点简直比C#还让我不爽,C#虽然设计代码和逻辑代码分离了,但好歹设计代码是可以直接改的。另外,挺搞不懂楼主为啥现在还用awt而不用Swing。
      

  4.   

    Panel p1 = new Panel(new BorderLayout());
    这是设置布局的,个人建议自由定位,不要用布局管理器,很难达到你的要求。以前用过Jbuilder做swing,在右边把borderlayout选为none就ok了,不过每个控件上都得有一个定位。 你把布局管理器设为none,就是不用任何布局管理器(注意,不是默认的,默认的有布局管理器)。
    怎么说做界面还是用个工具的好,要不定位很麻烦啊。
      

  5.   


    不太同意,如果说一切是图省事的话,用布局管理器null当然可以,而且还得完全不考虑resize的情况,不能改变窗体大小的程序,有是有,但要求改变窗体大小的也不少。另外,从楼主的命名Ex6_1来看,明显不是在赶项目,是在学习,如果是学习,我甚至都不推荐使用IDE,还别说拖控件,学习的时候不痛苦点把原理摸透了,赶项目的时候有点什么特殊要求要直接改代码,都不知道怎么改,那时候就更痛苦。我之前学Swing的时候也是用的JBuilder,很方便是吧,后来去某个公司做外包,用的是Eclipse,没有Swing插件,不能上网,所有界面程序都是代码一行行敲出来的,刚去那会儿叫一个痛苦,但正是经过那段时间的锤炼,现在用代码很随意地就能做出效果,对原理也了解得比较深入了,说实话,代码都能直接敲出界面来,还怕拖控件?最后还是建议楼主练习的时候辛苦点,不要图简单省事,做项目的时候根据情况选择。