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

📄 logindiag.java

📁 实现多人即时聊天
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
  
class LoginDiag extends JDialog{

	private Client client;
	private File ServerInfo;
	private DataInputStream fromFlie;
	private DataOutputStream ToFile;


	private boolean  Advance=false;		//"高级"(true)或者普通(false)
	private boolean  Register=false;	//登录(fasle)或者注册(true)
						//开始用按钮上显示的文字"Advanced","Normal","Reg","Log in"来判断当前窗口状态
						//这个就局限了按钮上的文字只能是以上几个,若改动,则代码的其他部分也要修改
						//容易出错,而且降低了通用性

	private UserInfo myself;
	private String   IPofServer;
	private int      port;
	private boolean  connected=false;	//为了减少开销,在正确已经连接到服务器而仅因为用户基本信息填写错误
						//造成的登录错误不必重新连接,重新输入再登录。暂时没用到。

	private String   RegName;		//注册名--显示的是用户名
	private String   UserName;		//真实姓名
	private String   Password;		//密码
	private boolean  Sex;			//性别
	private String   Job;			//职位


/****************************************************以下组件****************************************************/
	private Container contentPane;		//顶层容器	
	
	private JLabel LabRegName;		//注册名--显示的是用户名
	private JLabel LabUserName;		//真实姓名
	private JLabel LabPassword;		//密码
	private JLabel LabPasswordAgain;	//密码确认,注册时用
	private JLabel LabIPAddr;		//加边框
	private JLabel LabPort;			//加边框
	private JLabel LabSex;			//性别
	private JLabel LabJob;			//职位
	
	private JTextField RegNameText;
	private JTextField UserNameText;
	private JPasswordField PasswordText;
	private JPasswordField PasswordAgainText;
	private JTextField IPAddrText;
	private JTextField PortText;
	private JTextField JobText;
	
	private JCheckBox register;		//标识"注册"和"登录"的复选按钮

	private JRadioButton male;
	private JRadioButton female;
	private ButtonGroup GroupSex;

	private JButton OKBtn;			//"登陆"或"注册"按钮
	private JButton CancelBtn;		//"取消"按钮
	private JButton AdvBtn;		//"高级"/"普通"交替按钮
/****************************************************以上组件****************************************************/
public LoginDiag(Client client){
	this();
	this.client=client;
	ServerInfo=new File("."+File.separator+"SerIPort.dat");
	try{
		ServerInfo.createNewFile();
System.out.println("空文件创建成功");
		fromFlie=new DataInputStream(new FileInputStream(ServerInfo));
System.out.println("文件流创建成功");
		String IP_Port=fromFlie.readLine();//readUTF();//
		fromFlie.close();

		if(IP_Port==null){
//防止SerIPort.dat文件中无内容时(第一次使用时程序自动新建)
//IP_Port==null,此时文本框中内容为空造成文本框不可见		
			IPAddrText.setText("               ");
			PortText.setText("               ");
		}else{
			IP_Port=IP_Port.trim();
			String IP=IP_Port.substring(0,IP_Port.indexOf(":"));	//该substring()返回[0,indexOf(":")-1]内的字符串
			String port=IP_Port.substring(IP_Port.indexOf(":")+1);	//该substring()返回[indexOf(":")+1,串尾]内的字符串
	System.out.println("IP="+IP);
	System.out.println("port="+port);
			IPAddrText.setText(IP.trim());
			PortText.setText(port);

		}//if(IP_Port!=null)
			

	}catch(IOException e){
		JOptionPane.showMessageDialog(this,
						"文件创建或者读文件失败!",
						"Error",
						JOptionPane.ERROR_MESSAGE);
	}//catch(IOException e)

}//public LoginDiag(Client client)

public LoginDiag(){
	 
 	//创建容器
	contentPane=this.getContentPane();
	contentPane.setLayout(new GridBagLayout());		

	setTitle("Log in");
	setModal(false);				//不挂起其他窗口
	
	    
	LabRegName=new JLabel("RegName");		//注册名
	LabUserName=new JLabel("True Name");		//真实姓名
	LabPassword=new JLabel("Password");		//密码
	LabPasswordAgain=new JLabel("PasswordAgain");	//密码确认,注册时用
	LabIPAddr=new JLabel("ServerIP");		//加边框
	LabPort=new JLabel("ServerPort:");		//加边框
	LabSex=new JLabel("Sex");			//性别
	LabJob=new JLabel("Job");			//职位
	
	RegNameText=new JTextField();
	//RegNameText.setColumns(15);
	//RegNameText=new JTextPane();


	UserNameText=new JTextField();
	//UserNameText.setColumns(15);

	PasswordText=new JPasswordField();
	//PasswordText.setColumns(15);

	PasswordAgainText=new JPasswordField();
	//PasswordAgainText.setColumns(15);

	IPAddrText=new JTextField();
	//IPAddrText.setColumns(15);

	PortText=new JTextField();
	//PortText.setColumns(15);

	JobText=new JTextField("职员            ");			
	//JobText.setColumns(15);

	male=new JRadioButton("male",true);
	female=new JRadioButton("female",false);
	
	GroupSex=new ButtonGroup();
	GroupSex.add(male);
	GroupSex.add(female);

	register=new JCheckBox("Reg",false);	//"登陆"与"注册",复选按钮

	OKBtn=new JButton("Log in");		//"登陆"与"注册"按钮
	CancelBtn=new JButton("Cancel");
	AdvBtn=new JButton("Advanced"/*,new ImageIcon(".\\pics\\down.gif")*/);//在"高级"与"普通"互换的按钮
	//加上图片后文本框可能不能正常显示,所以没加

	DefaultLayout();
		
	setSize(400,400);
	setResizable(false);

	addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				System.exit(0);				
			}//public void windowClosing(WindowEvent e)
		});//addWindowListener(new WindowAdapter(){
	

	OKBtn.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e){
		
		try{
			RegName=RegNameText.getText().trim();		
			UserName=UserNameText.getText().trim();
			Password=PasswordText.getText().trim();		
			Sex=male.isSelected();
			Job=JobText.getText().trim();
			IPofServer=IPAddrText.getText().trim();			//服务器的IP,不是本地的
			port=Integer.parseInt(PortText.getText().trim());	//服务器的port,不是本地的

		//文本框中提供的IP和Port信息能正确连接服务器则写入文件保存,下次就不用再输入了
		//只要用在登录界面输入的IP和Port信息能正确登录,第一次使用时只要知道正确的IP和Port信息即可
		//不用手工写文件(只要服务器IP和Port信息没有改变以后也不用手工创建SerIPort.dat文件)
		
		ToFile=new DataOutputStream(new FileOutputStream(ServerInfo));//会自动删除原来的文件新建一个同名文件	
		ToFile.write((IPofServer+":"+PortText.getText().trim()).getBytes());
		ToFile.close();
		System.out.println("文件写入成功");
			if(RegName.equals("")||Password.equals("")){
				JOptionPane.showMessageDialog(LoginDiag.this,
						"Please enter RegName && password!",
						"Error",
						JOptionPane.ERROR_MESSAGE);
					return;
			}



		//Register	//当前状态:登录(fasle)或者注册(true)

		        if(Register){
				if(!PasswordAgainText.getText().trim().equals(Password)){
					JOptionPane.showMessageDialog(LoginDiag.this,
						"Liang mi ma bu yi zhi!",
						"Error",
						JOptionPane.ERROR_MESSAGE);
					return;
				}//检测两次密码是否一致

				if(UserName.equals("")){
					JOptionPane.showMessageDialog(LoginDiag.this,
						"Qing shu ru zheng shi xing ming!",
						"Error",
						JOptionPane.ERROR_MESSAGE);
					return;
				}
			}//if(Register)


		LoginDiag.this.client.connect(IPofServer,port);
		//UserName=CharConvert.GB2312toUnicode(UserName);//转换成正确的中文字符
		myself=new UserInfo(RegName,UserName,Password,System.currentTimeMillis());
		myself.setSex(Sex);
		//Job=CharConvert.GB2312toUnicode(Job);//转换成正确的中文字符
		myself.setJob(Job);
		myself.setIpAddr(client.getIPAddr());
		myself.setPort(client.getClientPort());

⌨️ 快捷键说明

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