📄 registerpane.java
字号:
/**
* @(#)client.frm.RegisterPane.java 2008-8-29
* Copy Right Information : Tarena
* Project : JavaQQ
* JDK version used : jdk1.6.4
* Comments : 新用户注册类。
* Version : 1.0
* Sr Date Modified By Why & What is modified
* 1. 2008-8-29 小猪 新建
**/
package client.frm;
import java.awt.Color;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import tools.DateDeal;
import data.JQMessage;
import data.Portrait;
import data.RegUser;
import data.User;
import data.UserSex;
import data.UserState;
/**
* 新用户注册类。
* 2008-8-29
* @author 达内科技[Tarena Training Group]
* @version 1.0
* @since JDK1.6(建议)
*/
public class RegisterPane extends JFrame implements ActionListener{
private JLabel lblNickName = new JLabel("用户昵称:");
private JLabel lblEmail = new JLabel("E-mail:");
private JLabel lblPassword = new JLabel("登录密码:");
private JLabel lblRePass = new JLabel("重复输入:");
private JLabel lblSex = new JLabel("性 别:");
private JLabel lblAge = new JLabel("年龄:");
private JLabel lblRealName = new JLabel("姓名:");
private JLabel lblSignature = new JLabel("个性签名:");
private JTextField txtNickName = new JTextField();
private JTextField txtEmail= new JTextField();
private JPasswordField pfPassword = new JPasswordField();
private JPasswordField pfRePass = new JPasswordField();
private JComboBox boxSex = new JComboBox();
private JTextField txtAge = new JTextField();
private JTextField txtRealName = new JTextField();
private JTextArea areaSignature = new JTextArea();
private JLabel lblPhoto = new JLabel();
private JButton btnChange = new JButton("更改头像");
private JButton btnSet = new JButton("设置↓");
private JButton btnOk = new JButton("注册");
private JButton btnCancle = new JButton("取消");
private ChooseProtrait chooseProtrait = null;
private JLabel lblServerIP = new JLabel("服务器IP:");
private JTextField txtServerIP = new JTextField("127.0.0.1");
private JLabel lblServerPort = new JLabel("端口:");
private JTextField txtServerPort = new JTextField("3608");
private boolean isSet = false;
private Socket client = null;
private ObjectOutputStream oos = null;
private ObjectInputStream ois = null;
public RegisterPane() {
setTitle("JQ新用户注册");
setSize(330,343);
setResizable(false);
Toolkit tk=Toolkit.getDefaultToolkit();
setLocation((tk.getScreenSize().width-getSize().width)/2,(tk.getScreenSize().height-getSize().height)/2);
init();
btnCancle.addActionListener(this);
btnChange.addActionListener(this);
btnOk.addActionListener(this);
btnSet.addActionListener(this);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
/**
* 初始化面板。因为时间关系,可能写的比较戳,望见谅。
*/
private void init(){
setLayout(null);
lblNickName.setPreferredSize(new Dimension(60,20));
lblEmail.setPreferredSize(new Dimension(60,20));
lblPassword.setPreferredSize(new Dimension(60,20));
lblRePass.setPreferredSize(new Dimension(60,20));
lblSex.setPreferredSize(new Dimension(60,20));
lblAge.setPreferredSize(new Dimension(30,20));
lblRealName.setPreferredSize(new Dimension(30,20));
lblSignature.setPreferredSize(new Dimension(60,20));
txtNickName.setPreferredSize(new Dimension(120,20));
txtEmail.setPreferredSize(new Dimension(120,20));
pfPassword.setPreferredSize(new Dimension(120,20));
pfRePass.setPreferredSize(new Dimension(120,20));
boxSex.setPreferredSize(new Dimension(40,20));
txtAge.setPreferredSize(new Dimension(40,20));
txtRealName.setPreferredSize(new Dimension(60,20));
JScrollPane sp = new JScrollPane(areaSignature);
sp.setPreferredSize(new Dimension(220,60));
lblPhoto.setOpaque(true);
lblPhoto.setBackground(Color.WHITE);
lblPhoto.setHorizontalAlignment(SwingConstants.CENTER);
lblPhoto.setPreferredSize(new Dimension(50,50));
lblPhoto.setBorder(new LineBorder(Color.DARK_GRAY));
lblPhoto.setIcon(new Portrait());
btnChange.setPreferredSize(new Dimension(60,20));
btnChange.setMargin(new Insets(0,0,0,0));
boxSex.addItem(UserSex.Boy);
boxSex.addItem(UserSex.Girl);
JPanel paneRequire = new JPanel();
paneRequire.setBorder(new TitledBorder(new LineBorder(Color.GRAY),"必填选项"));
paneRequire.setSize(210,135);
paneRequire.setLocation(10, 10);
paneRequire.setLayout(new FlowLayout(FlowLayout.LEFT,5,6));
paneRequire.add(lblNickName);
paneRequire.add(txtNickName);
paneRequire.add(lblEmail);
paneRequire.add(txtEmail);
paneRequire.add(lblPassword);
paneRequire.add(pfPassword);
paneRequire.add(lblRePass);
paneRequire.add(pfRePass);
JPanel paneUnRequire = new JPanel();
paneUnRequire.setBorder(new TitledBorder(new LineBorder(Color.GRAY),"选填选项"));
paneUnRequire.setSize(305,125);
paneUnRequire.setLocation(10, 150);
paneUnRequire.setLayout(new FlowLayout(FlowLayout.LEFT,5,6));
paneUnRequire.add(lblSex);
paneUnRequire.add(boxSex);
paneUnRequire.add(lblAge);
paneUnRequire.add(txtAge);
paneUnRequire.add(lblRealName);
paneUnRequire.add(txtRealName);
paneUnRequire.add(lblSignature);
paneUnRequire.add(sp);
JPanel panePhoto = new JPanel();
panePhoto.setBorder(new TitledBorder(new LineBorder(Color.GRAY),"头像"));
panePhoto.setSize(85,135);
panePhoto.setLocation(230, 10);
panePhoto.setLayout(new FlowLayout(FlowLayout.CENTER,5,8));
panePhoto.add(new FillWidth(50,4));
panePhoto.add(lblPhoto);
panePhoto.add(btnChange);
JPanel paneBottom = new JPanel();
paneBottom.setSize(305,30);
paneBottom.setLocation(10, 275);
paneBottom.setLayout(new FlowLayout(FlowLayout.LEFT,2,5));
paneBottom.add(btnSet);
paneBottom.add(new FillWidth(100,20));
paneBottom.add(btnOk);
paneBottom.add(new FillWidth(8,20));
paneBottom.add(btnCancle);
TitledBorder tb = new TitledBorder(new LineBorder(Color.GRAY),"网络设置");
JPanel paneSet = new JPanel();
paneSet.setSize(305,60);
paneSet.setLocation(10,313);
paneSet.setBorder(tb);
paneSet.add(lblServerIP);
paneSet.add(txtServerIP);
paneSet.add(new FillWidth(30,20));
paneSet.add(lblServerPort);
paneSet.add(txtServerPort);
add(paneRequire);
add(paneUnRequire);
add(panePhoto);
add(paneBottom);
add(paneSet);
}
/**
* 取消按钮、更改头像按钮、注册按钮、设置按钮事件。
*/
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btnCancle){
dispose();
new LoginPane();
}
if(e.getSource()==btnChange){
if(chooseProtrait==null)
new ChooseProtrait();
else
chooseProtrait.setVisible(true);
}
if(e.getSource()==btnOk){
try {
String pass = new String(pfPassword.getPassword());
String repass = new String(pfRePass.getPassword());
if(pass.equals(repass)){
btnOk.setEnabled(false);
new RegNewUser().start();
}
else
JOptionPane.showMessageDialog(null, "2次密码输入的不一致,请重新输入!");
} catch (IOException e1) {
JOptionPane.showMessageDialog(null, "错误:"+e1.getMessage());
}
}
if(e.getSource()==btnSet){
if(isSet){
isSet = false;
setSize(getWidth(),getHeight()-65);
btnSet.setText("设置↓");
}else{
isSet = true;
setSize(getWidth(),getHeight()+65);
btnSet.setText("设置↑");
}
}
}
/**
* 注册新用户线程
* 2008-9-27
* @author 达内科技[Tarena Training Group]
* @version 1.0
* @since JDK1.6(建议)
*/
private class RegNewUser extends Thread{
public RegNewUser() throws IOException {
client = new Socket(txtServerIP.getText(),Integer.parseInt(txtServerPort.getText()));
oos = new ObjectOutputStream(new BufferedOutputStream(client.getOutputStream()));
User user = new User();
user.setRealname(txtRealName.getText());
user.setNickname(txtNickName.getText());
user.setEmail(txtEmail.getText());
user.setPassword(new String(pfPassword.getPassword()));
user.setSex(((UserSex)boxSex.getSelectedItem()).getType());
user.setAge(Integer.parseInt(txtAge.getText()));
user.setSignature(areaSignature.getText());
user.setPhoto(((Portrait)lblPhoto.getIcon()).getNum());
JQMessage regMessage = new JQMessage();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -