集合

来源:互联网 发布:js面向对象什么意思 编辑:程序博客网 时间:2024/06/04 18:01


                    (第一部分)

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;


import javax.swing.JFrame;
import javax.swing.JOptionPane;


import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoTable;
import com.lovo.netCRM.component.LovoTxt;


public class MainFrame extends JFrame {
// 创建一个表格对象,第一个参数为表格加入容器,第二个参数为表头列表
// 第三个参数为每个表头对应相关对象的属性名
private LovoTable table = new LovoTable(this, new String[] { "学号", "姓名",
"性别", "成绩" }, new String[] { "code", "name", "sex", "grade" }, null);


public static List<Student> list = new ArrayList<Student>();
public  static  List<Student> currenList;
static {
list.add(new Student(1, "张三", "男", 90));
list.add(new Student(2, "李四", "男", 62));
list.add(new Student(3, "王五张", "女", 20));
list.add(new Student(4, "李家人", "男", 88));
list.add(new Student(5, "张田七", "男", 42));


}


private LovoTxt nameTxt = new LovoTxt("姓名", 30, 230, this);


public MainFrame() {
this.setLayout(null);


// 设置表格的大小和位置
table.setSizeAndLocation(50, 50, 300, 150);
currenList=list;
table.updateLovoTable(currenList);


LovoButton 添加Button = new LovoButton("添加", 50, 250, this);
LovoButton 删除Button = new LovoButton("删除", 150, 250, this);
LovoButton 修改Button = new LovoButton("修改", 250, 250, this);
LovoButton 查找Button = new LovoButton("查找", 350, 250, this);


添加Button.addActionListener(new ActionListener() {


@Override
public void actionPerformed(ActionEvent arg0) {
MainFrame.this.dispose();
AddFrame a = new AddFrame();


}
});


删除Button.addActionListener(new ActionListener() {


@Override
public void actionPerformed(ActionEvent arg0) {
int row = table.getSelectedRow();
if (row == -1) {
JOptionPane.showMessageDialog(null, "请选中行");
return;
}
int x = JOptionPane.showConfirmDialog(null, "真的要删吗?");
if (x == 0) {
// 删除选中行
currenList.remove(row);
// 更新表格
table.updateLovoTable(currenList);
}


}


});
修改Button.addActionListener(new ActionListener() {


@Override
public void actionPerformed(ActionEvent arg0) {
int row = table.getSelectedRow();
if (row == -1) {
JOptionPane.showMessageDialog(null, "请选中行");
return;


}
Student s = currenList.get(row);
MainFrame.this.dispose();
修改Frame f = new 修改Frame(s);
}


});

查找Button.addActionListener(new ActionListener() {


@Override
public void actionPerformed(ActionEvent arg0) {
List<Student> newList = new ArrayList<Student>();
String name = nameTxt.getText();
for (Student s : list) {
if (s.getName().indexOf(name) != -1) {
newList.add(s);


}
}
table.updateLovoTable(newList);
}
});


this.setSize(500, 400);
this.setVisible(true);
this.setDefaultCloseOperation(3);
this.setLocationRelativeTo(null);
}


public static void main(String[] args) {
MainFrame s = new MainFrame();
}


}

                          ( 第二部分)

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.JFrame;


import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoTxt;


public class AddFrame extends JFrame {
private LovoTxt codeTxt = new LovoTxt("学号", 30, 50, this);
private LovoTxt nameTxt = new LovoTxt("姓名", 30, 100, this);
private LovoTxt sexTxt = new LovoTxt("性别", 30, 150, this);
private LovoTxt gradeTxt = new LovoTxt("成绩", 30, 200, this);


public AddFrame() {
this.setLayout(null);


LovoButton addButton = new LovoButton("添加", 130, 300, this);
LovoButton delButton = new LovoButton("删除", 200,300, this);
LovoButton updButton = new LovoButton("修改", 270,300, this);


addButton.addActionListener(new ActionListener() {


@Override
public void actionPerformed(ActionEvent arg0) {
// 将文本框信息封装成学生对象
Student s = new Student();
s.setCode(Integer.parseInt(codeTxt.getText()));
s.setName(nameTxt.getText());
s.setSex(sexTxt.getText());
s.setGrade(Integer.parseInt(gradeTxt.getText()));


MainFrame.list.add(s);


AddFrame.this.dispose();
MainFrame a = new MainFrame();


}
});


this.setSize(300, 400);
this.setVisible(true);
this.setDefaultCloseOperation(3);
this.setLocationRelativeTo(null);


}


}



           (第三部分)

public class Student {
private int code;
private String name;
private String sex;
private int grade;


public Student() {


}


public Student(int code, String name, String sex, int grade) {
super();
this.code = code;
this.name = name;
this.sex = sex;
this.grade = grade;
}


public int getCode() {
return code;
}


public void setCode(int code) {
this.code = code;
}


public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


public int getGrade() {
return grade;
}


public void setGrade(int grade) {
this.grade = grade;
}


public String getSex() {
return sex;
}


public void setSex(String sex) {
this.sex = sex;
}
}

      (第四部分)

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.JFrame;


import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoLabel;
import com.lovo.netCRM.component.LovoTxt;


public class 修改Frame extends JFrame {


private LovoLabel codeLabel = new LovoLabel("学号", 30, 50, this);
private LovoLabel nameLabel = new LovoLabel("姓名", 30, 100, this);
private LovoLabel sexLabel = new LovoLabel("性别", 30, 150, this);
private LovoTxt gradeTxt = new LovoTxt("成绩", 30, 200, this);
private Student s;


public 修改Frame(Student st) {
this.s = st;
this.setLayout(null);
this.init();


LovoButton addButton = new LovoButton("修改", 100, 230, this);
addButton.addActionListener(new ActionListener() {


@Override
public void actionPerformed(ActionEvent arg0) {
s.setGrade(Integer.parseInt(gradeTxt.getText()));
修改Frame.this.dispose();
MainFrame m = new MainFrame();


}
});


this.setSize(300, 300);
this.setVisible(true);
this.setDefaultCloseOperation(3);
this.setLocationRelativeTo(null);
}


private void init() {
codeLabel.setText(s.getCode() + "");
nameLabel.setText(s.getName());
sexLabel.setText(s.getSex());
gradeTxt.setText(s.getGrade() + "");


}


}


0 0
原创粉丝点击