package database;
import java.util.Vector;
import javax.swing.table.AbstractTableModel;
public class Empmodel extends AbstractTableModel{
Vector<String> colums;
Vector<Vector> rows;
//写一个方法,用于查询需要的显示的人事信息
public void query(){
this.colums=new Vector<String>();
this.colums.add("员工号");
this.colums.add("姓名");
this.colums.add("性别");
this.colums.add("职位");
rows=new Vector<Vector>();
for (int i=1;i<=10;i++){
Vector<String> tem=new Vector<String>();
tem.add(i+"");
tem.add("luozt"+i+"");
tem.add("男");
tem.add("QA");
rows.add(tem);
}
}
@Override
public String getColumnName(int column) {
// TODO Auto-generated method stub
return this.colums.get(column).toString();
}
@Override
public int getRowCount() {
// TODO Auto-generated method stub
return this.rows.size();
}
@Override
public int getColumnCount() {
// TODO Auto-generated method stub
return this.colums.size();
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
// TODO Auto-generated method stub
return ((Vector)rows.get(rowIndex)).get(columnIndex);
}
}
//调用上面的JTable
package UI;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import database.Empmodel;
import tools.mytool;
public class EmpInfo extends JPanel{
public static void main(String[] args) {
// TODO Auto-generated method stub
EmpInfo emp=new EmpInfo();
}
JPanel jp1,jp2,jp3,jp4,jp5;
JLabel jp1_label,jp3_label;
JTextField jp1_jtf;
JButton jp1_button,jp4_button1,jp4_button2,jp4_button3,jp4_button4;
JTable jtb;
JScrollPane jsp;
public EmpInfo(){
jp1=new JPanel(new FlowLayout(FlowLayout.CENTER));
jp1_label=new JLabel("请输入姓名(员工或职位)");
jp1_label.setFont(mytool.f2);
jp1_jtf=new JTextField(20);
jp1_button=new JButton("查询");
jp1_button.setFont(mytool.f3);
jp1.add(jp1_label);
jp1.add(jp1_jtf);
jp1.add(jp1_button);
//center
Empmodel emp=new Empmodel();
emp.query();
jtb=new JTable(emp);
jp2=new JPanel(new BorderLayout());
//jtb 要放在JScrollPane里否则表头看不见
jsp=new JScrollPane(jtb);
jp2.add(jsp);
jp2.setBorder(BorderFactory.createTitledBorder("人事信息"));
jp3=new JPanel(new FlowLayout(FlowLayout.LEFT));
jp3_label=new JLabel("总记录是10条");
jp3_label.setFont(mytool.f3);
jp3.add(jp3_label);
jp4=new JPanel(new FlowLayout(FlowLayout.RIGHT));
jp4_button1=new JButton("详细信息");
jp4_button1.setFont(mytool.f3);
jp4_button2=new JButton("修改");
jp4_button2.setFont(mytool.f3);
jp4_button3=new JButton("添加");
jp4_button3.setFont(mytool.f3);
jp4_button4=new JButton("删除");
jp4_button4.setFont(mytool.f3);
jp4.add(jp4_button1);
jp4.add(jp4_button2);
jp4.add(jp4_button3);
jp4.add(jp4_button4);
jp5=new JPanel(new BorderLayout());
jp5.add(jp3,"West");
jp5.add(jp4,"East");
this.setLayout(new BorderLayout());
this.add(jp1,"North");
this.add(jp2,"Center");
this.add(jp5,"South");
//this.setBackground(Color.pink);
this.setVisible(true);
}
}