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

📄 clientinfogui.java

📁 这是个用java写的qq的多线程聊天的功能,有客户段和服务端,在eclipse里可以自动运行连接数据库
💻 JAVA
字号:
package smoker.client;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import smoker.server.IUserManager;
import smoker.tools.*;

public class ClientInfoGUI extends JFrame implements ActionListener, IUserManager {
	private static final long serialVersionUID = 1L;
	private JTextField tfLoginID;
	private JPasswordField tfPassword;
	private JPasswordField tfAgainPassword;
	private JTextField tfNickName;
	private JTextField tfName;
	private JTextField tfBirthday;
	private JRadioButton rbtMale;
	private JRadioButton rbtFemale;
	private JTextField tfEmail;
	private JTextField tfDpt;
	private JTextField tfAddress;
	private JTextField tfPhone;
	private JLabel labSelectPhoto;
	private JTextArea taMyMemo;
	private JButton btnOK;
	private JButton btnCancel;
	private Container container;
	private GridBagLayout gbl = new GridBagLayout();
	private GridBagConstraints gbc = new GridBagConstraints();
	private IClientCenter iClientCenter;
	private User user;
	private IClientGUI iClientGUI;
	private PhotoPanel winPhotos;
	
	public ClientInfoGUI(IClientGUI iClientGUI, User user, IClientCenter iClientCenter) {
		this.iClientGUI = iClientGUI;
		this.winPhotos = new PhotoPanel(this);
		this.user = user;
		this.iClientCenter = iClientCenter;
		this.setTitle("修改资料 -- " + user.getNickName());
		this.setSize(470, 440);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		GUIManager.CenterWindow(this);
		this.setResizable(false);
		this.init();
	}

	private void init() {
		container = this.getContentPane();
		container.setLayout(new BorderLayout());
		tfLoginID = new JTextField(10);
		tfLoginID.setEditable(false);
		tfLoginID.setText(String.valueOf(user.getLoginID()));
		tfPassword = new JPasswordField(10);
		tfPassword.setEchoChar('*');
		tfPassword.setText(user.getPassword());
		tfPassword.setToolTipText("若不需要修改密码,就请不要动它!");
		tfAgainPassword = new JPasswordField(10);
		tfAgainPassword.setEchoChar('*');
		tfAgainPassword.setText(user.getPassword());
		tfAgainPassword.setToolTipText("若不需要修改密码,就请不要动它!");
		tfNickName = new JTextField(10);
		tfNickName.setText(user.getNickName());
		tfName = new JTextField(10);
		tfName.setText(user.getName());
		tfBirthday = new JTextField(10);
		tfBirthday.setText(user.getBirthday());
		tfEmail = new JTextField(10);
		tfEmail.setText(user.getEmail());
		tfAddress = new JTextField(35);
		tfAddress.setText(user.getAddress());
		tfPhone = new JTextField(10);
		tfPhone.setText(user.getTelphone());
		rbtMale = new JRadioButton("男", true);
		rbtFemale = new JRadioButton("女");
		labSelectPhoto = new JLabel(user.getIcon());
		ButtonGroup bg = new ButtonGroup();
		bg.add(rbtMale);
		bg.add(rbtFemale);
		loadSex();
		taMyMemo = new JTextArea(5, 1);
		taMyMemo.setLineWrap(true);
		taMyMemo.setText(user.getMemo());
		btnOK = GUIManager.createJButton(null, new ImageIcon("image/ok.png"),this, "btnOK");
		btnCancel = GUIManager.createJButton(null, new ImageIcon("image/cancel.png"), this, "btnCancel");
		container.add(getRootPanel());
		
		labSelectPhoto.addMouseListener(new MouseAdapter(){
			public void mouseClicked(MouseEvent e){
				int x = getPoint().x;
				int y = getPoint().y;
				winPhotos.setVisible(true);
				winPhotos.setLocation(x + 120, y + 110);
			}
		});
		this.addComponentListener(new ComponentListener(){
			public void componentHidden(ComponentEvent e) {}
			public void componentMoved(ComponentEvent e) {
				int x = getPoint().x;
				int y = getPoint().y;
				winPhotos.setLocation(x + 120, y + 110);
			}
			public void componentResized(ComponentEvent e) {}
			public void componentShown(ComponentEvent e) {
				winPhotos.dispose();
			}
		});
	}
	
	private Point getPoint(){
		return this.getLocation();
	}
	
	private void loadSex() {
		if(user.getSex().trim().equals("男")) {
			rbtMale.setSelected(true);
		}else {
			rbtFemale.setSelected(true);
		}
	}
	
	private JPanel getRootPanel() {
		JPanel rootPanel = new JPanel();
		rootPanel.setLayout(new BoxLayout(rootPanel, BoxLayout.Y_AXIS));
		rootPanel.setBorder(BorderFactory.createTitledBorder("请输入用户信息"));

		rootPanel.add(getBasicPanel());
		rootPanel.add(getParticularPanel());
		rootPanel.add(getMemoPanel());
		rootPanel.add(getBtnPanel());

		return rootPanel;
	}

	private JPanel getBasicPanel() {
		JPanel basicPanel = new JPanel();
		basicPanel.setLayout(gbl);
		basicPanel.setBorder(BorderFactory.createTitledBorder("基本信息"));

		JLabel labLoginID = GUIManager.createJLabel("登录名:");
		JLabel labPassword = GUIManager.createJLabel("口令:");
		JLabel labAgainPassword = GUIManager.createJLabel("确认口令:");
		JLabel labHeadPortrait = GUIManager.createJLabel("头像:");
		
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.anchor = GridBagConstraints.EAST;
		GUIManager.setConstraints(gbl, gbc, 0, 0, 1, 1, 1, 1);
		basicPanel.add(labHeadPortrait, gbc);
		gbc.anchor = GridBagConstraints.WEST;
		GUIManager.setConstraints(gbl, gbc, 1, 0, 1, 1, 1, 1);
		basicPanel.add(labSelectPhoto, gbc);
		gbc.anchor = GridBagConstraints.EAST;
		GUIManager.setConstraints(gbl, gbc, 2, 0, 1, 1, 1, 1);
		basicPanel.add(labLoginID, gbc);
		gbc.anchor = GridBagConstraints.WEST;
		GUIManager.setConstraints(gbl, gbc, 3, 0, 1, 1, 1, 1);
		basicPanel.add(tfLoginID, gbc);
		gbc.anchor = GridBagConstraints.EAST;
		GUIManager.setConstraints(gbl, gbc, 0, 1, 1, 1, 1, 1);
		basicPanel.add(labPassword, gbc);
		gbc.anchor = GridBagConstraints.WEST;
		GUIManager.setConstraints(gbl, gbc, 1, 1, 1, 1, 1, 1);
		basicPanel.add(tfPassword, gbc);
		gbc.anchor = GridBagConstraints.EAST;
		GUIManager.setConstraints(gbl, gbc, 2, 1, 1, 1, 1, 1);
		basicPanel.add(labAgainPassword, gbc);
		gbc.anchor = GridBagConstraints.WEST;
		GUIManager.setConstraints(gbl, gbc, 3, 1, 1, 1, 1, 1);
		basicPanel.add(tfAgainPassword, gbc);

		return basicPanel;
	}

	private JPanel getParticularPanel() {
		JPanel particularPanel = new JPanel();
		particularPanel.setLayout(gbl);
		particularPanel.setBorder(BorderFactory.createTitledBorder("详细信息"));

		JLabel labNickName = GUIManager.createJLabel("昵称:");
		JLabel labName = GUIManager.createJLabel("真实姓名:");
		JLabel labBirthday = GUIManager.createJLabel("生日:");
		JLabel labEmail = GUIManager.createJLabel("Email:");
		JLabel labPhone = GUIManager.createJLabel("联系电话:");
		JLabel labSex = GUIManager.createJLabel("性别:");
		JLabel labAddress = GUIManager.createJLabel("住址:");

		JPanel sexPanel = new JPanel();
		sexPanel.setLayout(new GridLayout(1, 2));
		sexPanel.add(rbtMale);
		sexPanel.add(rbtFemale);

		gbc.anchor = GridBagConstraints.EAST;
		GUIManager.setConstraints(gbl, gbc, 0, 2, 1, 1, 1, 1);
		particularPanel.add(labNickName, gbc);
		gbc.fill = GridBagConstraints.HORIZONTAL;
		GUIManager.setConstraints(gbl, gbc, 1, 2, 1, 1, 1, 1);
		particularPanel.add(tfNickName, gbc);
		gbc.anchor = GridBagConstraints.EAST;
		GUIManager.setConstraints(gbl, gbc, 2, 2, 1, 1, 1, 1);
		particularPanel.add(labName, gbc);
		gbc.anchor = GridBagConstraints.WEST;
		GUIManager.setConstraints(gbl, gbc, 3, 2, 1, 1, 1, 1);
		particularPanel.add(tfName, gbc);

		gbc.anchor = GridBagConstraints.EAST;
		GUIManager.setConstraints(gbl, gbc, 0, 3, 1, 1, 1, 1);
		particularPanel.add(labSex, gbc);
		gbc.anchor = GridBagConstraints.WEST;
		GUIManager.setConstraints(gbl, gbc, 1, 3, 1, 1, 1, 1);
		particularPanel.add(sexPanel, gbc);
		gbc.anchor = GridBagConstraints.EAST;
		GUIManager.setConstraints(gbl, gbc, 2, 3, 1, 1, 1, 1);
		particularPanel.add(labBirthday, gbc);
		gbc.anchor = GridBagConstraints.WEST;
		GUIManager.setConstraints(gbl, gbc, 3, 3, 1, 1, 1, 1);
		particularPanel.add(tfBirthday, gbc);

		gbc.anchor = GridBagConstraints.EAST;
		GUIManager.setConstraints(gbl, gbc, 0, 4, 1, 1, 1, 1);
		particularPanel.add(labEmail, gbc);
		gbc.anchor = GridBagConstraints.WEST;
		GUIManager.setConstraints(gbl, gbc, 1, 4, 1, 1, 1, 1);
		particularPanel.add(tfEmail, gbc);
		gbc.anchor = GridBagConstraints.EAST;
		GUIManager.setConstraints(gbl, gbc, 2, 4, 1, 1, 1, 1);
		particularPanel.add(labPhone, gbc);
		gbc.anchor = GridBagConstraints.WEST;
		GUIManager.setConstraints(gbl, gbc, 3, 4, 1, 1, 1, 1);
		particularPanel.add(tfPhone, gbc);

		JLabel labDpt = new JLabel("所属部门:");
		tfDpt = new JTextField();
		tfDpt.setEditable(false);
		tfDpt.setText(user.getDepartmentName());
		gbc.fill = GridBagConstraints.HORIZONTAL;
		GUIManager.setConstraints(gbl, gbc, 0, 5, 1, 1, 1, 1);
		particularPanel.add(labDpt, gbc);
		gbc.anchor = GridBagConstraints.WEST;
		GUIManager.setConstraints(gbl, gbc, 1, 5, 3, 1, 1, 1);
		particularPanel.add(tfDpt, gbc);
		
		gbc.anchor = GridBagConstraints.EAST;
		GUIManager.setConstraints(gbl, gbc, 0, 6, 1, 1, 1, 1);
		particularPanel.add(labAddress, gbc);
		gbc.anchor = GridBagConstraints.WEST;
		GUIManager.setConstraints(gbl, gbc, 1, 6, 3, 1, 1, 1);
		particularPanel.add(tfAddress, gbc);

		return particularPanel;
	}

	private JPanel getMemoPanel() {
		JPanel memoPanel = new JPanel();
		memoPanel.setLayout(new BorderLayout());
		memoPanel.setBorder(BorderFactory.createTitledBorder("个人说明"));
		memoPanel.add(new JScrollPane(taMyMemo), BorderLayout.CENTER);
		return memoPanel;
	}

	private JPanel getBtnPanel() {
		JPanel btnPanel = new JPanel();
		btnPanel.setLayout(new BoxLayout(btnPanel, BoxLayout.X_AXIS));
		btnPanel.add(btnOK);
		btnPanel.add(btnCancel);
		return btnPanel;
	}

	private String getSex() {
		if (rbtMale.isSelected() == true) {
			return "男";
		} else {
			return "女";
		}
	}

	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
		if (command.equals("btnOK")) {
			btnOK.setEnabled(false);
			btnCancel.setEnabled(false);
			String loginID = tfLoginID.getText().trim();
			String password = new String(this.tfPassword.getPassword());
			String againPwd = new String(this.tfAgainPassword.getPassword());
			String nickName = tfNickName.getText().trim();
			String name = tfName.getText().trim();
			String birthday = tfBirthday.getText().trim();
			String email = tfPhone.getText().trim();
			String dptName = tfDpt.getText().trim();
			String address = tfAddress.getText().trim();
			String sex = getSex();
			String memo = taMyMemo.getText().trim();
			String photo = this.labSelectPhoto.getIcon().toString();
			String phone = tfPhone.getText().trim();
			if (!dataVlidate(password, againPwd, nickName, name, birthday,
					email, address, phone, memo)) {
				btnOK.setEnabled(true);
				btnCancel.setEnabled(true);
				return;
			}
			User updateUser = new User();
			updateUser.setAddress(address);
			updateUser.setBirthday(birthday);
			updateUser.setDepartmentName(dptName);
			updateUser.setEmail(email);
			updateUser.setIcon(new ImageIcon(photo));
			updateUser.setLoginID(loginID);
			updateUser.setMemo(memo);
			updateUser.setName(name);
			updateUser.setNickName(nickName);
			updateUser.setPassword(password);
			updateUser.setSex(sex);
			updateUser.setTelphone(phone);
			user = updateUser;
			iClientCenter.updateUser(updateUser);
		} else {
			dispose();
		}
	}
	
	public void onUpdateResult(int result) {
		if(result == MsgType.YTPE_UPDATEOK) {
			Msg.show("资料更新成功!");
			iClientGUI.displayData(user);
			btnOK.setEnabled(true);
			btnCancel.setEnabled(true);
		}
	}
	
	private boolean dataVlidate(String password, String againPwd,
			String nickName, String name, String birthday, String email,
			String address, String phone, String memo) {
		if (password.length() == 0) {
			Msg.show("口令不能为空!");
			return false;
		} else if (password.length() < 6 || password.length() > 16) {
			Msg.show("口令长度应在 6 - 16 位之间!");
			return false;
		} else if (!password.equals(againPwd)) {
			Msg.show("两次口令输入不一致!");
			return false;
		} else if (nickName.length() == 0) {
			Msg.show("昵称不能为空!");
			return false;
		} else if (nickName.length() > 16) {
			Msg.show("昵称的长度不能超过 16 位!");
			return false;
		} else if (name.length() > 5) {
			Msg.show("真实姓名长度不能超过 5 位!");
			return false;
		} else if (name.length() == 0) {
			Msg.show("真实姓名不能为空!");
			return false;
		} else if (birthday.length() != 0 && !DataManager.isDate(birthday)) {
			Msg.show("请正确填写您的出生日期!");
			return false;
		} else if (email.length() != 0 && !DataManager.isEmail(email)) {
			Msg.show("请正确填写您的Email!");
			return false;
		} else if (phone.length() > 15) {
			Msg.show("电话号码的长度不能超过 15 位!");
			return false;
		} else if (address.length() > 100) {
			Msg.show("住址的长度不能超过 100 位!");
			return false;
		} else if (memo.length() > 150) {
			Msg.show("个人说明的文字过长!");
			return false;
		}
		return true;
	}

	public void icon(ImageIcon icon) {
		labSelectPhoto.setIcon(icon);
		
	}
}

⌨️ 快捷键说明

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