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

📄 register.java

📁 与大家分享
💻 JAVA
字号:
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;


public class Register extends JFrame implements ActionListener
{
	private static final long serialVersionUID = 1L;

	JPanel  pnlRegister;
	JLabel  lblUserName,lblGender,lblAge;
	JLabel  lblPassword,lblConfirmPass,lblEmail,logoPosition;
	JTextField  txtUserName,txtAge,txtEmail;
	JPasswordField  pwdUserPassword,pwdConfirmPass;
	JRadioButton  rbtnMale,rbtnFemale;
	ButtonGroup  btngGender;
    JButton  btnOk,btnCancel,btnClear;
	String  strServerIp;
	private JRadioButton  buttonOne;
    private JRadioButton  buttonTwo;
    JLabel  Ilabel;

	final JLabel headLabel = new JLabel();
    //用于将窗口用于定位
	Dimension scrnsize;
    Toolkit toolkit=Toolkit.getDefaultToolkit();
    //构造方法
	public Register(String ip)
	{
		super("聊天室注册窗口");
		strServerIp=ip;
		pnlRegister=new JPanel();
		this.getContentPane().add(pnlRegister);
	
		lblUserName=new JLabel("用 户 名:");
		lblGender=new JLabel("性    别:");
		lblAge=new JLabel("年    龄:");
		lblPassword=new JLabel("密   码:");
		lblConfirmPass=new JLabel("确认密码:");
		lblEmail=new JLabel("电子邮件:");
		txtUserName=new JTextField(30);
		txtEmail=new JTextField(30);
		txtAge=new JTextField(10);
		pwdUserPassword=new JPasswordField(30);
		pwdConfirmPass=new JPasswordField(30);
		rbtnMale=new JRadioButton("男",true);
		rbtnFemale=new JRadioButton("女");
	    btngGender=new ButtonGroup();
	    btnOk=new JButton("确定");
	    btnOk.setToolTipText("保存注册信息");
		btnCancel=new JButton("返回");
		btnCancel.setToolTipText("返回登录窗口");
		btnClear=new JButton("清空");
		btnClear.setToolTipText("清空注册信息");
		
		/*  该布局采用手动布局           *
		 * setBounds设置组件位置        *
		 *  setFont设置字体、字型、字号  *
		 * setForeground设置文字的颜色  *
		 *  setBackground设置背景色      *
		 *  setOpaque将背景设置为透明    */
		pnlRegister.setLayout(null);   
		pnlRegister.setBackground(new Color(125,100,175));

		lblUserName.setBounds(30,80,100,30);
		txtUserName.setBounds(110,85,120,20);
		lblPassword.setBounds(30,141,100,30);
		pwdUserPassword.setBounds(110,146,120,20);
		lblConfirmPass.setBounds(30,166,100,30);
		pwdConfirmPass.setBounds(110,166,120,20);
		lblGender.setBounds(30,191,100,30);
		rbtnMale.setBounds(110,196,60,20);
		rbtnFemale.setBounds(190,196,60,20);
		lblAge.setBounds(30,216,100,30);
		txtAge.setBounds(110,221,120,20);
		lblEmail.setBounds(30,241,80,30);
		txtEmail.setBounds(110,246,120,20);

	    btnOk.setBounds(24,300,80,25);	
	    btnCancel.setBounds(246,300,80,25);
	    btnClear.setBounds(130,300,80,25);
	
		Font fontstr=new Font("宋体",Font.PLAIN,12);	
		lblUserName.setFont(fontstr);
	    lblGender.setFont(fontstr);
		lblPassword.setFont(fontstr);
		lblConfirmPass.setFont(fontstr);
		lblAge.setFont(fontstr);
		lblEmail.setFont(fontstr);
        rbtnMale.setFont(fontstr);
		rbtnFemale.setFont(fontstr);
		txtUserName.setFont(fontstr);
		txtEmail.setFont(fontstr);	
		btnOk.setFont(fontstr);
		btnCancel.setFont(fontstr);
		btnClear.setFont(fontstr);
						
		lblUserName.setForeground(Color.BLACK);
		lblGender.setForeground(Color.BLACK);
		lblPassword.setForeground(Color.BLACK);
		lblAge.setForeground(Color.BLACK);
		lblConfirmPass .setForeground(Color.BLACK);
		lblEmail.setForeground(Color.BLACK);
		rbtnMale.setForeground(Color.BLACK);
		rbtnFemale.setForeground(Color.BLACK);
		rbtnMale.setBackground(Color.white);
		rbtnFemale.setBackground(Color.white);
		btnOk.setBackground(Color.white);	
	    btnCancel.setBackground(Color.white);
	    btnClear.setBackground(Color.white);
		rbtnMale.setOpaque(false);   
		rbtnFemale.setOpaque(false);
		
		pnlRegister.add(lblUserName);
		pnlRegister.add(lblGender);
		pnlRegister.add(lblPassword);
		pnlRegister.add(lblConfirmPass);
		pnlRegister.add(lblEmail);
		pnlRegister.add(lblAge);
		pnlRegister.add(txtAge);
		pnlRegister.add(txtUserName);
		pnlRegister.add(txtEmail);
		pnlRegister.add(pwdUserPassword);
		pnlRegister.add(pwdConfirmPass);
		pnlRegister.add(btnOk);
		pnlRegister.add(btnCancel);
		pnlRegister.add(btnClear);
		pnlRegister.add(rbtnMale);
		pnlRegister.add(rbtnFemale);
		btngGender.add(rbtnMale);
	    btngGender.add(rbtnFemale);
	    
	    //设置背景图片
	    Icon logo = new ImageIcon("image\\i.jpg");
	 	logoPosition = new JLabel(logo);
		logoPosition.setBounds(0, 0, 360,78);
		pnlRegister.add(logoPosition);
	    
	    this.setSize(370,380);
		this.setVisible(true);
		this.setResizable(false);
		//将窗口定位在屏幕中央
    	scrnsize=toolkit.getScreenSize();
    	this.setLocation(scrnsize.width/2-this.getWidth()/2,
    	                 scrnsize.height/2-this.getHeight()/2);
		Image img=toolkit.getImage("images\\10.jpg");
        this.setIconImage(img);
		//三个按钮注册监听
		btnOk    .addActionListener(this);
		btnCancel.addActionListener(this);
		btnClear   .addActionListener(this);
		
		buttonOne = new JRadioButton("One", true);
        buttonTwo = new JRadioButton("Two");
        Ilabel = new JLabel(buttonOne.getText());

	}  //构造方法结束


	//按钮监听响应
	public void actionPerformed(ActionEvent ae)
	{
		Object source=new Object();
	    source=ae.getSource();
	    if (source.equals(btnOk))      //"确定"按钮
	    {
	        Register();
	    }
	    if (source.equals(btnCancel))  //"返回"按钮
	    {
	    	new Login();
	    	this.dispose();
	    }
	    if (source.equals(btnClear))     //"清空"按钮
	    {
	        txtUserName.setText("");
	        pwdUserPassword.setText("");
	        pwdConfirmPass.setText("");
	        txtAge.setText("");
	        txtEmail.setText("");	
	    }
	}  //actionPerformed()结束
	
	//////////"确定"按钮事件响应//////////
	public void Register()
	{

			//接受客户的资料
	        Register_User data=new Register_User();
		    data.custName     = txtUserName.getText();
			data.custPassword = pwdUserPassword.getText();
			data.age          = txtAge.getText();
			data.sex          = rbtnMale.isSelected()?"男":"女";
			data.email        = txtEmail.getText();
			if(data.sex == "男"){
				data.sex = "male";
			}
			if(data.sex == "女")
				data.sex="female";
			//验证用户名是否为空
			if(data.custName.length()==0)
			{
			    JOptionPane.showMessageDialog(null,"用户名不能为空!");	
	            return;	
			}
			//验证密码是否为空
			if(data.custPassword.length()==0)
			{
			    JOptionPane.showMessageDialog(null,"密码不能为空!");	
	            return;	
			}
			
			//验证密码是否一致
			if(!data.custPassword.equals(pwdConfirmPass.getText()))
			{
			    JOptionPane.showMessageDialog(null,"密码两次输入不一致,请重新输入!");	
	            return;
			}
			
			//验证年龄是否为空
			if(data.age.length()==0)
			{
			    JOptionPane.showMessageDialog(null,"年龄不能为空!");
	            return;	
			}
			
			String s = data.age;
			char[] a = s.toCharArray();//存放字符数组
			for(int i = 0;i < a.length;i++)
			{
				if(!Character.isDigit(a[i]))			
			    {
				   JOptionPane.showMessageDialog(null,"年龄不能为字符数据!");
				   return;
			    }
			}
			
			//验证年龄的合法性
			int age=Integer.parseInt(txtAge.getText());
			
			if (age<=0||age>150){
				JOptionPane.showMessageDialog(null,"年龄输入不合法!");
				return;
			}


			//验证Email是否合法,即有没有'@'
			int Found_flag=0;    //定义判断标志
			for (int i=0;i<data.email.length();i++)
			{
			    if(data.email.charAt(i)=='@')
			    {
			        Found_flag++;	
			    }	
			}
			if(Found_flag!=1)  //如果电子邮件中没有'@'标志
			{
			    JOptionPane.showMessageDialog(null,"电子邮箱格式不正确,请重新输入!");	
	            return;	
			}
			
			try
			{
			    //连接到服务器
			    Socket toServer = new Socket(strServerIp,30002); //生成Socket函数对象 
			    ObjectOutputStream streamToServer=new ObjectOutputStream (toServer.getOutputStream());  
			    //获得输出流					
			    //把客户详细资料写到服务器socket
			    streamToServer.writeObject((Register_User)data);
	            //读来自服务器socket的登陆状态
	            BufferedReader fromServer=new BufferedReader(new InputStreamReader(toServer.getInputStream()));
	            String status=fromServer.readLine();
	            //告知用户成功消息
	            JOptionPane.showMessageDialog(null,status);
	     
	            if(status.equals("注册成功")) 
	            {
	                txtUserName.setText("");
	                pwdUserPassword.setText("");
	                pwdConfirmPass.setText("");
	                txtAge.setText("");
	                txtEmail.setText("");
	                new Login();
	                this.dispose();
	            }
	        
	            //关闭流对象
			    streamToServer.close();
	            fromServer.close();
	         }
			//异常处理
			 catch(InvalidClassException e1)
			 {
			    JOptionPane.showMessageDialog(null,"类错误!");
			 }
			 catch(NotSerializableException e2)
			 {
				JOptionPane.showMessageDialog(null,"对象未序列化!");
			 }
			 catch(IOException e3)
			 {
			 	JOptionPane.showMessageDialog(null,"不能写入到指定服务器!");
			 }
			
		}

		//main函数
		public static void main(String args[])
		{
			new Register("localhost");
		}
	} 

⌨️ 快捷键说明

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