学生信息包括:学号,姓名,学院,专业(下拉列表),班级,籍贯,性别,爱好(多选),备注 。
代码:
import java.awt.event.ActionEvent;
public class T_one extends JFrame {
//public class T_one extends JFrame implements ItemListener{
JTextField jTextField,name1,academy,class1,others;//定义文本框组件
JPasswordField jPasswordField,number;//定义密码框组件
JLabel jLabel1,jLabel2,jLabel3,jLabel4,jLabel5,jLabel6,jLabel7,jLabel8,jLabel9,jLabel10,jLabel11,jLabel12;//面板名称
JPanel jp1,jp2,jp3,jp4,jp5,jp6,jp7,jp8,jp9,jp10,jp11,jp12,jp13;//面板
JButton jb1,jb2; //创建按钮
JRadioButton love1,love2,love3,love4,love5,sex1,sex2;
ButtonGroup jRadioGroup1;//单选框
JComboBox major1;
JComboBox place1,place2;
private static final long serialVersionUID = 1L;
private JComboBox college;
private JComboBox major;
private Map<Integer, Vector<String>> map = new HashMap<Integer, Vector<String>>();
private JComboBox f;//第一个下拉框,如果值改变,则改变第二个下拉框的值
private JComboBox s;//第二个下拉框
private Object item;//缓存下拉框当前已选择的值
public T_one(){
jTextField = new JTextField(12);
jPasswordField = new JPasswordField(12);
name1=new JTextField(12);
academy=new JTextField(12);
class1=new JTextField(12);
number=new JPasswordField(12);
major1=new JComboBox();
major1.addItem("软件工程");
major1.addItem("信息安全");
major1.addItem("计科");
major1.setBounds(40, 150, 55, 20);
others=new JTextField(12);
// place1=new JComboBox();
place2=new JComboBox();
String province[]= {"湖北","广东","江西"};
place1=new JComboBox(province);
place2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "" }));
place1.addActionListener(new java.awt.event.ActionListener(){
public void itemStateChanged(ItemEvent e) {
int index = place1.getSelectedIndex();
switch (index) {
case 0:
place2.removeAllItems();
place2.addItem("武汉");
place2.addItem("武穴");
break;
case 1:
place2.removeAllItems();
place2.addItem("珠海");
place2.addItem("深圳");
break;
case 2:
place2.removeAllItems();
place2.addItem("南昌");
place2.addItem("新余");
break;
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
});
jLabel1 = new JLabel("用户名");
jLabel2 = new JLabel("密码");
jLabel3=new JLabel("姓名");
jLabel4=new JLabel("学号");
jLabel5=new JLabel("学院");
jLabel6=new JLabel("班级");
jLabel7=new JLabel("爱好");
jLabel8=new JLabel("专业");
jLabel9=new JLabel("性别");
jLabel10=new JLabel("备注");
jLabel11=new JLabel("籍贯");
//jLabel12=new JLabel("学校");
love1=new JRadioButton("唱歌");
love2=new JRadioButton("跳舞");
love3=new JRadioButton("画画");
love4=new JRadioButton("看书");
love5=new JRadioButton("其他");
sex1=new JRadioButton("男");
sex2=new JRadioButton("女");
jRadioGroup1=new ButtonGroup();
jRadioGroup1.add(sex1);
jRadioGroup1.add(sex2);
jb1 = new JButton("确定");
jb1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){
//处理代码
String username, psd;
username = jTextField.getText();
psd = new String(jPasswordField.getPassword());
if (username.equals("wustzz") && psd.equals("123456"))
JOptionPane.showMessageDialog(null,this, "欢迎"+username,JOptionPane.INFORMATION_MESSAGE);
else{
JOptionPane.showMessageDialog(null,this, "用户名或密码错!",JOptionPane.INFORMATION_MESSAGE );
}
}
});
jb2 = new JButton("取消");
jp1 = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();
jp4 = new JPanel();
jp5 = new JPanel();
jp6 = new JPanel();
jp7 = new JPanel();
jp8 = new JPanel();
jp9 = new JPanel();
jp10=new JPanel();
jp11=new JPanel();
jp12=new JPanel();
//jp13=new JPanel();
//设置布局
this.setLayout(new GridLayout(5,1));
jp1.add(jLabel1);
jp1.add(jTextField);//第一块面板添加用户名和文本框
jp2.add(jLabel2);
jp2.add(jPasswordField);//第二块面板添加密码和密码输入框
jp4.add(jLabel3);
jp4.add(name1);
jp5.add(jLabel4);
jp5.add(number);
jp6.add(jLabel5);
jp6.add(academy);
jp7.add(jLabel6);
jp7.add(class1);
jp8.add(jLabel7);
jp8.add(love1);
jp8.add(love2);
jp8.add(love3);
jp8.add(love4);
jp8.add(love5);
jp9.add(jLabel8);
jp9.add(major1);
jp10.add(jLabel9);
jp10.add(sex1);
jp10.add(sex2);
jp11.add(jLabel10);
jp11.add(others);
jp12.add(jLabel11);
jp12.add(place1);
jp12.add(place2);
// jp13.add(jLabel12);
// jp13.add(college);
//jp13.add(major);
jp3.add(jb1);
jp3.add(jb2); //第三块面板添加确认和取消
// jp3.setLayout(new FlowLayout()); //因为JPanel默认布局方式为FlowLayout,所以可以注销这段代码.
this.add(jp1);
this.add(jp2);
this.add(jp3);//将三块面板添加到登陆框上面
this.add(jp4);
this.add(jp5);
this.add(jp6);
this.add(jp7);
this.add(jp8);
this.add(jp9);
this.add(jp10);
this.add(jp11);
this.add(jp12);
//设置显示
this.setSize(800, 200);
//this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setTitle("学生基本信息");
}
public static void main(String[] args) {
new T_one();
}
}
界面截图: