⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 modipane.java

📁 聊天工具
💻 JAVA
字号:
package client.chat.chatpane;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.io.File;
import java.io.FilenameFilter;
import java.io.InputStream;
import java.io.OutputStream;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.DefaultComboBoxModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.ListCellRenderer;
import javax.swing.border.Border;

import utils.StaticUtils;
/**
 * 修改用户信息对话框
 * @author 洪景泉
 *
 */
public class ModiPane extends JDialog {

	private static final long serialVersionUID = -6943047066928898708L;
	// 面板
	private JPanel centerPane = null;

	private JPanel bottomPane = null;

	// 对话框面板组件
	private JLabel IDLab = null; //    编号

	private JTextField IDText = null;

	//	private JLabel nameLab = null;       //   姓名
	//	private JTextField nameText = null;

	private JLabel nickNameLab = null; //   昵称

	private JTextField nickNameText = null;

	private JLabel pwdLab1 = null; //    旧密码

	private JPasswordField pwdText1 = null;

	private JLabel pwdLab2 = null; //    新密码

	private JPasswordField pwdText2 = null;

	private JLabel pwdLab3 = null; //    确认密码

	private JPasswordField pwdText3 = null;

	private JLabel addressLab = null; //    地址

	private JTextField addressText = null;

	private JLabel ageLab = null; //    年龄

	private JTextField ageText = null;

	private JLabel departmentLab = null; //  所在部门

	private JComboBox departmentCom = null;

	private JLabel phoneLab = null; //   电话

	private JTextField phoneText = null;

	private JComboBox iconCom = null; //头像

	// 中间面板盒子
	private Box hBox1 = null;

	private Box hBox2 = null;

	private Box hBox3 = null;

	private Box hBox4 = null;

	private Box hBox5 = null;

	private Box hBox6 = null;

	private Box hBox7 = null;

	private Box hBox8 = null;

	private Box vBox1 = null;

	private Box vBox2 = null;

	// 底部面板组件
	private JButton SaveBtn = null;

	private JButton cancleBtn = null;

	// 底部面板盒子
	private Box vBoxBtn = null;

	private Box hBoxBtn = null;

	// 图片相关
	private String pic[] = null;

	//添加构造函数  flag=1 表示添加
	public ModiPane(String title, ChatFrame chatFrame, InputStream is,
			OutputStream os) {
		super(chatFrame, title);

		// 图片相关
		File f = new File("./image/newface");
		pic = f.list(new FilenameFilter() {//过滤文件

					public boolean accept(File dir, String name) {
						if (name.endsWith(".gif")) {
							if (name.endsWith("_m.gif")) {
								return false;
							} else {
								return true;
							}
						}
						return false;
					}

				});
		for (int i = 0; i < pic.length; i++) {
			int j = i + 1;
			pic[i] = j + ".gif";
		}

		// 面板
		centerPane = new JPanel();
		bottomPane = new JPanel();

		// 中间面板组件-- JLabel

		IDLab = new JLabel("用户编号:");
		nickNameLab = new JLabel("用户昵称:");
		pwdLab1 = new JLabel("旧  密  码:");
		pwdLab2 = new JLabel("新  密  码:");
		pwdLab3 = new JLabel("确认密码:");
		addressLab = new JLabel("地         址:");
		ageLab = new JLabel("年        龄:");
		departmentLab = new JLabel("所在部门:");
		phoneLab = new JLabel("联系电话:");
		// 中间面板组件-- JComboBox
		departmentCom = new JComboBox();
		departmentCom.setPreferredSize(new Dimension(105, 20));
		iconCom = new JComboBox();
		iconCom.setPreferredSize(new Dimension(100, 40));
		// 中间面板组件-- JComboBox--为部门下拉框加数据
		departmentCom.addItem("");
		departmentCom.addItem("市场部");
		departmentCom.addItem("开发部");
		departmentCom.addItem("生产部");
		departmentCom.addItem("会计部");

		// //中间面板组件-- JComboBox--为头像下拉框加图片
		iconCom.setModel(new HeadPicComboBoxModel(pic));
		iconCom.setRenderer(new HeadpicCellRenderer());
		// cmbIcon.setRenderer(new )
		// 中间面板组件-- TextField
		IDText = new JTextField(8);
		IDText.setEditable(false);
		pwdText1 = new JPasswordField(8);
		pwdText2 = new JPasswordField(8);
		pwdText3 = new JPasswordField(8);
		nickNameText = new JTextField(8);
		ageText = new JTextField(8);
		phoneText = new JTextField(8);
		addressText = new JTextField(25);
		// 中间面板盒子

		hBox1 = Box.createHorizontalBox();
		hBox2 = Box.createHorizontalBox();
		hBox3 = Box.createHorizontalBox();
		hBox4 = Box.createHorizontalBox();
		hBox5 = Box.createHorizontalBox();
		hBox6 = Box.createHorizontalBox();
		hBox7 = Box.createHorizontalBox();
		hBox8 = Box.createHorizontalBox();

		vBox1 = Box.createVerticalBox();
		vBox2 = Box.createVerticalBox();
		//				

		// 中间面板--添加组件到盒子
		hBox1.add(Box.createHorizontalStrut(20));
		hBox1.add(IDLab);
		hBox1.add(IDText);

		hBox2.add(Box.createHorizontalStrut(20));
		hBox2.add(nickNameLab);
		hBox2.add(nickNameText);

		vBox1.add(hBox1);
		vBox1.add(Box.createVerticalStrut(10));
		vBox1.add(hBox2);

		hBox3.add(iconCom);
		hBox3.add(Box.createHorizontalStrut(50));
		//将用户编号、用户姓名和用户头像放在hBox4中
		hBox4.add(vBox1);
		hBox4.add(Box.createHorizontalStrut(55));
		hBox4.add(hBox3);

		hBox5.add(Box.createHorizontalStrut(20));
		hBox5.add(pwdLab1);
		hBox5.add(pwdText1);
		hBox5.add(Box.createHorizontalStrut(25));
		hBox5.add(ageLab);
		hBox5.add(ageText);
		hBox5.add(Box.createHorizontalStrut(20));

		hBox6.add(Box.createHorizontalStrut(20));
		hBox6.add(pwdLab2);
		hBox6.add(pwdText2);
		hBox6.add(Box.createHorizontalStrut(25));
		hBox6.add(departmentLab);
		hBox6.add(departmentCom);
		hBox6.add(Box.createHorizontalStrut(20));

		hBox7.add(Box.createHorizontalStrut(20));
		hBox7.add(pwdLab3);
		hBox7.add(pwdText3);

		hBox7.add(Box.createHorizontalStrut(25));
		hBox7.add(phoneLab);
		hBox7.add(phoneText);
		hBox7.add(Box.createHorizontalStrut(20));

		hBox8.add(Box.createHorizontalStrut(20));
		hBox8.add(addressLab);
		hBox8.add(addressText);
		hBox8.add(Box.createHorizontalStrut(20));

		vBox2.add(hBox4);
		vBox2.add(Box.createVerticalStrut(10));
		vBox2.add(hBox5);
		vBox2.add(Box.createVerticalStrut(10));
		vBox2.add(hBox6);
		vBox2.add(Box.createVerticalStrut(10));
		vBox2.add(hBox7);
		vBox2.add(Box.createVerticalStrut(10));
		vBox2.add(hBox8);
		vBox2.add(Box.createVerticalStrut(10));

		// 中间面板--添加盒子到面板中
		centerPane.setLayout(new BorderLayout());
		centerPane.add(vBox2);
		//centerPane.setBackground(GlobalVar.BORDER_COLOR);
		centerPane.setBorder(BorderFactory.createTitledBorder(BorderFactory
				.createLineBorder(new Color(144, 185, 229)), "用户信息列表"));
		// 底部面板组件
		cancleBtn = new JButton("取 消");
		cancleBtn.setActionCommand("cancle");
		SaveBtn = new JButton("确 定");
		SaveBtn.setActionCommand("ok");
		// 底部面板盒子
		vBoxBtn = Box.createVerticalBox();
		hBoxBtn = Box.createHorizontalBox();
		// 底部面板--添加组件到盒子中

		hBoxBtn.add(SaveBtn);
		hBoxBtn.add(Box.createHorizontalStrut(60));
		hBoxBtn.add(cancleBtn);

		vBoxBtn.add(hBoxBtn);
		vBoxBtn.add(Box.createVerticalStrut(10));

		// 底部面板--添加盒子到面板中
		bottomPane.add(vBoxBtn);
		//	bottomPane.setBackground(GlobalVar.BORDER_COLOR);

		//初始化

		//						System.out.println("****************");
		//						System.out.println(GlobalVar.currentUser.get("userID"));
		//						System.out.println(GlobalVar.currentUser.get("userName"));
		//						System.out.println(GlobalVar.currentUser.get("userNickName"));
		//						System.out.println(GlobalVar.currentUser.get("userPassword"));
		//						System.out.println(GlobalVar.currentUser.get("userAge"));
		//						System.out.println(GlobalVar.currentUser.get("userTelephone"));
		//						System.out.println(GlobalVar.currentUser.get("userDpm"));
		//						System.out.println(GlobalVar.currentUser.get("userAddress"));
		//						System.out.println(GlobalVar.currentUser.get("userOnline"));
		//						System.out.println(GlobalVar.currentUser.get("userIcon"));					

		IDText.setText((String) StaticUtils.currentUser.get("userID"));
		nickNameText.setText(StaticUtils.currentUser.get("userNickName")
				.equals("null") ? "" : StaticUtils.currentUser.get(
				"userNickName").toString());
		if (!(StaticUtils.currentUser.get("userAge").equals("0"))) {
			ageText.setText((String) StaticUtils.currentUser.get("userAge"));
		}
		departmentCom.setSelectedItem((String) StaticUtils.currentUser
				.get("userDpm"));
		phoneText.setText(StaticUtils.currentUser.get("userTelephone").equals(
				"null") ? "" : StaticUtils.currentUser.get("userTelephone")
				.toString());
		addressText.setText(StaticUtils.currentUser.get("userAddress").equals(
				"null") ? "" : StaticUtils.currentUser.get("userAddress")
				.toString());
		iconCom
				.setSelectedIndex(Integer
						.parseInt((String) StaticUtils.currentUser
								.get("userIcon")) - 1);
		// 添加面板到中间容器中
		this.setModal(true);
		this.add(centerPane);
		this.add(bottomPane, BorderLayout.SOUTH);
		//事件
		ModiPaneEvent mpe = new ModiPaneEvent(this, is, os);
		cancleBtn.setActionCommand("cancle");
		SaveBtn.setActionCommand("save");
		cancleBtn.addActionListener(mpe);
		SaveBtn.addActionListener(mpe);
	}

	public JTextField getAgeText() {
		return ageText;
	}

	public JTextField getAddressText() {
		return addressText;
	}

	public JTextField getIDText() {
		return IDText;
	}

	public JTextField getNickNameText() {
		return nickNameText;
	}

	public JTextField getPhoneText() {
		return phoneText;
	}

	public JComboBox getDepartmentCom() {
		return departmentCom;
	}

	//内部类:定义头像下拉列表框模型 
	class HeadPicComboBoxModel extends DefaultComboBoxModel {

		private static final long serialVersionUID = -3201732171831269061L;

		public HeadPicComboBoxModel(String[] pics) {
			for (int i = 0; i < pics.length; ++i) {
				// 添加显示的对象
				addElement(new Object[] { new ImageIcon("./image/newface/"
						+ pics[i]) });
			}
		}

		// 在HeadpicCellRenderer中要用到
		public Icon getIcon(Object object) {
			Object[] array = (Object[]) object;
			return (Icon) array[0];
		}
	}

	// ComboBox显示方式
	class HeadpicCellRenderer extends JLabel implements ListCellRenderer {
		//
		private static final long serialVersionUID = 1L;

		private Border lineBorder = BorderFactory
				.createLineBorder(Color.red, 2), emptyBorder = BorderFactory
				.createEmptyBorder(2, 2, 2, 2);

		public HeadpicCellRenderer() {
			setOpaque(true);
		}

		public Component getListCellRendererComponent(JList list, Object value,
				int index, boolean isSelected, boolean cellHasFocus) {
			HeadPicComboBoxModel model = (HeadPicComboBoxModel) list.getModel();
			setIcon(model.getIcon(value));
			if (isSelected) {
				setForeground(list.getSelectionForeground());
				setBackground(list.getSelectionBackground());
			} else {
				setForeground(list.getForeground());
				setBackground(list.getBackground());
			}
			if (cellHasFocus)
				setBorder(lineBorder);
			else
				setBorder(emptyBorder);
			return this;
		}
	}

	public JComboBox getIconCom() {
		return iconCom;
	}

	public void setIconCom(JComboBox iconCom) {
		this.iconCom = iconCom;
	}

	public JPasswordField getPwdText1() {
		return pwdText1;
	}

	public JPasswordField getPwdText2() {
		return pwdText2;
	}

	public JPasswordField getPwdText3() {
		return pwdText3;
	}

	//		public static void main(String[] args) {
	//			JFrame jf=new JFrame();
	//			modiPane mod=new modiPane("修改用户信息",jf);
	//			mod.setSize(420,320);
	//			mod.setModal(true);
	//			mod.setVisible(true);
	//			
	//			//mod.setLocationRelativeTo(jf);
	//			mod.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
	//		}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -