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

📄 register.java

📁 java编写聊天室
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.Socket;
import java.util.*;
import javax.swing.*;

public class Register extends JFrame implements ActionListener,ItemListener{
	JLabel name,password,affirm,sex; 
	JTextField jtextfield;
	JPasswordField jpasswordfield;
	JPasswordField jaffirmfield;
	JButton submitButton;
	JButton cancelButton;
	Socket soc=null;
    PrintStream ps=null;
    private BufferedReader reader;
    private PrintWriter writer;
	//JRadioButton r1,r2;
    int sPort;   
	JRadioButton girl,boy; 
	ButtonGroup bg;
	String userName;
	String userPassword;
	String userSex;
	Login LG;
	
	//构造函数
	public  Register(Login L){
		super("注册");
		LG=L;
		userSex="女";
		Container container=getContentPane();
		container.setLayout(null);
		name=new JLabel("用户名",JLabel.CENTER);
		sex=new JLabel("性    别",JLabel.CENTER);
		password=new JLabel("密    码",JLabel.CENTER);
		affirm=new JLabel("确认密码",JLabel.CENTER);
	   
	    girl=new JRadioButton("女",false);
		boy=new JRadioButton("男",false);
		girl.setSelected(true);
		bg=new ButtonGroup(); //复选框组
		bg.add(girl);
		bg.add(boy);
		
		
		
		/*引入图片*/
		ImageIcon g=new ImageIcon("images/girl.gif");
		ImageIcon b=new ImageIcon("images/boy.gif");
		
		jtextfield=new JTextField(16);
		jpasswordfield=new JPasswordField(16);
		jaffirmfield=new JPasswordField(16);
		submitButton=new JButton("提交");
		cancelButton=new JButton("取消");

		
		container.add(name);
		container.add(password);
		container.add(sex);
		container.add(affirm);
		container.add(jtextfield);
		container.add(jpasswordfield);
		container.add(jaffirmfield);
		container.add(submitButton);
		submitButton.addActionListener(this);
		container.add(cancelButton);
		cancelButton.addActionListener(this);
		
		container.add(girl);
		container.add(boy);
		girl.addActionListener(this);
		boy.addActionListener(this);
		
		//
		name.setBounds(0, 40, 150, 25);
		jtextfield.setBounds(120, 40, 120, 25);
		
		sex.setBounds(0, 80, 150, 25);
		girl.setBounds(130, 80, 50, 30);
		boy.setBounds(180, 80, 50, 30);
		
		password.setBounds(0, 115, 150, 25);
		jpasswordfield.setBounds(120, 115, 120, 25);
		
		affirm.setBounds(0, 160, 150, 25);
		jaffirmfield.setBounds(120, 160, 120, 25);
		
		submitButton.setBounds(50, 220, 70, 30);
		
		cancelButton.setBounds(200, 220, 70, 30);
		
		setResizable(false);//设置为false用户不能调整大小
		setSize(300,300);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//设置窗口显示的位置
		Dimension dim=Toolkit.getDefaultToolkit().getScreenSize();
		int w=getSize().width;
		int h=getSize().height;
		int x=(dim.width-w)/2;
		int y=(dim.height-h)/2;
		setLocation(x,y);
	}
	//public static void main(String[] args){
		//Register jk1=new Register();
	//}
	public void itemStateChanged(ItemEvent e){
		
		
	}
	
	void getMessage(){
		try{
			
				String message;
				message=reader.readLine();
				if(message.equals("注册失败")){
					//根据情况 弹出错误原因
					
					JOptionPane.showMessageDialog(null, "注册失败!", "提示", JOptionPane.ERROR_MESSAGE);
					jtextfield.setText("");
					jpasswordfield.setText("");
					jaffirmfield.setText("");
					jtextfield.requestFocus();
					girl.setSelected(true);
					boy.setSelected(false);
					userSex="女";
					disconnect();
					
				}
				else if(message.equals("注册成功")){
                     //提示成功
					JOptionPane.showMessageDialog(null, "恭喜你注册成功!", "提示", JOptionPane.OK_OPTION);
					disconnect();
					this.LG.setVisible(true);
					this.setVisible(false);
					
					
					 //	如果登录成功则进入 聊天室
				}
		}catch(Exception e){}
		finally{
			     try{
				       if(soc!=null) 
				       {
				    	   soc.close();
				    	   soc=null;
				        }
			        }catch(IOException ie){}
			  }
	}

	//一些基本控制
	
	public void actionPerformed(ActionEvent e){
		
		//String sex;
		  if(e.getSource()==girl) { userSex="女";}
		  if(e.getSource()==boy)   {userSex="男";}
		//对提交按钮的一些基本控制
		if(e.getActionCommand().equals("提交")){
			//System.out.println("aaa");
			if (jtextfield.getText().equals("")){	
				String inputValuename = JOptionPane.showInputDialog("你还没有输入用户名,请输入!");
				jtextfield.setText(inputValuename);
			}
			else if(jpasswordfield.getText().equals("")){
				//String inputValueps = JOptionPane.showInputDialog("你还没有输入密码,请输入!");
				//jpasswordfield.setText(inputValueps);
				JOptionPane.showMessageDialog(null, "密码不能为空!", "提示", JOptionPane.ERROR_MESSAGE);
				jpasswordfield.requestFocus();
			}
			else if(jaffirmfield.getText().equals("")){
				//String inputValuepsaf = JOptionPane.showInputDialog("你还没有确认密码,请输入!");
				//jaffirmfield.setText(inputValuepsaf);	
				JOptionPane.showMessageDialog(null, "密码不能为空!", "提示", JOptionPane.ERROR_MESSAGE);
				jaffirmfield.requestFocus();
			}
			else if(jpasswordfield.getText().equals(jaffirmfield.getText())==false){
				JOptionPane.showMessageDialog(null, "密码不匹配!", "alert", JOptionPane.ERROR_MESSAGE);
				jpasswordfield.setText("");
				jaffirmfield.setText("");
				jpasswordfield.requestFocus();//获取焦点
			}
			else if(soc==null){
				try{
					try{
						  sPort=6633;
						  System.out.println("尝试与服务器连接");
						  String sName="127.0.0.1";
						  soc=new Socket(sName,sPort);
						  //soc=new Socket("222.18.181.153",sPort);
						  System.out.println("连接完成");
						  writer=new PrintWriter(soc.getOutputStream(),true);
						  reader=new BufferedReader(new InputStreamReader(soc.getInputStream()));					
						 // try{
							  
							  userName=jtextfield.getText();
							  userPassword=jpasswordfield.getText();
							//使用输出流传送用户名和密码
							  writer.println("REG:"+userName+":"+userPassword+":"+userSex+":");
							  getMessage();  //diao yong
						 // }catch(Exception ie){}
					  }catch(Exception ie){
						  System.out.println("连接失败");
			            }
				}catch(Exception ie)
				{}
			}
		}
	
	
		
		
		
		
		//点击取消按钮时 关闭窗口
		if(e.getActionCommand().equals("取消")){
			girl.setSelected(true);
			boy.setSelected(false);
			userSex="女";
			jtextfield.setText("");
			jpasswordfield.setText("");
			jaffirmfield.setText("");
			jtextfield.requestFocus();//获取焦点
			//disconnect();
			//System.exit(0);    //退出整个程序
		}
	}
	
	public void disconnect()
	{
	  if(soc!=null)
		{
	      try
			  {
				soc.close();
		       }
          catch(IOException e)
			  {
		        System.out.println("Error:"+e);
			  }
           finally
			{}
	    }
	}
	
	/*private class JRadio implements ItemListener{
		public void ItemStateChanged(ItemEvent e){
			 if(e.getSource()==girl) { userSex="女";}
			 if(e.getSource()==boy)   {userSex="男";}
		}
	}*/
}




⌨️ 快捷键说明

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