swing自定义菜单面板_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > swing自定义菜单面板

swing自定义菜单面板

 2012/2/17 9:26:20  ljjclub  程序员俱乐部  我要评论(0)
  • 摘要:第一次接触swing。。大家帮忙看看面板切换监听那里怎么判断当前所在哪个面板,然后隐藏,importjava.awt.*;importjava.awt.event.*;importjava.util.List;importjavax.swing.*;publicclassFriendInfoimplementsActionListener{privateJFramejFrame;privateJPaneljContentPane,jContentPane1,jContentPane2
  • 标签:菜单 自定义 Swing
第一次接触swing。。 大家帮忙看看 面板切换监听那里怎么判断当前所在哪个面板,然后隐藏,


import java.awt.*;
import java.awt.event.*;
import java.util.List;

import javax.swing.*;

public class FriendInfo implements ActionListener {

	private JFrame jFrame;
	private JPanel jContentPane, jContentPane1, jContentPane2, jContentPane3;
	private JLabel mainText = null;
	private JLabel lblName = null;
	private JTextField tfName = null;
	private JLabel lblSex = null;
	private JComboBox cbSex = null;
	private JLabel lblPhone = null;
	private JTextField tfPhone = null;
	private JLabel lblEmail = null;
	private JTextField tfEmail = null;
	private JLabel lblAddress = null;
	private JTextField tfAddress = null;

	JMenuBar jMenuBarOne;
	JMenu menu1, menu2, menu3;
	ButtonGroup buttonEdit;

	private JButton btnAdd = null;
	private JButton btnDel = null;
	private JButton btnModify = null;
	private JButton btnQuery = null;
	// private JButton btnUpPage = null;
	// private JButton btnDownPage = null;
	private JTable table = null;
	TableModel tm = null;
	JScrollPane jsp = null;

	private JTextField getTfName() {
		if (tfName == null) {
			tfName = new JTextField();
			tfName.setBounds(new Rectangle(89, 17, 173, 27));
		}
		return tfName;
	}

	private JComboBox getCbSex() {
		if (cbSex == null) {
			String[] str = { "请选择", "男", "女" };
			cbSex = new JComboBox(str);
			cbSex.setBounds(new Rectangle(360, 17, 229, 28));
		}
		return cbSex;
	}

	private JTextField getTfPhone() {
		if (tfPhone == null) {
			tfPhone = new JTextField();
			tfPhone.setBounds(new Rectangle(89, 61, 173, 27));
		}
		return tfPhone;
	}

	private JTextField getTfEmail() {
		if (tfEmail == null) {
			tfEmail = new JTextField();
			tfEmail.setBounds(new Rectangle(360, 61, 229, 28));
		}
		return tfEmail;
	}

	private JTextField getTfAddress() {
		if (tfAddress == null) {
			tfAddress = new JTextField();
			tfAddress.setBounds(new Rectangle(89, 105, 173, 27));
		}
		return tfAddress;
	}

	private JButton getBtnAdd() {
		if (btnAdd == null) {
			btnAdd = new JButton();
			btnAdd.setBounds(new Rectangle(46, 152, 71, 30));
			btnAdd.addActionListener(this);
			btnAdd.setText("添加");
		}
		return btnAdd;
	}

	private JButton getBtnDel() {
		if (btnDel == null) {
			btnDel = new JButton();
			btnDel.setBounds(new Rectangle(189, 152, 71, 30));
			btnDel.addActionListener(this);
			btnDel.setText("删除");
		}
		return btnDel;
	}

	private JButton getBtnModify() {
		if (btnModify == null) {
			btnModify = new JButton();
			btnModify.setBounds(new Rectangle(342, 152, 71, 30));
			btnModify.addActionListener(this);
			btnModify.setText("更新");
		}
		return btnModify;
	}

	private JButton getBtnQuery() {
		if (btnQuery == null) {
			btnQuery = new JButton();
			btnQuery.setBounds(new Rectangle(484, 152, 71, 30));
			btnQuery.addActionListener(this);
			btnQuery.setText("查找");
		}
		return btnQuery;
	}

	// 添加分页选项
	// private JButton getBtnUpPage() {
	// if (btnUpPage == null) {
	// btnUpPage = new JButton("上一页");
	// btnUpPage.addActionListener(this);
	// btnUpPage.setBounds(200, 390, 80, 30);
	// }
	// return btnUpPage;
	// }
	//
	// private JButton getBtnDownPage() {
	// if (btnDownPage == null) {
	// btnDownPage = new JButton("下一页");
	// btnDownPage.addActionListener(this);
	// btnDownPage.setBounds(300, 390, 80, 30);
	// }
	// return btnDownPage;
	// }

	public FriendInfo(final String user) {
		init();
		this.getJFrame(user,getJContentPane1()).setVisible(true);

	}

	// public static void main(String[] args) {
	// SwingUtilities.invokeLater(new Runnable() {
	// public void run() {
	// FriendInfo application = new FriendInfo();
	// application.getJFrame().setVisible(true);
	// }
	// });
	// }
	private void init() {
		// TODO Auto-generated method stub
		if (jFrame == null) {
			jFrame = new JFrame();
			jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			jFrame.setBounds(new Rectangle(450, 250, 610, 454));
			
		}
	}
	
	JFrame getJFrame(String user,JPanel jContentPane) {
//		if (jFrame == null) {
//			jFrame = new JFrame();
//			jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//			jFrame.setBounds(new Rectangle(450, 250, 610, 454));
			jFrame.setContentPane(jContentPane);
			jFrame.setJMenuBar(getJMenuBar(user));

			jFrame.setTitle("人员信息--管理员:" + user);
//		}
		return jFrame;
	}

	private JMenuBar getJMenuBar(final String user) {
		jMenuBarOne = new JMenuBar();
		buttonEdit = new ButtonGroup();
		
		menu1 = new JMenu("基本信息");
		menu2 = new JMenu("其他");
		menu3 = new JMenu("关于");
		
		menu1.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				System.out.println("点击基本信息选项卡");
//				if(){}
				getJContentPane1().setVisible(false);
//				getJContentPane2().setVisible(false);
//				getJContentPane3().setVisible(false);
				getJFrame(user,getJContentPane()).setVisible(true);

			}
		});
		menu2.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				System.out.println("点击其他选项卡");
				getJContentPane().setVisible(false);
//				getJContentPane1().setVisible(false);
//				getJContentPane3().setVisible(false);
				getJFrame(user,getJContentPane2()).setVisible(true);
			}
		});
		menu3.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				System.out.println("点击关于选项卡");
				getJContentPane().setVisible(false);
//				getJContentPane1().setVisible(false);
//				getJContentPane2().setVisible(false);
				getJFrame(user,getJContentPane3()).setVisible(true);
				
			}
		});

		jMenuBarOne.add(menu1);
		jMenuBarOne.add(menu2);
		jMenuBarOne.add(menu3);
		return jMenuBarOne;
	}

	private JPanel getJContentPane1() {
		if (jContentPane1 == null) {
			mainText = new JLabel();
			mainText.setBounds(new Rectangle(15, 106, 63, 27));
			mainText.setText("主界面");
			jContentPane1 = new JPanel();
			jContentPane1.setLayout(null);
			jContentPane1.add(mainText, null);
		}
		return jContentPane1;
	}
	private JPanel getJContentPane2(){
		if(jContentPane2 == null){
			mainText = new JLabel();
			mainText.setBounds(new Rectangle(15,106,63,27));
			mainText.setText("其他待定界面");
			jContentPane2 = new JPanel();
			jContentPane2.setLayout(null);
			jContentPane2.add(mainText,null);
		}
		return jContentPane2;
	}
	private JPanel getJContentPane3(){
		if(jContentPane3 == null){
			lblAddress = new JLabel();
			lblAddress.setBounds(new Rectangle(15, 106, 63, 27));
			lblAddress.setText("3");
			lblEmail = new JLabel();
			lblEmail.setBounds(new Rectangle(285, 61, 63, 27));
			lblEmail.setText("3");
			lblPhone = new JLabel();
			lblPhone.setBounds(new Rectangle(15, 61, 63, 27));
			lblPhone.setText("3");
			lblSex = new JLabel();
			lblSex.setBounds(new Rectangle(285, 17, 63, 27));
			lblSex.setText("3");
			lblName = new JLabel();
			lblName.setBounds(new Rectangle(15, 17, 63, 27));
			lblName.setText("3");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(lblName, null);
			jContentPane.add(getTfName(), null);
			jContentPane.add(lblSex, null);
			jContentPane.add(getCbSex(), null);
			jContentPane.add(lblPhone, null);
			jContentPane.add(getTfPhone(), null);
			jContentPane.add(lblEmail, null);
			jContentPane.add(getTfEmail(), null);
			jContentPane.add(lblAddress, null);
			jContentPane.add(getTfAddress(), null);
			jContentPane.add(getBtnAdd(), null);
			jContentPane.add(getBtnDel(), null);
			jContentPane.add(getBtnModify(), null);
			jContentPane.add(getBtnQuery(), null);
		}
		return jContentPane3;
	}

	private JPanel getJContentPane() {
		if (jContentPane == null) {

			lblAddress = new JLabel();
			lblAddress.setBounds(new Rectangle(15, 106, 63, 27));
			lblAddress.setText("地址");
			lblEmail = new JLabel();
			lblEmail.setBounds(new Rectangle(285, 61, 63, 27));
			lblEmail.setText("邮箱");
			lblPhone = new JLabel();
			lblPhone.setBounds(new Rectangle(15, 61, 63, 27));
			lblPhone.setText("电话");
			lblSex = new JLabel();
			lblSex.setBounds(new Rectangle(285, 17, 63, 27));
			lblSex.setText("性别");
			lblName = new JLabel();
			lblName.setBounds(new Rectangle(15, 17, 63, 27));
			lblName.setText("姓名");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(lblName, null);
			jContentPane.add(getTfName(), null);
			jContentPane.add(lblSex, null);
			jContentPane.add(getCbSex(), null);
			jContentPane.add(lblPhone, null);
			jContentPane.add(getTfPhone(), null);
			jContentPane.add(lblEmail, null);
			jContentPane.add(getTfEmail(), null);
			jContentPane.add(lblAddress, null);
			jContentPane.add(getTfAddress(), null);
			jContentPane.add(getBtnAdd(), null);
			jContentPane.add(getBtnDel(), null);
			jContentPane.add(getBtnModify(), null);
			jContentPane.add(getBtnQuery(), null);
			// jContentPane.add(getBtnUpPage(), null);
			// jContentPane.add(getBtnDownPage(), null);
		}
		return jContentPane;
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		if (e.getActionCommand().equals("查找")) {
			selectAll();
		}
		// else if (e.getActionCommand().equals("下一页")) {
		// selectDownPage();
		// } else if (e.getActionCommand().equals("上一页")) {
		// selectUpPage();
		// }
		else if (e.getActionCommand().equals("添加")) {
			if (tfName.getText().equals("")
					|| cbSex.getSelectedItem().equals("请选择")
					|| tfPhone.getText().equals("")
					|| tfEmail.getText().equals("")
					|| tfAddress.getText().equals("")) {
				JOptionPane.showMessageDialog(jFrame, "请填写相关信息");
			} else {
				Friend fe = new Friend();
				fe.setName(tfName.getText());
				fe.setSex((String) cbSex.getSelectedItem());
				fe.setPhone(tfPhone.getText());
				fe.setEmail(tfEmail.getText());
				fe.setAddress(tfAddress.getText());
				boolean flag = FriendOperate.add(fe);
				if (flag) {
					JOptionPane.showMessageDialog(jFrame, "添加成功");
				} else {
					JOptionPane.showMessageDialog(jFrame, "添加失败");
				}
			}
		} else if (e.getActionCommand().equals("删除")) {
			if (!tfName.getText().equals("")) {
				String key = tfName.getText();
				boolean flag = FriendOperate.del(key);
				if (flag) {
					JOptionPane.showMessageDialog(jFrame, "删除成功");
				} else {
					JOptionPane.showMessageDialog(jFrame, "删除失败");
				}
			} else {
				JOptionPane.showMessageDialog(jFrame, "请填写姓名");
			}
		} else if (e.getActionCommand().equals("更新")) {
			if (!tfName.getText().equals("")
					&& !cbSex.getSelectedItem().equals("请选择")
					&& !tfPhone.getText().equals("")
					&& !tfEmail.getText().equals("")
					&& !tfAddress.getText().equals("")) {
				Friend frd = new Friend();
				frd.setName(tfName.getText());
				frd.setSex((String) cbSex.getSelectedItem());
				frd.setPhone(tfPhone.getText());
				frd.setEmail(tfEmail.getText());
				frd.setAddress(tfAddress.getText());
				boolean flag = FriendOperate.modify(frd);
				if (flag) {
					JOptionPane.showMessageDialog(jFrame, "更新成功");
				} else {
					JOptionPane.showMessageDialog(jFrame, "更新失败");
				}
			} else {
				JOptionPane.showMessageDialog(jFrame, "请填写相应的信息");
			}
		}

	}

	public void setValues(Object[][] obj) {
		this.tm.setDatavalues(obj);
	}

	public void selectAll() {
		List<Friend> list = FriendOperate.queryFirst();
		Object[][] datavalues = new Object[list.size()][5];
		for (int i = 0; i < list.size(); i++) {
			Friend fe = list.get(i);
			datavalues[i][0] = fe.getName();
			datavalues[i][1] = fe.getSex();
			datavalues[i][2] = fe.getPhone();
			datavalues[i][3] = fe.getEmail();
			datavalues[i][4] = fe.getAddress();
		}
		tm = new TableModel();
		this.setValues(datavalues);
		table = new JTable(tm);
		table.updateUI();
		JScrollPane jsp = new JScrollPane(table);
		jsp.setBounds(0, 200, 600, 182);
		jContentPane.add(jsp);
	}

	// public void selectDownPage() {
	// List<Friend> list = FriendOperate.downPage();
	// if (list != null) {
	// Object[][] datavalues = new Object[list.size()][5];
	// for (int i = 0; i < list.size(); i++) {
	// Friend fe = list.get(i);
	// datavalues[i][0] = fe.getName();
	// datavalues[i][1] = fe.getSex();
	// datavalues[i][2] = fe.getPhone();
	// datavalues[i][3] = fe.getEmail();
	// datavalues[i][4] = fe.getAddress();
	// }
	// this.setValues(datavalues);
	// table.updateUI();
	// } else {
	// JOptionPane.showMessageDialog(jFrame, "已经是最后一页");
	// }
	// }
	//
	// public void selectUpPage() {
	// List<Friend> list = FriendOperate.upPage();
	// if (list != null) {
	// Object[][] datavalues = new Object[list.size()][5];
	// for (int i = 0; i < list.size(); i++) {
	// Friend fe = list.get(i);
	// datavalues[i][0] = fe.getName();
	// datavalues[i][1] = fe.getSex();
	// datavalues[i][2] = fe.getPhone();
	// datavalues[i][3] = fe.getEmail();
	// datavalues[i][4] = fe.getAddress();
	// }
	// this.setValues(datavalues);
	// table.updateUI();
	// } else {
	// JOptionPane.showMessageDialog(jFrame, "已经是第一页");
	// }
	// }

}
发表评论
用户名: 匿名