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

📄 chattingroom.java

📁 企业内部管理系统
💻 JAVA
字号:
package viewtuba1;

import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JCheckBox;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;

import control.ClientReceive;

public class ChattingRoom extends JFrame {
	private JComboBox actionList;

	private JTextArea textArea;

	String ip = "127.0.0.1";

	int port = 8888;

	public static String userName = "无名过客";

	int type = 0;

	Socket socket;

	ObjectOutputStream output;

	ObjectInputStream input;

	ClientReceive recvThread;

	JMenuItem loginItem;

	JMenuItem logoff;

	JMenuItem exitItem;

	JMenuItem userItem;

	JMenuItem connectItem;

	JComboBox comboBox;

	JTextField textField;

	JButton clientMessageButton;

	JLabel showStatus_1;

	JButton loginButton;

	JButton userButton;

	JButton connectButton;

	JButton logoffButton;

	JCheckBox checkBox;

	public static void main(String args[]) {
		try {
			ChattingRoom frame = new ChattingRoom();
			frame.setTitle("聊天室客户端");
			frame.setVisible(true);
			frame.setResizable(false);
			Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
			frame.setLocation((int) (screenSize.width - 400) / 2 + 250,
					(int) (screenSize.height - 600) / 2);
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the frame
	 */
	public ChattingRoom() {
		super();
		getContentPane().setLayout(null);
		setBounds(100, 100, 418, 520);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		final JMenuBar menuBar = new JMenuBar();
		final JMenu operateMenu = new JMenu("操作(O)");
		operateMenu.setMnemonic('O');
		loginItem = new JMenuItem("用户登录(I)");
		loginItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I,
				InputEvent.CTRL_MASK));
		loginItem.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				Connect();
			}

		});
		logoff = new JMenuItem("用户注销(L)");
		logoff.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L,
				InputEvent.CTRL_MASK));
		logoff.setEnabled(false);
		logoff.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				DisConnect();
			}

		});
		exitItem = new JMenuItem("退出(X)");
		exitItem.setMnemonic('X');
		exitItem.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				dispose();
			}

		});
		final JMenu conMenu = new JMenu("设置(C)");
		conMenu.setMnemonic('C');
		userItem = new JMenuItem("用户设置(U)");
		userItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_U,
				InputEvent.CTRL_MASK));
		userItem.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				UserConf userConf = new UserConf();
				userConf.main(null);
				userName = userConf.getName();
			}

		});
		connectItem = new JMenuItem("连接设置(C)");
		connectItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
				InputEvent.CTRL_MASK));
		connectItem.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				ConnectConf con = new ConnectConf();
				con.main(null);
				ip = con.userInputIp;
				port = con.inputPort;
			}

		});
		final JMenu helpMenu = new JMenu("帮助(H)");
		helpMenu.setMnemonic('H');
		JMenuItem helpItem = new JMenuItem("帮助(H)");
		// helpItem.setMnemonic('H');
		helpItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H,
				InputEvent.CTRL_MASK));
		helpItem.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				new HelpChat().main(null);
			}

		});
		setJMenuBar(menuBar);
		operateMenu.add(loginItem);
		operateMenu.add(logoff);
		operateMenu.addSeparator();
		operateMenu.add(exitItem);
		conMenu.add(userItem);
		conMenu.add(connectItem);
		helpMenu.add(helpItem);
		menuBar.add(operateMenu);
		menuBar.add(conMenu);
		menuBar.add(helpMenu);

		final JToolBar toolBar = new JToolBar();
		toolBar.setBounds(0, 0, 410, 29);
		getContentPane().add(toolBar);

		final JPanel panel = new JPanel();
		panel.setLayout(null);
		toolBar.add(panel);

		userButton = new JButton();
		userButton.setText("用户设置");
		userButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				new UserConf().main(null);
			}

		});
		userButton.setBounds(0, 0, 87, 24);
		panel.add(userButton);

		connectButton = new JButton();
		connectButton.setText("连接设置");
		connectButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				ConnectConf con = new ConnectConf();
				con.main(null);
				ip = con.userInputIp;
				port = con.inputPort;
			}

		});
		connectButton.setBounds(89, 0, 87, 24);
		panel.add(connectButton);

		loginButton = new JButton();
		loginButton.setText("登录");
		loginButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				Connect();
			}

		});
		loginButton.setBounds(191, 0, 64, 24);
		panel.add(loginButton);

		logoffButton = new JButton();
		logoffButton.setText("注销");
		logoffButton.setEnabled(false);
		logoffButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				DisConnect();
			}

		});
		logoffButton.setBounds(257, 0, 64, 24);
		panel.add(logoffButton);

		final JButton exitButton = new JButton();
		exitButton.setText("退出");
		exitButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if (type == 1) {
					DisConnect();
				}
				dispose();
			}

		});
		exitButton.setBounds(330, 0, 60, 24);
		panel.add(exitButton);

		final JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(3, 35, 402, 329);
		getContentPane().add(scrollPane);

		textArea = new JTextArea();
		textArea.setEditable(false);
		scrollPane.setViewportView(textArea);
		final JLabel label = new JLabel();
		label.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
		label.setText("发送至:");
		label.setBounds(10, 379, 60, 22);
		getContentPane().add(label);

		comboBox = new JComboBox();
		comboBox.insertItemAt("所有人", 0);
		comboBox.insertItemAt("所有人1", 1);
		comboBox.setSelectedIndex(0);
		comboBox.setBounds(81, 380, 69, 21);
		getContentPane().add(comboBox);

		final JLabel label_1 = new JLabel();
		label_1.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
		label_1.setText("发送消息:");
		label_1.setBounds(10, 416, 69, 15);
		getContentPane().add(label_1);

		textField = new JTextField();
		textField.setBounds(81, 413, 221, 21);
		getContentPane().add(textField);

		clientMessageButton = new JButton();
		clientMessageButton.setText("发送");
		clientMessageButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				SendMessage();
			}

		});
		clientMessageButton.setBounds(316, 412, 69, 23);
		getContentPane().add(clientMessageButton);

		final JLabel showStatus = new JLabel();
		showStatus.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
		showStatus.setText("在线人数:");
		showStatus.setBounds(10, 442, 60, 22);
		getContentPane().add(showStatus);

		showStatus_1 = new JLabel();
		showStatus_1.setBounds(71, 442, 91, 22);
		getContentPane().add(showStatus_1);

		final JLabel label_2 = new JLabel();
		label_2.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
		label_2.setText("表情:");
		label_2.setBounds(167, 379, 46, 22);
		getContentPane().add(label_2);

		actionList = new JComboBox();
		actionList.addItem("微笑地");
		actionList.addItem("高兴地");
		actionList.addItem("轻轻地");
		actionList.addItem("生气地");
		actionList.addItem("小心地");
		actionList.addItem("静静地");
		actionList.addItem("吃惊地");
		actionList.setSelectedIndex(0);
		actionList.setBounds(214, 380, 69, 21);
		getContentPane().add(actionList);

		checkBox = new JCheckBox();
		checkBox.setText("悄悄话");
		checkBox.setBounds(311, 380, 74, 21);
		getContentPane().add(checkBox);
	}

	public void Connect() {
		try {
			// System.out.println(ip);
			// System.out.println(port);
			socket = new Socket(ip, port);
		} catch (Exception e) {
			// TODO: handle exception
			JOptionPane.showMessageDialog(null, "不能连接到指定的服务器。\n 请确认连接设置是否正确!",
					"提示", JOptionPane.CANCEL_OPTION);
			return;
		}
		try {
			output = new ObjectOutputStream(socket.getOutputStream());
			output.flush();
			input = new ObjectInputStream(socket.getInputStream());
			output.writeObject(userName);
			output.flush();
			recvThread = new ClientReceive(socket, output, input, comboBox,
					textArea, showStatus_1);
			recvThread.start();
			loginItem.setEnabled(false);
			loginButton.setEnabled(false);
			userItem.setEnabled(false);
			userButton.setEnabled(false);
			connectItem.setEnabled(false);
			connectButton.setEnabled(false);
			logoff.setEnabled(true);
			logoffButton.setEnabled(true);
			textField.setEnabled(true);
			textArea.append("连接服务器" + ip + ":" + port + "成功...\n");
			type = 1;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return;
		}
	}

	public void DisConnect() {
		loginItem.setEnabled(true);
		loginButton.setEnabled(true);
		userItem.setEnabled(true);
		userButton.setEnabled(true);
		connectItem.setEnabled(true);
		connectButton.setEnabled(true);
		logoff.setEnabled(false);
		logoffButton.setEnabled(false);
		textField.setEnabled(false);
		if (socket.isClosed()) {
			return;
		}
		try {
			output.writeObject("用户下线");
			output.flush();
			input.close();
			output.close();
			socket.close();
			textArea.append("已经与服务器断开连接...\n");
			showStatus_1.setText("");
			type = 0;
		} catch (Exception e) {
			// TODO: handle exception
			// e.printStackTrace();
		}
	}

	public void SendMessage() {
		String toSomebody = comboBox.getSelectedItem().toString();
		String status = "";
		if (checkBox.isSelected()) {
			status = "悄悄话";
		}
		String action = actionList.getSelectedItem().toString();
		String message = textField.getText();
		if (socket.isClosed()) {
			return;
		}
		try {
			output.writeObject("聊天信息");
			output.flush();
			output.writeObject(toSomebody);
			output.flush();
			output.writeObject(status);
			output.flush();
			output.writeObject(action);
			output.flush();
			output.writeObject(message);
			output.flush();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}

⌨️ 快捷键说明

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