随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)
package cn;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;
import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoTable;
import com.lovo.netCRM.component.LovoTxt;
public class TeacherFrame extends JFrame {
/** 姓名文本框 */
private LovoTxt nameTxt = new LovoTxt("姓名", 50, 50, this);
/** 学历文本框 */
private LovoTxt eduTxt = new LovoTxt("学历", 50, 100, this);
/** 职称文本框 */
private LovoTxt jobTxt = new LovoTxt("职称", 50, 150, this);
/** 值文本框 */
private JTextField jtxt = new JTextField();
private LovoTable table = new LovoTable(this, new String[] { "姓名", "学历",
"职称" }, new String[] { "name", "edu", "job" }, "id");
/** 老师数据查询对象 */
private TeacherDao dao = new TeacherDao();
public TeacherFrame() {
this.setLayout(null);
table.setSizeAndLocation(300, 50, 220, 150);
JPanel jp = new JPanel();
jp.setLayout(null);
Border b = BorderFactory.createLineBorder(Color.blue);
Border border = BorderFactory.createTitledBorder(b, "查找");
jp.setBorder(border);
jp.setBounds(380, 220, 200, 100);
this.add(jp);
jtxt.setBounds(110, 30, 80, 20);
jp.add(jtxt);
LovoButton addButton = new LovoButton("添加", 50, 220, this);
LovoButton delButton = new LovoButton("删除", 150, 220, this);
LovoButton updateButton = new LovoButton("修改", 250, 220, this);
LovoButton findButton = new LovoButton("查找", 100, 60, jp);
load();
findButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
find();
}
});
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
add();
}
});
delButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
del();
}
});
updateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
update();
}
});
this.setSize(600, 400);
this.setVisible(true);
this.setDefaultCloseOperation(3);
this.setLocationRelativeTo(null);
this.setResizable(false);
}
/**
* 将数据库数据加载到表格中
*/
public void load() {
// 查询数据库得到全部数据
List<Teacher> list = dao.findAll();
table.updateLovoTable(list);
}
/**
* 将文本框数据封装插入数据库
*/
public void add() {
// 创建老师对象,并封装文本框数据
Teacher t = new Teacher();
t.setName(nameTxt.getText());
t.setEdu(eduTxt.getText());
t.setJob(jobTxt.getText());
// 完成数据库的添加
dao.add(t);
List<Teacher> list = dao.findAll();
// 重新查询所有记录,更新表格
table.updateLovoTable(list);
}
/**
* 删除选中行的数据
*/
public void del() {
// 得到选中行的id
int id = table.getKey();
if (id == -1) {
JOptionPane.showMessageDialog(null, "请选中行");
return;
}
// 删除记录
dao.del(id);
List<Teacher> list = dao.findAll();
table.updateLovoTable(list);
}
/**
* 修改数据
*/
public void update() {
int id = table.getKey();
if (id == -1) {
JOptionPane.showMessageDialog(null, "请选中行");
return;
}
dao.update(id, eduTxt.getText(), jobTxt.getText());
List<Teacher> list = dao.findAll();
table.updateLovoTable(list);
}
/**
* 按姓名模糊查询
*/
public void find() {
// 得到文本框的内容
String value = jtxt.getText();
List<Teacher> list = null;
list = dao.findByName(value);
table.updateLovoTable(list);
}
public static void main(String[] args) {
TeacherFrame m = new TeacherFrame();
}
}