java Swing寫學生資訊介面
學生資訊包括:學號,姓名,學院,專業(下拉列表),班級,籍貫,性別,愛好(多選),備註 。
程式碼:
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();
}
}
介面截圖: