JAVA製作的人事管理系統
package pdsu.hrms.view;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.net.URL;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
public class HrMain extends JFrame implements TreeSelectionListener{
public static JSplitPane splitPane;
private JTree tree;//樹元件
private DefaultMutableTreeNode root;//樹的根結點
//定義歡迎面板
private JPanel pWelcome;
public HrMain() {
//關閉時退出程式
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(900,600);//設定大小
setLocationRelativeTo(null);//設定位置(相對於螢幕居中)
setResizable(false);//不能調大小
setTitle("人事管理系統");//設定標題
//設定視窗的圖示
//為什麼要將圖示放置在src下?因為程式內部的圖片放在src下時,生成可執行檔案時,圖片就包含在可執行檔案裡
//若是專案執行所需要的載入的像菜品的圖片時,則需要在和src同級下建立一個資料夾用於專門儲存相應圖片,生成可執行檔案時,需要將相應的圖片資料夾也放置出來
//獲取指定圖片的URL路徑
//(1)找到檔案位置
URL url=getClass().getClassLoader().getResource("url.png");
//通過Toolkit的getImage(url)方法獲取指定url下的圖片並儲存在一個Image下
//(2)載入圖片到記憶體
Image image=Toolkit.getDefaultToolkit().getImage(url);
//設定當前視窗的圖示
//(3)把圖片設定為視窗圖示
setIconImage(image);
init();//呼叫初始化方法
setVisible(true);//視窗可見性
}
public void init() {
//初始化分隔面板(設定為水平分隔)
//分隔面板將視窗劃分為左右兩部分,所以使用水平分隔
splitPane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
//設定分隔條位置(預設的分隔條左邊的位置太小,需重新設定)
splitPane.setDividerLocation(150);
//設定分隔條寬度(預設的分隔條寬度太寬,需重新設定)
splitPane.setDividerSize(2);
//設定分隔條變化的連續性(當內容過多時,需要調整分隔條的位置,這時就需要觀察分隔條的變化,以便於調整到合理的位置)
splitPane.setContinuousLayout(true);
//新增臨時元件,測試分隔面板
splitPane.setLeftComponent(new JButton("Left"));
splitPane.setRightComponent(new JButton("Right"));
//初始化樹選單
root=new DefaultMutableTreeNode("人事管理系統");
//建立一級結點物件
DefaultMutableTreeNode node1=new DefaultMutableTreeNode("基本資訊管理");
DefaultMutableTreeNode node2=new DefaultMutableTreeNode("人員調動管理");
DefaultMutableTreeNode node3=new DefaultMutableTreeNode("人員考核管理");
DefaultMutableTreeNode node4=new DefaultMutableTreeNode("勞資管理");
//新增基本資訊管理的子結點
node1.add(new DefaultMutableTreeNode("新增人員資訊"));
node1.add(new DefaultMutableTreeNode("修改人員資訊"));
node1.add(new DefaultMutableTreeNode("刪除人員資訊"));
node1.add(new DefaultMutableTreeNode("查詢人員資訊"));
node1.add(new DefaultMutableTreeNode("部門管理"));
//新增人員調動管理的子結點
node2.add(new DefaultMutableTreeNode("人員調動"));
node2.add(new DefaultMutableTreeNode("調動歷史查詢"));
//新增人員考核管理的子結點
node3.add(new DefaultMutableTreeNode("人員考核"));
node3.add(new DefaultMutableTreeNode("考核歷史查詢"));
//新增勞資管理的子結點
node4.add(new DefaultMutableTreeNode("勞資分配管理"));
node4.add(new DefaultMutableTreeNode("勞資歷史查詢"));
//往根節點下新增一級結點
root.add(node1);
root.add(node2);
root.add(node3);
root.add(node4);
//顯示選單
tree=new JTree(root);
tree.addTreeSelectionListener(this);
//用帶有滾動條的tree覆蓋之前左邊的按鈕
// splitPane.setLeftComponent(new JScrollPane(tree));
//重新設定左部的面板讓其不顯示的那麼滿,上下部留空隙
JPanel pleft=new JPanel();
JScrollPane js=new JScrollPane(tree);
//設定面板的大小
js.setPreferredSize(new Dimension(140, 555));
pleft.add(js);
//設定分隔面板的左部控制元件為pleft面板
splitPane.setLeftComponent(pleft);
//定義右側歡迎介面
pWelcome=new JPanel();
JLabel lbWelcome=new JLabel("歡迎使用人事管理系統");
//Font.PLAIN代表字型沒有特效,字型大小16
lbWelcome.setFont(new Font("宋體", Font.PLAIN, 16));
pWelcome.add(lbWelcome);
//設定分隔面板的右部控制元件為pWelcome面板
splitPane.setRightComponent(pWelcome);
//新增分隔面板到整個JFrame介面
add(splitPane);
}
public void valueChanged(TreeSelectionEvent e) {
// System.out.println(e.getPath());
//獲取路徑下的最後一個路徑,並將它轉換為字串型別(原本的返回值為Object)
String menuStr=e.getPath().getLastPathComponent().toString();
// System.out.println(menuStr);
switch(menuStr){
//點根節點回到歡迎介面
case"人事管理系統":
splitPane.setRightComponent(pWelcome);
break;
//若case中沒有書寫break語句,則switch的邏輯會讓其按照順序繼續執行下去,直到跳出
//只要滿足其中一個條件都執行下部的程式碼
case"基本資訊管理":;
case"人員調動管理":;
case"人員考核管理":;
case"勞資管理":;
//獲取當前項的路徑
TreePath path=e.getPath();
//判斷當前項是展開狀態還是摺疊狀態
if(tree.isExpanded(path)) {
//如果當前項是展開狀態,則改為摺疊狀態
tree.collapsePath(path);
}else {
//如果當前項是摺疊狀態,則改為展開狀態
tree.expandPath(path);
}
break;
case"新增人員資訊":
// System.out.println("右側應顯示新增人員介面");
splitPane.setRightComponent(new Panel11());
break;
case"修改人員資訊":
// System.out.println("右側應顯示修改人員介面");
splitPane.setRightComponent(new Panel12());
break;
case"刪除人員資訊":
// System.out.println("右側應顯示刪除人員介面");
splitPane.setRightComponent(new Panel13());
break;
case"查詢人員資訊":
// System.out.println("右側應顯示查詢人員介面");
splitPane.setRightComponent(new Panel14());
break;
case"部門管理":
// System.out.println("右側應顯示部門管理介面");
splitPane.setRightComponent(new Panel15());
break;
case"人員調動":
// System.out.println("右側應顯示人員調動介面");
splitPane.setRightComponent(new Panel21());
break;
case"調動歷史查詢":
// System.out.println("右側應顯示調動歷史查詢介面");
splitPane.setRightComponent(new Panel22());
break;
case"人員考核":
// System.out.println("右側應顯示人員考核介面");
splitPane.setRightComponent(new Panel31());
break;
case"考核歷史查詢":
// System.out.println("右側應顯示考核歷史查詢介面");
splitPane.setRightComponent(new Panel32());
break;
case"勞資分配管理":
// System.out.println("右側應顯示勞資分配管理介面");
splitPane.setRightComponent(new Panel41());
break;
case"勞資歷史查詢":
// System.out.println("右側應顯示勞資歷史查詢介面");
splitPane.setRightComponent(new Panel42());
break;
}
}
public static void main(String[] args) {
//修改程式外觀
try {
//跨平臺的外觀
// UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
//獲取系統的外觀來建立該介面的外觀
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
new HrMain();
}
}
***********************************************************************************************************************************************
package pdsu.hrms.view;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.JobAttributes;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.net.PortUnreachableException;
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.JTextField;
import javax.swing.border.LineBorder;
import pdsu.hrms.dao.DeptDao;
import pdsu.hrms.dao.PersonDao;
import pdsu.hrms.model.Dept;
import pdsu.hrms.model.Person;
public class Panel11 extends JPanel implements ActionListener,ItemListener{
//JPanel pTitle; //標題區域面板
JPanel pContent; //新增人員資訊內容區域面板
JScrollPane js; //新增人員資訊所在的滾動面板
JTextField tfPersonId; //人員編號
JTextField tfName; //姓名
JTextField tfSex; //性別
JTextField tfBirth; //出生日期
JTextField tfNat; //民族
JTextField tfAddress; //地址
JTextField tfOther; //其他說明
//String DeptID = null;
JComboBox<String> comboDept; //部門資訊
JButton btnAdd; //增加
JButton btnClear; //清空
long DeptID=0;
public Panel11() {
//設定為網格包佈局
GridBagLayout gridBag = new GridBagLayout();
GridBagConstraints cons = null;
setLayout(gridBag);
//新增標題
JLabel lbTitle = new JLabel("新增人員資訊");
lbTitle.setFont(new Font("宋體",0,16));
cons = new GridBagConstraints();
cons.gridx = 0;
cons.gridy = 0;
gridBag.setConstraints(lbTitle, cons);
add(lbTitle);
//新增內容區域
initContent(); //初始化新增人員資訊區域
js = new JScrollPane(pContent);
js.setPreferredSize(new Dimension(600, 500));
cons = new GridBagConstraints();
cons.gridx = 0;
cons.gridy = 1;
gridBag.setConstraints(js, cons);
add(js);
}
public void initContent() {
//建立內容面板並設定佈局方式
pContent = new JPanel();
// pContent.setBorder(new LineBorder(Color.red));
GridBagLayout layout = new GridBagLayout();
pContent.setLayout(layout);
GridBagConstraints cons = null;
//新增第1行元件(人員編號標籤、人員編號文字框、人員姓名標籤、人員姓名文字框)
//人員編號標籤
JLabel lbPersonId = new JLabel("人員編號");
//lbPersonId.setFont(new Font("宋體",0,12));
cons = new GridBagConstraints();
cons.gridx = 0;
cons.gridy = 0;
cons.insets = new Insets(10,10,10,1);//上左下右間距
layout.setConstraints(lbPersonId, cons);
pContent.add(lbPersonId);
//人員編號文字框
tfPersonId = new JTextField(15);
cons = new GridBagConstraints();
cons.gridx = 1;
cons.gridy = 0;
cons.insets = new Insets(10,1,10,15);
layout.setConstraints(tfPersonId, cons);
tfPersonId.setText(String.valueOf(PersonDao.getNextId()));
tfPersonId.setEditable(false);
pContent.add(tfPersonId);
//人員姓名標籤
JLabel lbName = new JLabel("人員姓名");
cons = new GridBagConstraints();
cons.gridx = 2;
cons.gridy = 0;
cons.insets = new Insets(10,15,10,1);
layout.setConstraints(lbName, cons);
pContent.add(lbName);
//人員姓名文字框
tfName = new JTextField(15);
cons = new GridBagConstraints();
cons.gridx = 3;
cons.gridy = 0;
cons.insets = new Insets(10,1,10,10);
layout.setConstraints(tfName, cons);
pContent.add(tfName);
//新增第2行元件(性別標籤、性別文字框、出生年月標籤、出生年月文字框)
//性別標籤
JLabel lbSex = new JLabel("性別");
cons = new GridBagConstraints();
cons.gridx = 0;
cons.gridy = 1;
cons.insets = new Insets(10,10,10,1);
layout.setConstraints(lbSex, cons);
pContent.add(lbSex);
//性別文字框
tfSex = new JTextField(15);
cons = new GridBagConstraints();
cons.gridx = 1;
cons.gridy = 1;
cons.insets = new Insets(10,1,10,15);
//cons.gridwidth = 3;
//cons.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfSex, cons);
pContent.add(tfSex);
//出生年月標籤
JLabel lbBirth = new JLabel("出生年月");
cons = new GridBagConstraints();
cons.gridx = 2;
cons.gridy = 1;
cons.insets = new Insets(10,15,10,1);
layout.setConstraints(lbBirth, cons);
pContent.add(lbBirth);
//出生年月文字框
tfBirth = new JTextField(15);
cons = new GridBagConstraints();
cons.gridx = 3;
cons.gridy = 1;
cons.insets = new Insets(10,1,10,10);
layout.setConstraints(tfBirth, cons);
pContent.add(tfBirth);
//新增第3行元件(民族標籤、民族文字框、地址標籤、地址文字框)
//民族標籤
JLabel lbNat = new JLabel("民族");
cons = new GridBagConstraints();
cons.gridx = 0;
cons.gridy = 2;
cons.insets = new Insets(10,10,10,1);
layout.setConstraints(lbNat, cons);
pContent.add(lbNat);
//民族文字框
tfNat = new JTextField(15);
cons = new GridBagConstraints();
cons.gridx = 1;
cons.gridy = 2;
cons.insets = new Insets(10,1,10,15);
layout.setConstraints(tfNat, cons);
pContent.add(tfNat);
//地址標籤
JLabel lbAddress = new JLabel("地址");
cons = new GridBagConstraints();
cons.gridx = 2;
cons.gridy = 2;
cons.insets = new Insets(10,15,10,1);
layout.setConstraints(lbAddress, cons);
pContent.add(lbAddress);
//地址文字框
tfAddress = new JTextField(15);
cons = new GridBagConstraints();
cons.gridx = 3;
cons.gridy = 2;
cons.insets = new Insets(10,1,10,10);
layout.setConstraints(tfAddress, cons);
pContent.add(tfAddress);
//新增第4行元件(部門標籤、部門下拉列表、其他標籤、其他文字框)
//部門標籤
JLabel lbDept = new JLabel("部門");
cons = new GridBagConstraints();
cons.gridx = 0;
cons.gridy = 3;
cons.insets = new Insets(10,10,10,1);
layout.setConstraints(lbDept, cons);
pContent.add(lbDept);
//部門下拉列表
/*String[] data = DeptDao.getDeptsForSelect();
for (int i =0;i<=data.length;i++)
{
comboDept.addItem(data[i]);
}*/
String[] allType = DeptDao.getDeptsForSelect();
comboDept = new JComboBox<String>(allType);
cons = new GridBagConstraints();
cons.gridx = 1;
cons.gridy = 3;
cons.insets = new Insets(10,1,10,15);
layout.setConstraints(comboDept, cons);
pContent.add(comboDept);
//其他標籤
JLabel lbOther = new JLabel("其他");
cons = new GridBagConstraints();
cons.gridx = 2;
cons.gridy = 3;
cons.insets = new Insets(10,15,10,1);
layout.setConstraints(lbOther, cons);
pContent.add(lbOther);
//其他文字框
tfOther = new JTextField(15);
cons = new GridBagConstraints();
cons.gridx = 3;
cons.gridy = 3;
cons.insets = new Insets(10,1,10,10);
layout.setConstraints(tfOther, cons);
pContent.add(tfOther);
//新增第5行元件(增加按鈕、清空按鈕)
//增加
btnAdd = new JButton("增加");
cons = new GridBagConstraints();
cons.gridx = 0;
cons.gridy = 4;
cons.gridwidth = 2;
cons.insets = new Insets(10,10,10,10);
layout.setConstraints(btnAdd, cons);
pContent.add(btnAdd);
//清空
btnClear = new JButton("清空");
cons = new GridBagConstraints();
cons.gridx = 2;
cons.gridy = 4;
cons.gridwidth = 2;
cons.insets = new Insets(10,10,10,10);
layout.setConstraints(btnClear, cons);
pContent.add(btnClear);
btnAdd.addActionListener(this);
btnClear.addActionListener(this);
comboDept.addItemListener(this);
}
@Override
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
if (e.getStateChange() == 1) {
Object tempStr = e.getItem();
int i = ((String) tempStr).indexOf("-");
DeptID = Long.parseLong(((String) tempStr).substring(0, i));
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == btnClear)
{
tfSex.setText(null);
tfAddress.setText(null);
tfBirth.setText(null);
tfName.setText(null);
tfNat.setText(null);
tfOther.setText(null);
tfPersonId.setText(null);
}
if (e.getSource() == btnAdd) {
String d = tfName.getText();
String a = tfSex.getText();
String b = tfAddress.getText();
String c = tfBirth.getText();
String g = tfNat.getText();
String f = tfOther.getText();
if(a.equals(null)||b.equals(null)||c.equals(null)||d.equals(null)||g.equals(null))
{
JOptionPane.showMessageDialog(null, "請輸入資訊");
}else {
if (DeptID != 0)
{
Person p = new Person();
p.setAddress(b);
p.setSex(a);
p.setBirth(c);
p.setName(d);
p.setNat(g);
p.setOther(f);
p.setAssess("未考核");
Long m = Long.valueOf(tfPersonId.getText());
p.setDeptid(DeptID);
p.setPersonid(m);
PersonDao.add(p);
}else
{
JOptionPane.showMessageDialog(null, "請為該人員選擇部門");
}
}
}
}
}
***********************************************************************************************************************************************
package pdsu.hrms.view;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.DefaultListSelectionModel;
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 pdsu.hrms.dao.HistoryDao;
public class Panel42 extends JPanel{
//上部
private JScrollPane js;
private JTable table;
String[][] colValue=new String[10][6];
public Panel42() {
GridBagLayout gridBag = new GridBagLayout();
GridBagConstraints cons = null;
setLayout(gridBag);
//新增標題
JLabel lbTitle = new JLabel("勞資歷史查詢");
lbTitle.setFont(new Font("宋體",0,16));
cons = new GridBagConstraints();
cons.gridx = 0;
cons.gridy = 0;
gridBag.setConstraints(lbTitle, cons);
add(lbTitle);
//建立一個10行3列的列表
//table=new JTable(10,3);
//列標題
String[] colTitle=new String[]{"流水號","人員姓名","原薪水","新薪水","變更次數","變更日期"};
//建立一個10行3列的列表
// String[][] colValue=new String[10][3];
//模擬資料
/*
colValue[0][0]="1";
colValue[0][1]="張三";
colValue[0][2]="0";
colValue[0][3]="4000";
colValue[0][4]="1";
colValue[0][5]="2010-10-01";
colValue[1][0]="2";
colValue[1][1]="李四";
colValue[1][2]="4000";
colValue[1][3]="3000";
colValue[1][4]="1";
colValue[1][5]="2010-10-25";
*/
String abc = "勞資分配";
colValue = HistoryDao.getAllByType(abc);
table=new JTable(colValue, colTitle);
//設定表格的預設大小
table.setPreferredScrollableViewportSize(new Dimension(430, 390));
//設定不能同時選中多個行
table.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
js=new JScrollPane(table);
js.setPreferredSize(new Dimension(430, 390));
cons = new GridBagConstraints();
cons.gridx = 0;
cons.gridy = 1;
gridBag.setConstraints(js, cons);
add(js);
}
}
*************************************************************************************************************************************************
package pdsu.hrms.view;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.DefaultListSelectionModel;
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 javax.swing.table.DefaultTableModel;
import pdsu.hrms.dao.DeptDao;
import pdsu.hrms.dao.PersonDao;
import pdsu.hrms.model.Person;
public class Panel13 extends JPanel implements ActionListener{
//定義所需的元件
private JPanel pTop;
private JPanel pCenter;
private JPanel pBottom;
//上部
private JScrollPane js;
private JTable table;
//中部
private JLabel lbDeptId;
private JLabel lbName;
private JLabel lbDeptName;
private JTextField tfDeptId;
private JTextField tfName;
private JTextField tfDeptName;
//底部
private JButton btnDelete;
String[][] colValue=new String[10][6];
String[] colTitle=new String[]{"編號","姓名","出生年月","民族","地址","部門"};
public Panel13() {
//設定為邊界佈局
setLayout(new BorderLayout());
initTop();
initCenter();
initBottom();
}
//初始化上部面板
public void initTop() {
pTop=new JPanel();
GridBagLayout gridBag = new GridBagLayout();
GridBagConstraints cons = null;
pTop.setLayout(gridBag);
//新增標題
JLabel lbTitle = new JLabel("人員資訊刪除");
lbTitle.setFont(new Font("宋體",0,16));
cons = new GridBagConstraints();
cons.gridx = 0;
cons.gridy = 0;
gridBag.setConstraints(lbTitle, cons);
pTop.add(lbTitle);
//建立一個10行3列的列表
//table=new JTable(10,3);
//列標題
//建立一個10行3列的列表
// String[][] colValue=new String[10][3];
//模擬資料
/*
colValue[0][0]="1";
colValue[0][1]="張三";
colValue[0][2]="1980-03-19";
colValue[0][3]="漢族";
colValue[0][4]="北京";
colValue[0][5]="辦公室-綜合科";
colValue[1][0]="2";
colValue[1][1]="李四";
colValue[1][2]="1981-09-17";
colValue[1][3]="漢族";
colValue[1][4]="北京";
colValue[1][5]="人事處-人事科";
*/
colValue = PersonDao.getpersonsfortable();
table=new JTable(colValue, colTitle);
//設定表格的預設大小
table.setPreferredScrollableViewportSize(new Dimension(430, 300));
//設定不能同時選中多個行
table.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
//獲取選中行
int row=table.getSelectedRow();
//將資料顯示到文字框中
tfDeptId.setText(colValue[row][0]);
tfName.setText(colValue[row][1]);
tfDeptName.setText(colValue[row][5]);
btnDelete.setEnabled(true);
}
});
js=new JScrollPane(table);
js.setPreferredSize(new Dimension(430, 300));
cons = new GridBagConstraints();
cons.gridx = 0;
cons.gridy = 1;
gridBag.setConstraints(js, cons);
pTop.add(js);
add(pTop, BorderLayout.NORTH);
}
public void initCenter() {
pCenter=new JPanel();
//編號項
lbDeptId=new JLabel("編號");
tfDeptId=new JTextField(15);
//一級部門項
lbName=new JLabel("姓名");
tfName=new JTextField(14);
//二級部門項
lbDeptName=new JLabel("部門");
tfDeptName=new JTextField(14);
pCenter.add(lbDeptId);
pCenter.add(tfDeptId);
pCenter.add(lbName);
pCenter.add(tfName);
pCenter.add(lbDeptName);
pCenter.add(tfDeptName);
add(pCenter, BorderLayout.CENTER);
}
public void initBottom() {
pBottom=new JPanel();
btnDelete=new JButton("刪除");
btnDelete.setEnabled(false);
pBottom.add(btnDelete);
add(pBottom,BorderLayout.SOUTH);
btnDelete.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == btnDelete)
{
Long personId = Long.valueOf(tfDeptId.getText());
PersonDao.delete(personId);
// PersonDao.getDeptId(personId);
}
updatetable();
}
private void updatetable()
{
colValue = PersonDao.getpersonsfortable();
DefaultTableModel tableModel=new DefaultTableModel(colValue, colTitle);
table.setModel(tableModel);
}
}
********************************************************************************************************************************************
package pdsu.hrms.view;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.DefaultListSelectionModel;
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 javax.swing.table.DefaultTableModel;
import pdsu.hrms.dao.DeptDao;
import pdsu.hrms.dao.PersonDao;
public class Panel14 extends JPanel{
//上部
private JScrollPane js;
private JTable table;
String[][] colValue=null;
String[] colTitle=new String[]{"編號","姓名","出生年月","民族","地址","部門"};
public Panel14() {
GridBagLayout gridBag = new GridBagLayout();
GridBagConstraints cons = null;
setLayout(gridBag);
//新增標題
JLabel lbTitle = new JLabel("人員資訊查詢");
lbTitle.setFont(new Font("宋體",0,16));
cons = new GridBagConstraints();
cons.gridx = 0;
cons.gridy = 0;
gridBag.setConstraints(lbTitle, cons);
add(lbTitle);
colValue = PersonDao.getpersonsfortable();
table=new JTable(colValue, colTitle);
//設定表格的預設大小
table.setPreferredScrollableViewportSize(new Dimension(430, 390));
//設定不能同時選中多個行
table.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
js=new JScrollPane(table);
js.setPreferredSize(new Dimension(600, 500));
cons = new GridBagConstraints();
cons.gridx = 0;
cons.gridy = 1;
gridBag.setConstraints(js, cons);
add(js);
updatetable();
}
private void updatetable()
{
colValue = PersonDao.getpersonsfortable();
DefaultTableModel tableModel=new DefaultTableModel(colValue, colTitle);
table.setModel(tableModel);
}
}
***********************************************************************************************************************************************
package pdsu.hrms.view;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.DefaultListSelectionModel;
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 javax.swing.table.DefaultTableModel;
import pdsu.hrms.dao.DeptDao;
import pdsu.hrms.model.Dept;
public class Panel15 extends JPanel implements ActionListener{
//定義所需的元件
private JPanel pTop;
private JPanel pCenter;
private JPanel pBottom;
//上部
private JScrollPane js;
private JTable table;
//中部
private JLabel lbDeptId;
private JLabel lbDept1Name;
private JLabel lbDept2Name;
private JTextField tfDeptId;
private JTextField tfDept1Name;
private JTextField tfDept2Name;
//底部
private JButton btnNextId;
private JButton btnAdd;
private JButton btnUpdate;
private JButton btnDelete;
private JButton btnClear;
String[][] colValue=null;
String[] colTitle=new String[]{"部門編號","一級部門","二級部門"};
public Panel15() {
//設定為邊界佈局
setLayout(new BorderLayout());
initTop();
initCenter();
initBottom();
}
//初始化上部面板
public void initTop() {
pTop=new JPanel();
//建立一個10行3列的列表
//table=new JTable(10,3);
//列標題
//建立一個10行3列的列表
// String[][] colValue=new String[10][3];
//模擬資料
/*
colValue[0][0]="1";
colValue[0][1]="辦公室";
colValue[0][2]="綜合科";
colValue[1][0]="2";
colValue[1][1]="人事處";
colValue[1][2]="人事科";*/
colValue = DeptDao.getDeptsForTable();
table=new JTable(colValue, colTitle);
//設定表格的預設大小
table.setPreferredScrollableViewportSize(new Dimension(430, 300));
//設定不能同時選中多個行
table.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
//獲取選中行
int row=table.getSelectedRow();
//將資料顯示到文字框中
tfDeptId.setText(colValue[row][0]);
tfDept1Name.setText(colValue[row][1]);
tfDept2Name.setText(colValue[row][2]);
btnUpdate.setEnabled(true);
btnDelete.setEnabled(true);
btnAdd.setEnabled(false);
}
});
js=new JScrollPane(table);
js.setPreferredSize(new Dimension(430, 300));
pTop.add(js);
add(pTop, BorderLayout.NORTH);
}
public void initCenter() {
pCenter=new JPanel();
//編號項
lbDeptId=new JLabel("編號");
tfDeptId=new JTextField(15);
//一級部門項
lbDept1Name=new JLabel("一級部門");
tfDept1Name=new JTextField(14);
//二級部門項
lbDept2Name=new JLabel("二級部門");
tfDept2Name=new JTextField(14);
pCenter.add(lbDeptId);
pCenter.add(tfDeptId);
pCenter.add(lbDept1Name);
pCenter.add(tfDept1Name);
pCenter.add(lbDept2Name);
pCenter.add(tfDept2Name);
add(pCenter, BorderLayout.CENTER);
}
public void initBottom() {
pBottom=new JPanel();
btnNextId=new JButton("獲取新編號");
btnAdd=new JButton("增加");
btnUpdate=new JButton("修改");
btnDelete=new JButton("刪除");
btnClear=new JButton("清空");
btnAdd.setEnabled(false);
btnUpdate.setEnabled(false);
btnDelete.setEnabled(false);
pBottom.add(btnNextId);
pBottom.add(btnAdd);
pBottom.add(btnUpdate);
pBottom.add(btnDelete);
pBottom.add(btnClear);
btnNextId.addActionListener(this);
btnAdd.addActionListener(this);
btnClear.addActionListener(this);
btnDelete.addActionListener(this);
btnUpdate.addActionListener(this);