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

📄 register.java

📁 JAVA网络聊天室.运用网络编程、文件操作、数据流处理以及多线程等基础技术编写一个网络聊天室.
💻 JAVA
字号:
/**********************************************************************************/
/////////////////////////////////////客户注册机构////////////////////////////////////
/**********************************************************************************/
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class Register extends JFrame implements ActionListener
{
    static String r_serverIP;   //记录服务器IP地址
    static int r_PORT;
    
    JLabel lblHeading;
    JLabel lblUserName;
    JLabel lblUserPwd;
    JLabel lblCnfUserPwd;
    JLabel lblPetName;
    JLabel lblAge;
    JLabel lblEmpId;
    JLabel lblSex;

    String usrName;
    char[] usrPwd;
    char[] cnfPwd;
    String PetName;
    String age;
    String empid;

    Socket toServer;
    ObjectInputStream streamFromServer;
    PrintStream streamToServer;

    JComboBox lstSex;

    JTextField txtUserName;
    JTextField txtEmpId;
    JTextField txtPetName;
    JPasswordField txtUsrPwd;
    JPasswordField txtCnfUsrPwd;
    JTextField txtAge;
    Font font;
    Color r;
    JButton btnSubmit;
    JButton btnCancel;

    public Register(String l_serverIP, int l_PORT)
    {
        r_serverIP = l_serverIP;
        r_PORT = l_PORT;
        this.setTitle("新用户注册");
	JPanel panel = new JPanel();

	//应用布局管理器
	panel.setLayout(new GridBagLayout());
	GridBagConstraints gbCons = new GridBagConstraints();

	//放置组件
	gbCons.gridx = 0;
	gbCons.gridy = 0;
	lblHeading = new JLabel("注册信息");
	Font font = new Font("Monospaced", Font.BOLD, 20);
	lblHeading.setFont(font);
	//Color color = new Color(0, 200, 0);
	lblHeading.setForeground(new Color(131, 25, 38));
	lblHeading.setVerticalAlignment(SwingConstants.TOP);
	gbCons.anchor = GridBagConstraints.WEST;
	panel.add(lblHeading, gbCons);

	gbCons.gridx = 0;
	gbCons.gridy = 1;
	lblUserName = new JLabel("用户账号:");
	gbCons.anchor = GridBagConstraints.WEST;
	panel.add(lblUserName, gbCons);

	gbCons.gridx = 1;
	gbCons.gridy = 1;
	txtUserName = new JTextField(15);
	panel.add(txtUserName, gbCons);
        
        gbCons.gridx = 0;
	gbCons.gridy = 2;
	lblEmpId = new JLabel("用户ID:");
	panel.add(lblEmpId, gbCons);

	gbCons.gridx = 1;
	gbCons.gridy = 2;
	txtEmpId = new JTextField(15);
	panel.add(txtEmpId, gbCons);

	gbCons.gridx = 0;
	gbCons.gridy = 3;
	lblPetName = new JLabel("昵称:");
	panel.add(lblPetName, gbCons);

	gbCons.gridx = 1;
	gbCons.gridy = 3;
	txtPetName = new JTextField(15);
	panel.add(txtPetName, gbCons);

	gbCons.gridx = 0;
	gbCons.gridy = 4;
	lblUserPwd = new JLabel("设定密码:");
	panel.add(lblUserPwd, gbCons);

	gbCons.gridx = 1;
	gbCons.gridy = 4;
	txtUsrPwd = new JPasswordField(15);
	panel.add(txtUsrPwd, gbCons);

	gbCons.gridx = 0;
	gbCons.gridy = 5;
	lblCnfUserPwd = new JLabel("确认密码:");

	panel.add(lblCnfUserPwd, gbCons);

	gbCons.gridx = 1;
	gbCons.gridy = 5;
	txtCnfUsrPwd = new JPasswordField(15);
	panel.add(txtCnfUsrPwd, gbCons);

	gbCons.gridx = 0;
	gbCons.gridy = 7;
	lblAge = new JLabel("年龄:");
	panel.add(lblAge, gbCons);

	gbCons.gridx = 1;
	gbCons.gridy = 7;
	txtAge = new JTextField(3);
	panel.add(txtAge, gbCons);

	gbCons.gridx = 0;
	gbCons.gridy = 8;
	lblSex = new JLabel("性别:");
	panel.add(lblSex, gbCons);

	gbCons.gridx = 1;
	gbCons.gridy = 8;
	String[] sex = { "男", "女" };
	JComboBox lstSex = new JComboBox(sex);
	lstSex.setSelectedIndex(0);
	panel.add(lstSex, gbCons);

	JPanel btnPanel = new JPanel();

	btnSubmit = new JButton("提交");
	btnPanel.add(btnSubmit);
	btnSubmit.addActionListener(this); //为提交按钮添加监听器
	btnCancel = new JButton("取消");
	btnPanel.add(btnCancel);
	btnCancel.addActionListener(this); //为取消按钮添加监听器*/

	gbCons.gridx = 0;
	gbCons.gridy = 9;
	gbCons.anchor = GridBagConstraints.EAST;
	panel.add(btnPanel, gbCons);

	getContentPane().add(panel);

	setDefaultCloseOperation(EXIT_ON_CLOSE);
	setSize(350, 300);
	Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();
        setLocation((screen.width-350)/2,(screen.height-300)/2);
	setVisible(true);
        setResizable(false);
    }

    public void actionPerformed(ActionEvent E)
    {
        JButton button = (JButton)E.getSource(); //获取事件源
	if (button.equals(btnCancel))
	{
            new Login(r_serverIP, r_PORT);
            this.dispose();
        }
	else
	{
            int ver = verify();  //调用方法verify()测试数据有效性
            if (ver == 1)
            {
                try
                {
                    //建立socket连接并创建一个Socket输入输出流
                    toServer = new Socket(r_serverIP, r_PORT);
                    streamFromServer = new ObjectInputStream(toServer.getInputStream());
                    streamToServer = new PrintStream(toServer.getOutputStream());

                    //向服务器发送注册信息
                    streamToServer.println("RegisterInfo");
                    usrName = txtUserName.getText();
                    //PetName = PetName = txtPetName.getText();
                    usrPwd = txtUsrPwd.getPassword();
                    String pwd = new String(usrPwd);

                    //向服务器发送用户名和密码
                    streamToServer.println(usrName +  ":" + pwd);

                    //读取服务器返回消息
                    String frmServer = (String)streamFromServer.readObject();
                    if (frmServer.equals("Registered"))
                    {
                        new Login(r_serverIP, r_PORT);
                        this.dispose();
                    }
                    else if (frmServer.equals("User Exists"))
                    {
                        showUsrExists(); //显示错误信息
                    }
                }
                catch (Exception e)
		{
                    System.out.println("发生异常 " + e);
		}
            }
        }
    }
    
    int verify()    //测试数据的有效性
    {
        int ctr = 0;
	int intAge = 0;
	try
	{
            usrName = txtUserName.getText();
            usrPwd = txtUsrPwd.getPassword();
            cnfPwd = txtCnfUsrPwd.getPassword();
            PetName = txtPetName.getText();
            age = txtAge.getText();
            empid = txtEmpId.getText();
            String strUsrPwd = new String(usrPwd);
            String strCnfPwd = new String(cnfPwd);
            try
            {
                intAge = (int)Integer.parseInt(age.trim());
            }
            catch (Exception e)
            {
                showErrordlgInt();
            }
            if ((usrName.length() > 0) && (strUsrPwd.length() > 0) && (strCnfPwd.length() > 0) && (PetName.length() > 0) && (intAge > 0) && (intAge < 150) && (empid.length() > 0) && (strUsrPwd.equals(strCnfPwd)))
            {
                JOptionPane.showMessageDialog(null, "注册请求已受理", "注册提示", JOptionPane.CLOSED_OPTION);
		ctr = 1;
		return ctr;
            }
            else
            {
                showErrordlg();
            }
        }
	catch (Exception e)
	{
            System.out.println("抛出异常 " + e);
	}
	return ctr;
    }	

    void showUsrExists()
    {
        JOptionPane.showMessageDialog(this, "用户已存在 ", "错误消息", JOptionPane.ERROR_MESSAGE);
    }

    int flg = 0;

    void showErrordlg()
    {
        JOptionPane.showMessageDialog(this, "非法输入 ", "错误消息", JOptionPane.ERROR_MESSAGE);
    }
    void showErrordlgInt()
    {
        JOptionPane.showMessageDialog(this, "年龄不正确", "错误消息", JOptionPane.ERROR_MESSAGE);
    }

    public static void main(String args[])
    {
        new Register(r_serverIP, r_PORT);
    }
}

⌨️ 快捷键说明

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