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

📄 registerframe.java

📁 文件共享虚拟社区,可以实现上传下载聊天等多种信息交互.
💻 JAVA
字号:
/*
 * Created on 2005-8-30
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

package fsc.client;

/**
 * @author zhujing
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
import javax.swing.*;

import java.awt.Container;
import java.awt.BorderLayout;
import java.awt.*;
import java.awt.event.*;
import java.awt.Container;
import java.awt.event.ActionListener;

import fsc.models.*;
import fsc.models.exceptions.*;
import fsc.util.StringProcessor;

public class RegisterFrame extends JFrame implements ActionListener 
{
	ClientApp app;

    /*
     *对一些基本的后面所要用到的元件进行申明和定义
     *从而避免后面的函数在用到这些元件是无法识别
     */
	RegisterModel rm;
	JRadioButton male =new JRadioButton("男");
	JRadioButton female =new JRadioButton("女");
//	JLabel l15=new JLabel("");
//	JLabel l1;
	
	//对文本框的申明和定义并设置大小
	JTextField userName=new JTextField(10);
	JPasswordField passWord=new JPasswordField(10);
	JPasswordField passWordAgain=new JPasswordField(10);
	JTextField question=new JTextField(10);
	JTextField answer=new JTextField(10);
	JTextField email=new JTextField(14);
	
	//对按钮的定义
	JButton reg=new JButton("注册");
	JButton cancel=new JButton("取消");
	
	public RegisterFrame(String name, ClientApp a)
	{
		super(name);//继承JFrame中的文件名命名的函数
		app=a;
		rm=app.registerModel;
		
        Container container=getContentPane();//		设置总体局面的布局
        container.setLayout(new BorderLayout(5,20));
		
		//对按钮进行事件监听
		reg.addActionListener(this);
		cancel.addActionListener(this);
		//对基本标签的定义和内容设定
		JPanel p1=new JPanel();
		JPanel p2=new JPanel();
		JPanel p3=new JPanel();
		JLabel l1=new JLabel("         请输入您的帐号  (必填)");
		JLabel l2=new JLabel("         请输入您的密码  (必填)");
		JLabel l3=new JLabel("         请再次输入您的密码(必填)");
		JLabel l4=new JLabel("         请输入密码保护问题(必填)");
		JLabel l5=new JLabel("         请输入密码保护答案(必填)");
		JLabel l6=new JLabel("         请输入您的的真实姓名   ");
		JLabel l7=new JLabel("         请选择您的性别(必填)  ");
		JLabel l9=new JLabel("         请输入您的邮箱(必填)");
		
		//设置构成单选按钮组
		ButtonGroup sex=new ButtonGroup();
		sex.add(male);
		sex.add(female);
		male.setSelected(true);
       //将各个元件添加到面板中 同时再添加到容器中
		p2.add(male);
		p2.add(female);
		p1.setLayout(new FlowLayout(FlowLayout.LEFT,20,20));
		p1.add(l1);
		p1.add(userName);
     	p1.add(l2);
		p1.add(passWord);
		p1.add(l3);
		p1.add(passWordAgain);
		p1.add(l4);
		p1.add(question);
		p1.add(l5);
		p1.add(answer);
		p1.add(l7);
		p1.add(p2);
		p1.add(l9);
		p1.add(email);
		reg.setBounds(100,380,80,30);
		cancel.setBounds(210,380,80,30);
		container.add(reg);
		container.add(cancel);
		container.add(p1);
		container.add(p3,BorderLayout.NORTH);
		setLocation(300,100);
		setSize(400,500);
		setVisible(true);
		setResizable(false);
	}
	


	public static void main(String[] args)
	{
		RegisterFrame frame=new RegisterFrame("注册界面",null);
		
	}
    //此处对监听事件的具体处理
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource()==reg)
		{	
	    	if(!passWord.getText().equals(passWordAgain.getText()))
	    	{
	    		JOptionPane.showMessageDialog(this,"两次输入的密码不相同");
	    		return;
	    	}
	    	
	    	
			try
			{
				rm.setUserName(userName.getText());
				rm.setPassword(passWord.getText());
				rm.setQuestion(question.getText());
				rm.setAnswer(answer.getText());
				if(male.isSelected());
				{
					rm.setSex('M');
				}				
				 if(female.isSelected())
				{
					rm.setSex('F');
				}
				rm.setEmail(email.getText());
			}
			catch(InvalidUserNameException ex)
			{
				JOptionPane.showMessageDialog(this,ex.getMessage());
				return;
			}
			catch(InvalidPassswordException ex)
			{
				JOptionPane.showMessageDialog(this,ex.getMessage());
				return;
			}
			catch(InvalidQuestionException ex)
			{
				JOptionPane.showMessageDialog(this,ex.getMessage());
				return;
			}
			catch(InvalidAnswerException ex)
			{
				JOptionPane.showMessageDialog(this,ex.getMessage());
				return;
			}
			catch(InvalidEmailException ex)
			{
				JOptionPane.showMessageDialog(this,ex.getMessage());
				return;
			}
			catch (InvalidSexException ex) {
				JOptionPane.showMessageDialog(this,ex.getMessage());
				return;
			}
			catch(Exception ex)
			{
				JOptionPane.showMessageDialog(this,ex.getMessage());
				return;
			}
			
			//连接服务器注册
			try
			{
				app.getDataOutputStream().writeUTF(rm.toString());
				
				String result= app.getDataInputStream().readUTF();
				System.out.println(result);
				StringProcessor sp = new StringProcessor(result);
				String tag=sp.getLine();
				RegisterResultModel rrm= new RegisterResultModel(sp.getContent());
				if(tag.equals(RegisterResultModel.TAG))
				{
					if(rrm.getRegisterResult().equals("NO") )
					{
						JOptionPane.showMessageDialog(this,"注册失败!\n请稍后再试。","提示",JOptionPane.ERROR_MESSAGE);
					}
					else if(rrm.getRegisterResult().equals("YES"))
					{
						JOptionPane.showMessageDialog(this,"恭喜您!\n您注册的用户名:\t"+rrm.getUserName()+"\n您注册的邮箱:\t"+rrm.getEmail(),"注册成功!",JOptionPane.INFORMATION_MESSAGE);
					}
					else
					{
						JOptionPane.showMessageDialog(this,"注册失败!\n请稍后再试。","提示",JOptionPane.ERROR_MESSAGE);
					}
					app.closeIOStreamsAndDisconnect();
				}
				
			}
			catch(Exception ex)
			{
				ex.printStackTrace();
			}


			System.out.println(rm.toString());
		
	}
	else if (e.getSource()==cancel)		//隐藏对话框
	{
		this.setVisible(false);
	}
}
}

⌨️ 快捷键说明

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