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(