标签:
class Employee {
private String name;
private int age;
private String gender;
private String telNumber;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getTelNumber() {
return telNumber;
}
public void setTelNumber(String telNumber) {
this.telNumber = telNumber;
}
@Override
public String toString() {
return "Employee [name=" + name + ", age=" + age + ", gender=" + gender
+ ", telNumber=" + telNumber + "]";
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class EmployeeFrame extends JFrame{
private MyTxt nameTxt = new MyTxt("姓名", 50, 50, this);
private MyTxt ageTxt = new MyTxt("年龄", 50, 100, this);
private MyTxt genderTxt = new MyTxt("性别", 50, 150, this);
private MyTxt telTxt = new MyTxt("电话", 50, 200, this);
private int number;
private Employee[] em = new Employee[10];
private MyButton addButton = new MyButton("添加", 50, 250, this);
public EmployeeFrame(){
this.setLayout(null);
MyButton showButton = new MyButton("显示", 140, 250, this);
showButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String s ="姓名 年龄 性别 电话\n";
for(int i = 0; i < number; i++){
s += (em[i].getName()+" "+em[i].getAge()+" "+em[i].getGender()+" "+em[i].getTelNumber()+"\n");
}
JOptionPane.showMessageDialog(null, s);
}
});
MyButton findButton = new MyButton("查找", 230, 250, this);
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
boolean isLand = verify();
if(isLand == false){
return;
}
Employee s = new Employee();
s.setAge(Integer.parseInt(ageTxt.getText()));
s.setName(nameTxt.getText());
s.setGender(genderTxt.getText());
s.setTelNumber(telTxt.getText());
em[number] = s;
number++;
JOptionPane.showMessageDialog(null, "添加成功");
nameTxt.setText("");
ageTxt.setText("");
genderTxt.setText("");
telTxt.setText("");
if(number >= em.length){
addButton.setEnabled(false);
}
}
});
findButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e1) {
Employee d = getEmployee();
if(d == null){
JOptionPane.showMessageDialog(null, "查无此人");
return;
}
}
});
this.setSize(350,400);
this.setVisible(true);
this.setTitle("信息登记");
this.setDefaultCloseOperation(3);
this.setLocationRelativeTo(null);
}
public Employee getEmployee(){
for(int i = 0; i < number; i++){
if(nameTxt.getText().equals(em[i].getName())){
ageTxt.setText(em[i].getAge()+"");
genderTxt.setText(em[i].getGender());
telTxt.setText(em[i].getTelNumber());
return em[i];
}
}
return null;
}
public boolean verify(){
String info = "";
if(nameTxt.getText().matches("[a-zA-Z\\u4e00-\\u9fa5]{2,30}")==false){
info += "姓名为两位及以上的汉字或字母\n";
}
if(ageTxt.getText().matches("\\d{2}")==false){
info += "年龄为两位数字\n";
}
if(genderTxt.getText().matches("(男|女)")==false){
info += "性别为男或女\n";
}
if(telTxt.getText().matches("1[358]\\d{9}")==false){
info += "电话号码为13、15、18开头的十一位数字\n";
}
if(info.length()!= 0){
JOptionPane.showMessageDialog(null, info);
return false;
}
return true;
}
public static void main(String[] args){
EmployeeFrame ef = new EmployeeFrame();
}
}
标签:
原文地址:http://www.cnblogs.com/tcc89/p/5402885.html