LOVO封装的表格的使用

116 篇文章 22 订阅

随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(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;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

u010142437

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值