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

📄 chatclient.java

📁 This is the simple Chat application use RMI
💻 JAVA
字号:
package chat.client;

import chat.share.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.rmi.*;
import java.rmi.server.*;

public class ChatClient extends javax.swing.JFrame implements MessageTransferListener {

	JScrollPane chatScrollPane = new JScrollPane();
	JTextArea chatContent = new JTextArea();
	JTextField chatInput    = new JTextField();
	JScrollPane userScrollPane = new JScrollPane();
	JList userList = new JList();
	JMenuBar menuBar = new JMenuBar();
	JMenu mnuOptions = new JMenu();
	JMenu mnuHelp = new JMenu();
	JMenuItem mnuOptionsBeep = new JMenuItem();
	JMenuItem mnuOptionsLogoff = new JMenuItem();
	JMenuItem mnuHelpAbout = new JMenuItem();
	MenuItemAction mnuActionListener = new MenuItemAction();

	public ChatClient() {
		setSize(315, 230);
		setVisible(false);
		setResizable(false);
		getContentPane().setLayout(null);

		setJMenuBar(menuBar);
		menuBar.add(mnuOptions);
		menuBar.add(mnuHelp);

		mnuOptions.setText("Options");
		mnuOptions.setMnemonic((int)'O');
		mnuOptions.add(mnuOptionsBeep);
		mnuOptionsBeep.setText("Beep On");
		mnuOptions.add(mnuOptionsLogoff);
		mnuOptionsLogoff.setText("Log off");

		mnuHelp.setText("Help");
		mnuHelp.setMnemonic((int)'H');
		mnuHelp.add(mnuHelpAbout);
		mnuHelpAbout.setText("About");

		getContentPane().add(chatInput);
		chatInput.setBounds(5, 155, 210, 20);

		getContentPane().add(chatScrollPane);
		chatScrollPane.setBounds(5, 5, 210, 145);
		chatScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

		chatScrollPane.getViewport().add(chatContent);
		chatContent.setBounds(0, 0, 210, 145);
		chatContent.setEditable(false);
		//chatContent.setFont(new Font("Diag", Font.PLAIN, 9));

		getContentPane().add(userScrollPane);
		userScrollPane.setBounds(220, 5, 85, 145);
		userScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

		userScrollPane.getViewport().add(userList);
		userList.setBounds(0, 0, 85, 145);

		chatInput.addKeyListener(new MessageInputAction());
		this.addWindowListener(new LogOffAction());

		mnuOptionsBeep.addActionListener(mnuActionListener);
		mnuOptionsLogoff.addActionListener(mnuActionListener);
		mnuHelpAbout.addActionListener(mnuActionListener);
	}

	public void setVisible(boolean b) {
		if (b) {
			setLocation(0, 500);
			super.setVisible(b);
			chatInput.requestFocus();
		} else {
			super.setVisible(b);
		}
	}

	/**
	 * 
	 *
	 */
	public void init(String hostName, String userName) {
		// T[oOI
		try {
			chatNotifyImpl = new ChatNotifyImpl();
			chatNotifyImpl.addMessageTransferListener(this);

			chatServer = (ChatManager) Naming.lookup("//" + hostName + ":1099/chatServer");
			curUser = userName;
			logon(curUser, chatNotifyImpl);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void messageTransfered(MessageTransferEvent me) {
		chatContent.append(me.getMessage() + "\n");

		int len = chatContent.getText().length();
		chatContent.select(len, len);

		this.toFront();
		this.show();
		this.requestFocus();

		if (beepSwitch) {
			Toolkit.getDefaultToolkit().beep();
		}
	}

	public void UserlistRefreshed(MessageTransferEvent me) {
		Vector vecUsers = me.getUsers();
		userList.setListData(vecUsers);
	}


	ChatNotifyImpl chatNotifyImpl = null;
	ChatManager chatServer = null;
	String curUser = null;
	boolean beepSwitch = false;

	/**
	 * 
	 */
	public static void main(String[] args) {
		if (args.length < 2) {
			System.out.println("use ChatClient hostname yourname");
			System.exit(1);
		}

		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}

		ChatClient client = new ChatClient();
		client.init(args[0], args[1]);
		client.setDefaultCloseOperation(3);
		client.setVisible(true);
	}

	/**
	 * j[Cxg
	 *
	 */
	class MenuItemAction implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			Object obj = e.getSource();
			if (obj == mnuOptionsBeep) {
				setBeepSwitch();
			} else if (obj == mnuOptionsLogoff) {
				logoff();
				System.exit(0);
			} else if (obj == mnuHelpAbout) {

			}
		}
	}

	/**
	 * bZ[W͏L[CxgNX
	 *
	 */
	class MessageInputAction extends KeyAdapter {
		public void keyPressed(KeyEvent ke) {
			int keyCode = ke.getKeyCode();
			if (keyCode == KeyEvent.VK_ENTER) {
				JTextField txtInput = (JTextField) ke.getSource();
				String sText = txtInput.getText().trim();
				if (!sText.equals("")) {
					try {
						chatServer.processChatMessage(curUser, sText);
					} catch (Exception e) {
						e.printStackTrace();
					}
					txtInput.setText("");
				}
			}
		}
	}


	/**
	 * OItCxg
	 *
	 */
	class LogOffAction extends WindowAdapter {
		public void windowClosing(WindowEvent we) {
			logoff();
		}

		public void windowActivated(WindowEvent we) {
			chatInput.requestFocus();
		}
	}

	private void logoff() {
		try {
			chatServer.logoff(curUser);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void logon(String userName, ChatNotifyImpl chatNotifyImpl) {
		try {
			String result = chatServer.logon(userName, chatNotifyImpl);

			if (!result.equals(MessageCode.OK)) {
				System.out.println(result);
				System.exit(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void setBeepSwitch() {
		String sText = mnuOptionsBeep.getText();
		if (sText.equals("Beep On")) {
			beepSwitch = true;
			mnuOptionsBeep.setText("Beep Off");
		} else {
			beepSwitch = false;
			mnuOptionsBeep.setText("Beep On");
		}
	}
}

⌨️ 快捷键说明

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