import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class TestGridBagLayout extends JFrame{
//注册名和密码
JTextField userName=null;
JPasswordField userPwd=null;
JPasswordField userPwdSubmit=null;
//性别
JRadioButton man=null;
JRadioButton woman=null;
//爱好
JCheckBox hobby1=null;
JCheckBox hobby2=null;
JCheckBox hobby3=null;
//定义几个面板
JPanel jp1=null;
JPanel jp2=null;
JPanel jp3=null;
JPanel jp4=null;
//出生日期
JComboBox year=null;
JComboBox month =null;
JComboBox day=null;
//个人简介
JTextArea intreduce=null;
//提交按钮
JButton submit=null;
JButton reset=null;
ButtonGroup bg=null;
TestGridBagLayout(){
//this.setBounds(20,20,50, 50);
this.setSize(400,600);
this.setTitle("注册用户界面");
this.setLocationRelativeTo(null);
initComponent();
addComponent();
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private void addComponent() {
//设置网格包布局
this.setLayout(new GridBagLayout());
GridBagConstraints gbc=new GridBagConstraints();
//用户名区域
gbc.gridx=0;
gbc.gridy=0;
gbc.weightx=1;
gbc.weighty=1;
gbc.anchor = GridBagConstraints.EAST;
this.add(new JLabel("用户名"),gbc);
gbc=new GridBagConstraints();
gbc.gridx=1;
gbc.gridy=0;
gbc.weightx=1;
gbc.weighty=1;
gbc.anchor = GridBagConstraints.WEST;
this.add(userName,gbc);
//设置密码区域
gbc=new GridBagConstraints();
gbc.gridx=0;
gbc.gridy=1;
gbc.weightx=1;
gbc.weighty=1;
gbc.anchor = GridBagConstraints.EAST;
this.add(new JLabel("密码"),gbc);
gbc=new GridBagConstraints();
gbc.gridx=1;
gbc.gridy=1;
gbc.weightx=1;
gbc.weighty=1;
gbc.anchor = GridBagConstraints.WEST;
this.add(userPwd,gbc);
//设置密码确认
gbc=new GridBagConstraints();
gbc.gridx=0;
gbc.gridy=2;
gbc.weightx=1;
gbc.weighty=1;
gbc.anchor = GridBagConstraints.EAST;
this.add(new JLabel("密码确认"),gbc);
gbc=new GridBagConstraints();
gbc.gridx=1;
gbc.gridy=2;
gbc.weightx=1;
gbc.weighty=1;
gbc.anchor = GridBagConstraints.WEST;
this.add(userPwdSubmit,gbc);
//设置性别位置
gbc=new GridBagConstraints();
gbc.gridx=0;
gbc.gridy=3;
gbc.weightx=1;
gbc.weighty=1;
gbc.anchor = GridBagConstraints.EAST;
this.add(new JLabel("性别"),gbc);
gbc=new GridBagConstraints();
gbc.gridx=1;
gbc.gridy=3;
gbc.weightx=1;
gbc.weighty=1;
gbc.anchor = GridBagConstraints.WEST;
this.add(jp1,gbc);
jp1.add(man);
jp1.add(woman);
bg.add(woman);
bg.add(man);
//设置爱好区域
gbc=new GridBagConstraints();
gbc.gridx=0;
gbc.gridy=4;
gbc.weightx=1;
gbc.weighty=1;
gbc.anchor = GridBagConstraints.EAST;
this.add(new JLabel("爱好"),gbc);
gbc=new GridBagConstraints();
gbc.gridx=1;
gbc.gridy=4;
gbc.weightx=1;
gbc.weighty=1;
gbc.anchor = GridBagConstraints.WEST;
this.add(jp2,gbc);
jp2.add(hobby1);
jp2.add(hobby2);
jp2.add(hobby3);
//设置出生日期
gbc=new GridBagConstraints();
gbc.gridx=0;
gbc.gridy=5;
gbc.weightx=1;
gbc.weighty=1;
gbc.anchor = GridBagConstraints.EAST;
this.add(new JLabel("出生日期"),gbc);
gbc=new GridBagConstraints();
gbc.gridx=1;
gbc.gridy=5;
gbc.weightx=1;
gbc.weighty=1;
gbc.anchor = GridBagConstraints.WEST;
this.add(jp3,gbc);
jp3.add(year);
jp3.add(new JLabel("年"));
jp3.add(month);
jp3.add(new JLabel("月"));
jp3.add(day);
jp3.add(new JLabel("日"));
gbc=new GridBagConstraints();
gbc.gridx=0;
gbc.gridy=6;
gbc.weightx=1;
gbc.weighty=1;
gbc.anchor = GridBagConstraints.EAST;
this.add(new JLabel("个人简介"),gbc);
gbc=new GridBagConstraints();
gbc.gridx=1;
gbc.gridy=6;
gbc.anchor = GridBagConstraints.WEST;
this.add(intreduce,gbc);
this.add(new JScrollPane(intreduce),gbc);
// gbc=new GridBagConstraints();
// gbc.gridx=0;
// gbc.gridy=7;
// gbc.weightx=1;
// gbc.weighty=1;
// this.add(jp4,gbc);
// jp4.add(submit);
// jp4.add(reset);
gbc=new GridBagConstraints();
gbc.gridx=0;
gbc.gridy=7;
//gbc.anchor = GridBagConstraints.EAST;
this.add(submit,gbc);
// submit.addActionListener(my);
// submit.setActionCommand(submit);
gbc=new GridBagConstraints();
gbc.gridx=1;
gbc.gridy=7;
//gbc.anchor = GridBagConstraints.WEST;
this.add(reset,gbc);
// reset.addActionListener(my);
// reset.setActionCommand(reset);
}
private void initComponent() {
userName=new JTextField(10);
userPwd=new JPasswordField(10);
userPwdSubmit=new JPasswordField(10);
jp1=new JPanel();
jp2=new JPanel();
jp3=new JPanel();
jp4=new JPanel();
man=new JRadioButton("男");
woman =new JRadioButton("女");
bg=new ButtonGroup();
hobby1 =new JCheckBox("足球");
hobby2 =new JCheckBox("篮球");
hobby3 =new JCheckBox("排球");
String []yearDate=new String[53];
for(int i=0;i<yearDate.length;i++){
yearDate[i]=(i+1960)+"";
}
year=new JComboBox(yearDate);
String []monthDate=new String[12];
for(int i=0;i<monthDate.length;i++){
monthDate[i]=(i+1)+"";
}
month=new JComboBox(monthDate);
String []dayDate=new String[31];
for(int i=0;i<dayDate.length;i++){
dayDate[i]=(i+1)+"";
}
day =new JComboBox(dayDate);
intreduce=new JTextArea(12,15);
submit=new JButton("确认");
reset=new JButton("重置");
}
}
-------------------------------------------------------------------------------------------------
//主函数测试
public class Test {
public static void main(String []args){
new TestGridBagLayout();
}
}
------------------------------------------------------------------------
做了好久只做好了界面,功能还没实现。。界面如下: