package com.view;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;
import com.db.SqlHelper;
import com.view.AddEmployeeView.A;
public class EmployeeManagerView extends JPanel {
private DefaultTableModel dtm = new DefaultTableModel();
private JTable empJtable = new JTable(dtm);
private JScrollPane centerJscrollPane = new JScrollPane(empJtable);
private JPanel bottomAllJpane = new JPanel();
private JPanel bottonPageJpanel = new JPanel(){
public void paintComponent(Graphics g) {
super.paintComponent(g);
ImageIcon icon;
Image img;
icon = new ImageIcon("E:\\图片册\\pu.jpg");// 背景图片
img = icon.getImage();
g.drawImage(img, 0, 0,this.getWidth(), this.getHeight(), this);
}
};
private JButton firstButton, lastButton, preButton, nextButton;
private JLabel pageInfoLagel;
private JPanel bottomJpanel = new JPanel(){
public void paintComponent(Graphics g) {
super.paintComponent(g);
ImageIcon icon;
Image img;
icon = new ImageIcon("E:\\图片册\\puu.jpg");// 背景图片
img = icon.getImage();
g.drawImage(img, 0, 0,this.getWidth(), this.getHeight(), this);
}
};
private JButton newButton, updateButton, deleteButton;
private JPanel topJpanel = new JPanel(){
public void paintComponent(Graphics g) {
super.paintComponent(g);
ImageIcon icon;
Image img;
icon = new ImageIcon("E:\\图片册\\pu.jpg");// 背景图片
img = icon.getImage();
g.drawImage(img, 0, 0,this.getWidth(), this.getHeight(), this);
}
};
private JLabel filterUserNameJLable = new JLabel("员工id/员工姓名");
private JTextField filterUsernameJTexfd = new JTextField(12);
private JLabel isAdminJLabel = new JLabel("身份(管理员/普通员工)");
private JTextField isAdminJTexfd = new JTextField(12);
private JButton searchBtn = new JButton("查找");
private ArrayList<Object[]> datas = null;
int pageSize = 3; // 每页显示条数
int dataSum = 0; // 总的数据条数
int pageCount = 0; // 总页数
int currntPage = 0; // 当前页数
public EmployeeManagerView() {
setLayout(new BorderLayout());
add(centerJscrollPane, BorderLayout.CENTER);
dtm.addColumn("员工id");
dtm.addColumn("名称");
dtm.addColumn("姓别");
dtm.addColumn("入职日期");
dtm.addColumn("年龄");
dtm.addColumn("是否管理");
try {
dataSum = getDataCount();
} catch (SQLException e1) {
e1.printStackTrace();
}
bottomAllJpane.setLayout(new BorderLayout());
bottomAllJpane.setPreferredSize(new Dimension(900,240));
bottomAllJpane.add(bottonPageJpanel, BorderLayout.CENTER);
firstButton = new JButton("首页");
firstButton.setBackground(Color.green);
lastButton = new JButton("尾页");
lastButton.setBackground(Color.green);
preButton = new JButton("上一页");
preButton.setBackground(Color.green);
nextButton = new JButton("下一页");
nextButton.setBackground(Color.green);
pageInfoLagel = new JLabel(" ");
pageInfoLagel.setOpaque(true);
pageInfoLagel.setFont(new Font("Dialog",1,18));
bottonPageJpanel.add(firstButton);
bottonPageJpanel.add(preButton);
bottonPageJpanel.add(nextButton);
bottonPageJpanel.add(lastButton);
bottonPageJpanel.add(pageInfoLagel);
firstButton.addActionListener(new PageClick());
lastButton.addActionListener(new PageClick());
preButton.addActionListener(new PageClick());
nextButton.addActionListener(new PageClick());
//refreshJTable();
newButton = new JButton("新增用户");
newButton.setBackground(Color.green);
newButton.setFont(new Font("Dialog",1,15));
updateButton = new JButton("修改用户");
updateButton.setBackground(Color.green);
updateButton.setFont(new Font("Dialog",1,15));
deleteButton = new JButton("删除用户");
deleteButton.setBackground(Color.green);
deleteButton.setFont(new Font("Dialog",1,15));
bottomJpanel.add(newButton);
bottomJpanel.add(updateButton);
bottomJpanel.add(deleteButton);
bottomJpanel.setPreferredSize(new Dimension(900,80));
bottomAllJpane.add(bottomJpanel, BorderLayout.SOUTH);
add(bottomAllJpane, BorderLayout.SOUTH);
newButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
AddEmployeeView aev = new AddEmployeeView(EmployeeManagerView.this);
}
});
deleteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// deleteEmployee();
comfirmDelete();
}
});
updateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int selectIndex = empJtable.getSelectedRow();
if(selectIndex == -1){
JOptionPane.showConfirmDialog(null, "请先选中一行","提示",JOptionPane.WARNING_MESSAGE);
}else{
Object[] row = datas.get(selectIndex);
String eid = (String) row[0];
new UpdateEmployeeView(eid, EmployeeManagerView.this);
}
}
});
topJpanel.add(filterUserNameJLable);
filterUserNameJLable.setFont(new Font("Dialog",1,13));
topJpanel.add(filterUsernameJTexfd);
topJpanel.add(isAdminJLabel);
isAdminJLabel.setFont(new Font("Dialog",1,13));
topJpanel.add(isAdminJTexfd);
topJpanel.add(searchBtn);
searchBtn.setFont(new Font("Dialog",1,13));
searchBtn.setBackground(Color.green);
add(topJpanel, BorderLayout.NORTH);
searchBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
getFilter();
}
});
pageCount = dataSum / pageSize;
if (dataSum % pageSize >0) {
pageCount ++;
}
refreshJTable(currntPage + 1, pageSize);
}
/**
* 查找所有员工
*
* @return
* @throws SQLException
*/
private ArrayList<Object[]> getEmployeesByDb(int begin, int size) throws SQLException {
ArrayList<Object[]> employeeslist = new ArrayList<Object[]>();
// 查询sql语句
String sql = "select * from EmployeeInfo limit " + begin + "," + size + "";
// 查询数据
ResultSet result = new SqlHelper().query(sql, new String[] {});
if (result != null) {
while (result.next()) {
Object rows[] = new Object[6];
rows[0] = result.getString(1);
rows[1] = result.getString(3);
rows[2] = result.getString(4).equals("0") ? "女" : "男";
rows[3] = result.getString(5);
rows[4] = result.getString(6);
rows[5] = result.getString(7).equals("0") ? "普通员工" : "管理员";
employeeslist.add(rows);
}
} else {
JOptionPane.showConfirmDialog(null, "没有查询到员工", "提示", JOptionPane.WARNING_MESSAGE);
}
return employeeslist;
}
private int getDataCount() throws SQLException {
int count = 0;
String sql = "select count(*) as count from EmployeeInfo";
// 查询数据
ResultSet result = new SqlHelper().query(sql, new String[] {});
if (result != null && result.next()) {
count = result.getInt(1);
}
return count;
}
@SuppressWarnings("unused")
private void printPageInfo(){
String pageInfo = "当前页数"+(currntPage+1) +" /"+" 总页数 "+pageCount;
pageInfoLagel.setText(pageInfo);
}
/**
* 刷新表格数据
*/
public void refreshJTable(int begin, int size) {
dtm.setNumRows(0);
try {
datas = getEmployeesByDb(begin, size);
for (int i = 0; i < datas.size();
- 1
- 2
前往页