package com.jnzyjsxy.node1; import java.awt.BorderLayout; import java.awt.Button; import java.awt.Dimension; import java.awt.Font; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; import com.jnzyjsxy.common.DeptBean; import com.jnzyjsxy.common.PersonBean; import com.jnzyjsxy.test.HrMain; /* * 第一个节点 * 添加人员信息 */ public class Node11Panel extends JPanel implements ActionListener,ItemListener{ //创建两个面板 一个中间面板 一个上部面板 JPanel centerPanel=new JPanel(); JPanel upPanel=new JPanel(); //定义图形元素 JLabel jLabel = new JLabel(); JLabel jLabel1 = new JLabel(); JLabel jLabel2 = new JLabel(); JLabel jLabel3 = new JLabel(); JLabel jLabel4 = new JLabel(); JLabel jLabel5 = new JLabel(); JLabel jLabel6 = new JLabel(); JLabel jLabel7 = new JLabel(); JLabel jLabel8 = new JLabel(); JLabel jLabel9 = new JLabel(); JTextField jTextField1 =new JTextField(15);//编号 JTextField jTextField2 =new JTextField(15);//姓名 JTextField jTextField3 =new JTextField(15);//性别 JTextField jTextField4 =new JTextField(15);//出生年月 JTextField jTextField5 =new JTextField(15);//民族 JTextField jTextField6 =new JTextField(15);//地址 JComboBox jComboBox1=null;//显示人员所在部门 String DeptID="1";//部门编号 String Salary="0";//薪酬 String Assess="未考核";//考核 //用于输入其他字段值 JTextField jTextField8=new JTextField(15); JScrollPane jScrollPane; //创建按钮 本页面使用addInfo clearInfo //为了实现页面复制 定义多个按钮 哪个页面需要 使用哪个按钮 JButton addInfo=new JButton(); JButton modifyInfo=new JButton(); JButton deleteInfo=new JButton(); JButton clearInfo=new JButton(); //创建网格包布局对象 GridBagLayout girdBag=new GridBagLayout(); //创建网格包约束类变量 GridBagConstraints girdBagCon; //创建PersonBean 人员数据操作类 PersonBean bean=new PersonBean(); public Node11Panel() { // TODO Auto-generated constructor stub //采用边框布局 this.setLayout(new BorderLayout()); try { jscrollPanelInit();//上部面板初始 panelInit();//中间面板初始化 addListener();//监听事件方法 } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } /* * jscrollpanel 面板的布局 包含放置组件的中间面板centerPanel * (non-Javadoc) * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent) */ public void jscrollPanelInit() { //中间放组件的面板 采用网格包布局 centerPanel.setLayout(girdBag); //第一行组件 //griy=1 实际的第二行 显示4个组件 gridx依次增加 jLabel1.setText("人员编号"); jLabel1.setFont(new Font("Dialog", 0, 12)); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=0; girdBagCon.gridy=1; //外部填充 4个参数 top,left,bottom,right //既外部间距 girdBagCon.insets=new Insets(0, 10, 10, 1); girdBag.setConstraints(jLabel1, girdBagCon); centerPanel.add(jLabel1); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=1; girdBagCon.gridy=1; girdBagCon.insets=new Insets(0, 1, 10, 15); girdBag.setConstraints(jTextField1, girdBagCon); centerPanel.add(jTextField1); jLabel2.setText("人员姓名"); jLabel2.setFont(new Font("Dialog", 0, 12)); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=2; girdBagCon.gridy=1; //外部填充 4个参数 top,left,bottom,right //既外部间距 girdBagCon.insets=new Insets(0, 15, 10, 1); girdBag.setConstraints(jLabel2, girdBagCon); centerPanel.add(jLabel2); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=3; girdBagCon.gridy=1; girdBagCon.insets=new Insets(0, 1, 10, 10); girdBag.setConstraints(jTextField2, girdBagCon); centerPanel.add(jTextField2); jLabel3.setText("性 别"); jLabel3.setFont(new Font("Dialog", 0, 12)); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=0; girdBagCon.gridy=2; //外部填充 4个参数 top,left,bottom,right //既外部间距 girdBagCon.insets=new Insets(10, 10, 10, 1); girdBag.setConstraints(jLabel3, girdBagCon); centerPanel.add(jLabel3); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=1; girdBagCon.gridy=2; girdBagCon.insets=new Insets(10, 1, 10, 15); girdBag.setConstraints(jTextField3, girdBagCon); centerPanel.add(jTextField3); jLabel4.setText("出 生 年 月"); jLabel4.setFont(new Font("Dialog", 0, 12)); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=2; girdBagCon.gridy=2; //外部填充 4个参数 top,left,bottom,right //既外部间距 girdBagCon.insets=new Insets(10, 15, 10, 1); girdBag.setConstraints(jLabel4, girdBagCon); centerPanel.add(jLabel4); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=3; girdBagCon.gridy=2; girdBagCon.insets=new Insets(10, 1, 10, 10); girdBag.setConstraints(jTextField4, girdBagCon); centerPanel.add(jTextField4); jLabel5.setText("民 族"); jLabel5.setFont(new Font("Dialog", 0, 12)); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=0; girdBagCon.gridy=3; //外部填充 4个参数 top,left,bottom,right //既外部间距 girdBagCon.insets=new Insets(10, 10, 10, 1); girdBag.setConstraints(jLabel5, girdBagCon); centerPanel.add(jLabel5); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=1; girdBagCon.gridy=3; girdBagCon.insets=new Insets(10, 1, 10, 15); girdBag.setConstraints(jTextField5, girdBagCon); centerPanel.add(jTextField5); jLabel6.setText("地 址"); jLabel6.setFont(new Font("Dialog", 0, 12)); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=2; girdBagCon.gridy=3; //外部填充 4个参数 top,left,bottom,right //既外部间距 girdBagCon.insets=new Insets(10, 15, 10, 1); girdBag.setConstraints(jLabel6, girdBagCon); centerPanel.add(jLabel6); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=3; girdBagCon.gridy=3; girdBagCon.insets=new Insets(10, 1, 10, 10); girdBag.setConstraints(jTextField6, girdBagCon); centerPanel.add(jTextField6); //第四行显示组件 gridy=4 gridx 依次增加 jLabel7.setText("部 门"); jLabel7.setFont(new Font("Dialog", 0, 12)); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=0; girdBagCon.gridy=4; //外部填充 4个参数 top,left,bottom,right //既外部间距 girdBagCon.insets=new Insets(10, 10, 10, 1); girdBag.setConstraints(jLabel7, girdBagCon); centerPanel.add(jLabel7); //定义部门操作类 DeptBean dBean=new DeptBean(); //调用方法获取所有部门的字符串 String[] alltype=dBean.searchAllForNode(); jComboBox1=new JComboBox(alltype); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=1; girdBagCon.gridy=4; //girdBagCon.gridwidth=3; //girdBagCon.gridheight=1; girdBagCon.insets=new Insets(10, 1, 10, 15); girdBag.setConstraints(jComboBox1, girdBagCon); centerPanel.add(jComboBox1); jLabel8.setText("其 他"); jLabel8.setFont(new Font("Dialog", 0, 12)); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=2; girdBagCon.gridy=4; //外部填充 4个参数 top,left,bottom,right //既外部间距 girdBagCon.insets=new Insets(10, 0, 10, 1); girdBag.setConstraints(jLabel8, girdBagCon); centerPanel.add(jLabel8); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=3; girdBagCon.gridy=4; girdBagCon.gridwidth=3; girdBagCon.gridheight=1; girdBagCon.insets=new Insets(10, 1, 10, 15); girdBag.setConstraints(jTextField8, girdBagCon); centerPanel.add(jTextField8); addInfo.setText("增加"); addInfo.setFont(new Font("Dialog", 0, 12)); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=0; girdBagCon.gridy=5; girdBagCon.gridwidth=2; girdBagCon.gridheight=1; //外部填充 4个参数 top,left,bottom,right //既外部间距 girdBagCon.insets=new Insets(10, 10, 10, 10); girdBag.setConstraints(addInfo, girdBagCon); centerPanel.add(addInfo); clearInfo.setText("清空"); clearInfo.setFont(new Font("Dialog", 0, 12)); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=2; girdBagCon.gridy=5; girdBagCon.gridwidth=2; girdBagCon.gridheight=1; //外部填充 4个参数 top,left,bottom,right //既外部间距 girdBagCon.insets=new Insets(10, 10, 10, 10); girdBag.setConstraints(clearInfo, girdBagCon); centerPanel.add(clearInfo); } //上部面板初始化 public void panelInit() { //上部面板,网格包布局 upPanel.setLayout(girdBag); //上部包含泽增加人员信息标签和滚动面板 jLabel.setText("增加人员信息"); jLabel.setFont(new Font("Dialog", 0, 16)); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=0; girdBagCon.gridy=0; //外部填充 4个参数 top,left,bottom,right //既外部间距 girdBagCon.insets=new Insets(0, 10, 0, 10); girdBag.setConstraints(jLabel, girdBagCon); upPanel.add(jLabel); //滚动面板 包含组件的中间,面板在滚动面板上 jScrollPane =new JScrollPane(centerPanel); jScrollPane.setPreferredSize(new Dimension(450,380)); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=0; girdBagCon.gridy=1; //外部填充 4个参数 top,left,bottom,right //既外部间距 girdBagCon.insets=new Insets(0, 0, 0, 0); girdBag.setConstraints(jScrollPane, girdBagCon); upPanel.add(jScrollPane); //上部面板添加到窗体 this.add(upPanel,BorderLayout.NORTH); //设置人员编号文本框不可用 jTextField1.setEditable(false); jTextField2.setEditable(true); jTextField3.setEditable(true); jTextField4.setEditable(true); jTextField5.setEditable(true); jTextField6.setEditable(true); jTextField8.setEditable(true); jTextField1.setText(""+bean.getId()); } /*添加事件监听 * (non-Javadoc) * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent) */ public void addListener() { //添加添加,清空按钮事件监听 addInfo.addActionListener(this); clearInfo.addActionListener(this); //给显示部门的下拉列表添加项目选择监听 //既当在下拉列表框中选择项目后 触发监听 jComboBox1.addItemListener(this); } @Override public void itemStateChanged(ItemEvent e) { // TODO Auto-generated method stub //ItemEvent--项目事件,例如下拉列表框中项被选定或取消选定的事件 //e.getStateChange()--表示选定或取消选定 //ItemEvent.SELECTED--表示选定 //ItemEvent.DESELECTED--表示取消选定 //如果在下拉列表框中选中一个部门 if (e.getStateChange()==ItemEvent.SELECTED) { //获取选中项目值,得到的是一个“1-办公室-秘书室”这样的字符串 //字符串构成:部门编号-一级部门-二级部门 String temStr=""+e.getItem(); //返回指定子字符串"-"在此字符串中第一次出现处的索引 int i=temStr.indexOf("-"); //取部门id子字符串,目的是取前面的部门编号 DeptID=temStr.substring(0,i); } } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub Object obj=e.getSource(); if (obj==addInfo) {//增加 //调用Personean的add()方法 实现添加纪录 bean.add(jTextField1.getText(), jTextField2.getText(), jTextField3.getText(), jTextField4.getText(), jTextField5.getText(), jTextField6.getText(), DeptID, Salary, Assess, jTextField8.getText()); //定义添加人员信息页面对象 Node11Panel node11Panel=new Node11Panel(); //用添加人员信息作为右侧组件 HrMain.splitPane.setRightComponent(node11Panel); } else if (obj==clearInfo) { setNull();//调用清空方法 } } void setNull(){ jTextField2.setText(null); jTextField3.setText(null); jTextField4.setText(null); jTextField5.setText(null); jTextField6.setText(null); jTextField8.setText(null); } }

庞玉栋个人博客、人生苦短-我用Python