随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)
package cn;
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.LovoTable;
import com.lovo.userObj.LovoButton;
import com.lovo.userObj.LovoText;
public class TableFrame extends JFrame{
private LovoText codeTxt=new LovoText("学号",50,50,this);
private LovoText nameTxt=new LovoText("姓名",50,100,this);
private LovoText gradeTxt=new LovoText("成绩",50,150,this);
//第一个参数为属性名,第二个参数为表头,第三个参数表上实体类中哪个属性对应表头的指定列(LovoTable是自己封装的一个类)
private LovoTable table=new LovoTable(this,new String[]{"学号","姓名","成绩"},
new String[]{"code","name","grade"},null);
private List<Student> list =new ArrayList<Student>();
public TableFrame(){
this.setLayout(null);
list.add(new Student(1,"夏侯惇",40));
list.add(new Student(2,"张辽",91));
list.add(new Student(3,"徐晃",92));
list.add(new Student(4,"许褚",93));
list.add(new Student(4,"典韦",53));
//更新表格数据
table.updateLovoTable(list);
LovoButton addButton=new LovoButton("添加",50,200,this);
LovoButton delButton=new LovoButton("删除",150,200,this);
LovoButton updateButton=new LovoButton("修改",250,200,this);
LovoButton findButton=new LovoButton("查找",350,200,this);
//设置位置和大小
table.setSizeAndLocation(270, 50, 200, 140);
addButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
add();
clear();
}});
delButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
delete();
}});
findButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
find();
clear();
}});
updateButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
update();
clear();
}});
this.setSize(600,400);
this.setVisible(true);
this.setDefaultCloseOperation(3);
this.setLocationRelativeTo(null);
}
/**
* 添加学生信息
*/
public void add(){
//将学生对象加入集合
list.add(getStudent());
//更新表格组件
table.updateLovoTable(list);
}
/**
* 删除学生信息
*/
public void delete(){
int index=table.getSelectedRow();
if(index!=-1){
list.remove(index);
table.updateLovoTable(list);
}
else{
JOptionPane.showMessageDialog(null, "请选中行");
}
}
/**
* 查找不及格的学生信息
*/
public void find(){
List<Student> myList=new ArrayList<Student>();
int m=list.size();
for(int i=0;i<m;i++){
Student s=list.get(i);
if(s.getGrade()<60){
myList.add(s);
}
}
table.updateLovoTable(myList);
}
/**
* 修改学生信息
*/
public void update(){
int index=table.getSelectedRow();
if(index!=-1){
list.set(index,getStudent());
table.updateLovoTable(list);
}
else{
JOptionPane.showMessageDialog(null, "请选中要修改的行");
}
}
/**
* 得到页面学生信息
* @return 学生对象
*/
private Student getStudent(){
Student s=new Student();
s.setCode(Integer.parseInt(codeTxt.getText()));
s.setGrade(Double.parseDouble(gradeTxt.getText()));
s.setName(nameTxt.getText());
return s;
}
/**
* 清除页面数据
*/
public void clear(){
codeTxt.setText(null);
nameTxt.setText(null);
gradeTxt.setText(null);
}
public static void main(String[] args) {
TableFrame t=new TableFrame();
}
}
class Student {
private int code;
private String name;
private double grade;
public Student() {
super();
}
public Student(int code, String name, double grade) {
super();
this.code = code;
this.name = name;
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 double getGrade() {
return grade;
}
public void setGrade(double grade) {
this.grade = grade;
}
}