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

📄 chatclient.java

📁 it is a java chat program Written in a RMI code. its simple to use and can be used as a project.
💻 JAVA
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.rmi.*;
import java.rmi.server.*;
public class ChatClient extends JFrame implements ActionListener,
ClientRemote
{
		JTextField str;
		JButton send;
		JButton File Sharing;
		JTextArea area;
		JScrollPane scroll;
		JLabel label1, label2, label3;
		GridBagLayout gb1;
		GridBagConstraints gbc;
		ServerRemote ser;
		private static String name="";
		int i=0;
		
		
		public ChatClient()
		{
			setSize(500,400);
			gb1 = new GridBagLayout();
			gbc = new GridBagConstraints();
			getContentPane().setLayout(gb1);
			
			gbc.gridx = 1;
			gbc.gridy = 1;
			label1 = new JLabel("CLASS ROOM");
			label1.setFont(new Font("Arial", Font.BOLD,20));
			getContentPane().add(label1,gbc)     ;
			
			gbc.gridx = 1;
			gbc.gridy = 2;
			label2 = new JLabel("Message Display Area");
			getContentPane().add(label2, gbc);
			
			gbc.gridx = 1;
			gbc.gridy = 3;
			area = new JTextArea(25,40);
			scroll = new JScrollPane(area);
			getContentPane().add(scroll, gbc);
			
			gbc.gridx = 1;
			gbc.gridy = 4;
			label3 = new JLabel("Enter Message");
			getContentPane().add(label3, gbc);
			
			str = new JTextField(40);
			gbc.gridx = 1;
			gbc.gridy = 5;
			getContentPane().add(str, gbc);
			
			File Sharing = new JButton("File Sharing");
			gbc.gridx = 1;
			gbc.gridy = 7;
			getContentPane().add(File Sharing, gbc);
			
			send = new JButton("send");
			gbc.gridx = 1;
			gbc.gridy = 6;
			getContentPane().add(send, gbc);
			
			send.addActionListener(this);
			
			addWindowListener(new WindowAdapter() 
			{
				public void windowClosing(WindowEvent we)
				{
					try
					{
						ser.removeClient(i);
					}
					catch (Exception e)
					{
					System.out.println("Error: " + e);
					}
					System.exit(0);
				}
			});
			try
			{
				System.setSecurityManager(new RMISecurityManager() );
				UnicastRemoteObject.exportObject(this);
				ser = (ServerRemote)Naming.lookup("rmi://172.16.20.131/server");
			}
			catch(Exception e)
			{
				System.out.println("Error:" +e);
			}
			try
			{
				i = ser.registerClient (this);
			}
			catch (Exception e)
			{
				System.out.println("Error:" + e);
			}
		}
		public void actionPerformed(ActionEvent ae)
		{
		try
		{
			ser.showText(name+": "+str.getText() );
		}
		catch (Exception e)
		{
			System.out.println("Error:" +e);
		}
		}
		public void getString(String str) throws RemoteException
		{
			area.append(str +"\n");
		}
		public static void main (String arg[])
		{
			name=arg[0];
			ChatClient cc = new ChatClient();
			cc.show();
			cc.setTitle(name);
		}
}

⌨️ 快捷键说明

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