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

📄 mychatroomclient.java

📁 本程序是一个基于RMI的聊天室,使用了多线程,事件模型,支持多用户同时登陆
💻 JAVA
字号:

import javax.swing.JFrame;
import java.awt.FlowLayout;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.rmi.*;
public class MychatroomClient extends JFrame implements ActionListener
{
	FlowLayout flowLayout1 = new FlowLayout();
	JLabel     jLabel1 = new JLabel("UserName:");
	JTextField jTextField1 = new JTextField(8);
	JButton    jButton1 = new JButton("OK");
	JLabel     jLabel2 = new JLabel("PassWord:");
	JPasswordField jPasswordField1 = new JPasswordField(8);
	JButton    jButton2 = new JButton("Exit");
	
	MychatroomClient()
	{
		
		super(" Login:");
		setSize(200,150);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.getContentPane().setLayout(flowLayout1);
		this.getContentPane().add(jLabel1);
		this.getContentPane().add(jTextField1); 
		 
		this.getContentPane().add(jLabel2); 
		this.getContentPane().add(jPasswordField1); 
                this.getContentPane().add(jButton1);
		this.getContentPane().add(jButton2); 
		jButton1.addActionListener(this);
		jButton2.addActionListener(this);
		show();
	}
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource()==jButton1)
		{
			try{
                String mychatroomURL;
	            mychatroomURL="rmi://localhost/Mychatroom";
	            Mychatroom mychatroom;
	            mychatroom=(Mychatroom)Naming.lookup(mychatroomURL);
	            String input="",username="",password="";   
	            int flag,i;
	            for(i=0;i<3;i++)
	            {
	               username=jTextField1.getText();   
	               password=jPasswordField1.getText();
	               flag=mychatroom.personalinfo(username,password);
	               if(flag==0)
	               {
	      	          break;
	               } 
	               else
	               {
	            	   jTextField1.setText("");
	            	   jPasswordField1.setText("");
	            	   jTextField1.requestFocus();
	               }
      }
      if(i>2)
      {
      	   JOptionPane.showMessageDialog(null,"You can't try again","Message",JOptionPane.PLAIN_MESSAGE);
      }
      else
      {
    	
	   input=JOptionPane.showInputDialog("Enter your message");
	   String message=String.valueOf(input);
	  
	   mychatroom.setMessage(message);
                  
	}
}
catch(Exception ex)
{
	ex.printStackTrace();
}
   finally {System.exit(0);}
		}
		else if(e.getSource()==jButton2)
		{
			System.exit(0);
		}	
	}
	public static void main(String[] args)
	{
		MychatroomClient test = new MychatroomClient();
	}
}


    

⌨️ 快捷键说明

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