标签:
制作一个界面完成员工信息的添加、显示和查找
实现步骤:
1、制作界面
2、建立员工类,包含姓名、年龄、性别、电话4个私有属性,加上get和set方法作为访问器和修改器
3、在事件类中加入一个员工数组的属性,用于保存员工信息,加入一个number的属性,用于记录员工的人数。
4、当单击添加时,从四个文本框中得到信息,并封装成员工对象,然后将员工对象加入员工数组,人数加1。如果超出数组的长度,添加按钮变灰,阻止用户继续添加。
5、当单击显示时,弹出对话框,显示现有员工的信息。
6、在姓名文本框中输入用户姓名,单击查找按钮,将该用户的姓名、年龄、性别、电话,显示在文本框中。
1、员工类
1 public class Staff { 2 private String name ; 3 private String age ; 4 private String sex ; 5 private String tel ; 6 7 public String getName() { 8 return name; 9 } 10 public void setName(String name) { 11 this.name = name; 12 } 13 public String getAge() { 14 return age; 15 } 16 public void setAge(String age) { 17 this.age = age; 18 } 19 public String getSex() { 20 return sex; 21 } 22 public void setSex(String sex) { 23 this.sex = sex; 24 } 25 public String getTel() { 26 return tel; 27 } 28 public void setTel(String tel) { 29 this.tel = tel; 30 } 31 32 }
2、主界面
1 import java.awt.event.ActionEvent; 2 import java.awt.event.ActionListener; 3 4 import javax.swing.JFrame; 5 import javax.swing.JOptionPane; 6 7 import JLabelAndJButton.MyButton; 8 import JLabelAndJButton.MyTxt; 9 10 public class MainJFrame extends JFrame{ 11 private MyTxt workName = new MyTxt("姓名", 60, 40, this); 12 private MyTxt workAge = new MyTxt("年龄", 60, 100, this); 13 private MyTxt workSex = new MyTxt("性别", 60, 160, this); 14 private MyTxt workTel = new MyTxt("电话", 60, 220, this); 15 16 private MyButton addButton = new MyButton("添加", 40, 280, this); 17 18 static int num = 0; 19 public static Staff [] workers = new Staff[2]; 20 21 public MainJFrame(){ 22 this.setTitle("我的窗体"); 23 this.setLayout(null); 24 MyButton showButton = new MyButton("显示", 140, 280, this); 25 MyButton findButton = new MyButton("查找", 240, 280, this); 26 27 /**添加*/ 28 addButton.addActionListener(new ActionListener() { 29 30 @Override 31 public void actionPerformed(ActionEvent arg0) { 32 addnum (); 33 } 34 }); 35 /**显示*/ 36 showButton.addActionListener(new ActionListener() { 37 @Override 38 public void actionPerformed(ActionEvent e) { 39 shownum(); 40 } 41 }); 42 /**查找*/ 43 findButton.addActionListener(new ActionListener() { 44 @Override 45 public void actionPerformed(ActionEvent e) { 46 findnum(); 47 } 48 }); 49 50 this.setSize(400, 400); 51 this.setVisible(true); 52 this.setDefaultCloseOperation(3); 53 this.setLocationRelativeTo(null); 54 55 } 56 /**添加方法*/ 57 public void addnum (){ 58 Staff worker = new Staff(); 59 60 worker.setName(workName.getText()); 61 worker.setAge(workAge.getText()); 62 worker.setSex(workSex.getText()); 63 worker.setTel(workTel.getText()); 64 workers[num] = worker; 65 num++; 66 if (num == workers.length){ 67 JOptionPane.showMessageDialog(null, "添加成功"); 68 addButton.setEnabled(false); 69 }else { 70 JOptionPane.showMessageDialog(null, "添加成功"); 71 } 72 workName.setText(""); 73 workAge.setText(""); 74 workSex.setText(""); 75 workTel.setText(""); 76 } 77 /**显示方法*/ 78 public void shownum (){ 79 String s = "姓名 "+"年龄 "+"性别 "+"电话\n"; 80 for(int i = 0 ;i <num ;i++){ 81 s+= workers[i].getName()+ " " + workers[i].getAge()+ " " + 82 workers[i].getSex()+ " " + workers[i].getTel()+"\n"; 83 } 84 JOptionPane.showMessageDialog(null, s); 85 } 86 /**查找方法*/ 87 public void findnum(){ 88 int m = -1; 89 90 for(int i = 0;i< num;i++){ 91 if((workName.getText()).equals(workers[i].getName())){ 92 m=i; 93 } 94 } 95 96 if( m ==-1){ 97 JOptionPane.showMessageDialog(null, "找不到此人"); 98 }else { 99 workAge.setText(workers[m].getAge()); 100 workSex.setText(workers[m].getSex()); 101 workTel.setText(workers[m].getTel()); 102 } 103 104 } 105 108 public static void main(String[] args) { 109 MainJFrame meun = new MainJFrame(); 110 111 } 112 113 }
标签:
原文地址:http://www.cnblogs.com/zzyytt/p/5897802.html