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

📄 chatclient.java

📁 JAVA做的QQ软件,感兴趣的可以看看,收藏依旧
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Toolkit;
import javax.swing.JTextArea;
import javax.swing.JTabbedPane;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JList;
import javax.swing.JToolBar;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JCheckBox;

import javax.swing.JScrollPane;
import java.util.Vector;
import java.awt.event.*;
import javax.swing.JTable;
import javax.swing.DefaultListModel;
import java.net.*;
import java.io.*;
import javax.swing.JOptionPane;
import javax.swing.JFileChooser;
import java.beans.*;
import javax.swing.*;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JTextPane;
import javax.swing.text.*;
import javax.swing.Icon;
import javax.swing.UIManager;


public class ChatClient extends JFrame implements Runnable
{
	private JPanel jContentPane = null;
	private JTabbedPane chatTabbedPane = null;
	private JTabbedPane myTabbedPane = null;
	private JTextArea myMsgTextArea = null;
	private JLabel welcomeLabel = null;
	private JTextField noticTitleTextField = null;
	public static JTextArea noticeTextArea = null;
	private JTextField userTitleTextField = null;
	private JLabel pictureLabel = null;
	private JPanel toolPanel = null;
	private JButton exitButton = null;
	private JLabel toLabel = null;
	public static JComboBox toComboBox = null;
	private JButton transFileButton = null;
	private JButton sendMsgButton = null;
	private JCheckBox onlyCheckBox = null;	
	private JScrollPane chatScrollPane = null;
	private JScrollPane usersListScrollPane = null;
	public static JList login_usersList = null;
	private JButton showUserInfoButton = null;
	
	public static DefaultListModel listModel = new DefaultListModel();			
	private String userID;
	private JTextPane chatTextPane = null;	
	private File file;
	
	private JTabbedPane getChatTabbedPane() {
		if (chatTabbedPane == null) {
			chatTabbedPane = new JTabbedPane();
			chatTabbedPane.setBackground(new java.awt.Color(153,246,232));
			chatTabbedPane.setForeground(java.awt.Color.blue);
			chatTabbedPane.setFont(new java.awt.Font("黑体", java.awt.Font.PLAIN, 14));
			chatTabbedPane.setEnabled(true);
			chatTabbedPane.setSize(new java.awt.Dimension(530,302));
			chatTabbedPane.setLocation(new java.awt.Point(1,47));
			chatTabbedPane.addTab("主聊天频道", new ImageIcon(getClass().getResource("/pictures/pub.JPG")), getChatScrollPane(), null);
		}
		return chatTabbedPane;
	}

	private JTabbedPane getMyTabbedPane() {
		if (myTabbedPane == null) {
			myTabbedPane = new JTabbedPane();
			myTabbedPane.setForeground(java.awt.Color.blue);
			myTabbedPane.setFont(new java.awt.Font("黑体", java.awt.Font.PLAIN, 14));
			myTabbedPane.setLocation(new java.awt.Point(1,349));
			myTabbedPane.setSize(new java.awt.Dimension(530,111));
			myTabbedPane.addTab(userID + "的频道", new ImageIcon(getClass().getResource("/pictures/myself.JPG")), getMyMsgTextArea(), null);
		}
		return myTabbedPane;
	}

	private JTextArea getMyMsgTextArea() {
		if (myMsgTextArea == null) {
			myMsgTextArea = new JTextArea();
			myMsgTextArea.setLineWrap(true);
		}
		return myMsgTextArea;
	}


	private JTextField getNoticTitleTextField() {
		if (noticTitleTextField == null) {
			noticTitleTextField = new JTextField();
			noticTitleTextField.setEditable(false);
			noticTitleTextField.setEnabled(true);
			noticTitleTextField.setFont(new java.awt.Font("Dialog", java.awt.Font.PLAIN, 12));
			noticTitleTextField.setForeground(java.awt.Color.black);
			noticTitleTextField.setBackground(new java.awt.Color(251,227,251));
			noticTitleTextField.setLocation(new java.awt.Point(535,2));
			noticTitleTextField.setSize(new java.awt.Dimension(164,21));
			noticTitleTextField.setText("           -------公 告 栏-------");
		}
		return noticTitleTextField;
	}


	private JTextArea getNoticeTextArea() {
		if (noticeTextArea == null) {
			noticeTextArea = new JTextArea();
			noticeTextArea.setBackground(new java.awt.Color(255,255,226));
			noticeTextArea.setLocation(new java.awt.Point(536,23));
			noticeTextArea.setEditable(false);
			noticeTextArea.setSize(new java.awt.Dimension(162,75));
			noticeTextArea.setText("\n       欢迎" + userID + "进入聊天室!");
			noticeTextArea.setLineWrap(true);
		}
		return noticeTextArea;
	}

	private JTextField getUserTitleTextField() {
		if (userTitleTextField == null) {
			userTitleTextField = new JTextField();
			userTitleTextField.setLocation(new java.awt.Point(535,103));
			userTitleTextField.setBackground(new java.awt.Color(251,225,251));
			userTitleTextField.setText("       ----Fetion在线用户----");
			userTitleTextField.setSize(new java.awt.Dimension(163,21));
			userTitleTextField.setEditable(false);
		}
		return userTitleTextField;
	}



	private JPanel getToolPanel() {
		if (toolPanel == null) {
			toLabel = new JLabel();
			toLabel.setText("对:");
			toLabel.setFont(new java.awt.Font("黑体", java.awt.Font.BOLD, 14));
			toLabel.setForeground(java.awt.Color.blue);
			toLabel.setBounds(new java.awt.Rectangle(206,16,38,18));
			toolPanel = new JPanel();
			toolPanel.setLayout(null);
			toolPanel.setBackground(new java.awt.Color(250,230,251));
			toolPanel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
			toolPanel.setSize(new java.awt.Dimension(528,47));
			toolPanel.setLocation(new java.awt.Point(3,463));
			toolPanel.add(toLabel, null);
			toolPanel.add(getToComboBox(), null);
			toolPanel.add(getTransFileButton(), null);
			toolPanel.add(getSendMsgButton(), null);
			toolPanel.add(getOnlyCheckBox(), null);
		}
		return toolPanel;
	}


	private JButton getExitButton() {
		if (exitButton == null) {
			exitButton = new JButton();
			exitButton.setFont(new java.awt.Font("黑体", java.awt.Font.PLAIN, 14));
			exitButton.setBackground(new java.awt.Color(203,247,232));
			exitButton.setLocation(new java.awt.Point(536,466));
			exitButton.setSize(new java.awt.Dimension(160,46));
			exitButton.setText("退出聊天室");
			exitButton.setIcon(new ImageIcon(getClass().getResource("/pictures/leave.gif")));
			exitButton.setToolTipText("离开聊天室 (Esc)");
			exitButton.addActionListener(new ExitListener());
			exitButton.registerKeyboardAction(new ExitListener(),
			            KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE,0),
			            JComponent.WHEN_IN_FOCUSED_WINDOW);
		}
		return exitButton;
	}

	private JComboBox getToComboBox() {
		if (toComboBox == null) {
			toComboBox = new JComboBox();
			toComboBox.setBounds(new java.awt.Rectangle(236,13,100,24));
			toComboBox.setForeground(new java.awt.Color(108,205,108));
			toComboBox.setBackground(java.awt.Color.white);
			toComboBox.addItem("所有人");
			toComboBox.setSelectedIndex(0);
			toComboBox.addItemListener(new CBoxListener());
		}
		return toComboBox;
	}

	private JButton getTransFileButton() {
		if (transFileButton == null) {
			transFileButton = new JButton();
			transFileButton.setBounds(new java.awt.Rectangle(29,13,129,25));
			transFileButton.setFont(new java.awt.Font("黑体", java.awt.Font.PLAIN, 14));
			transFileButton.setForeground(java.awt.Color.blue);
			transFileButton.setBackground(new java.awt.Color(204,204,255));
			transFileButton.setIcon(new ImageIcon(getClass().getResource("/pictures/transFile.JPG")));
			transFileButton.setText("传送文件");
			transFileButton.addActionListener(new TransFileListener());
		}
		return transFileButton;
	}

	private JButton getSendMsgButton() {
		if (sendMsgButton == null) {
			sendMsgButton = new JButton();
			sendMsgButton.setFont(new java.awt.Font("黑体", java.awt.Font.PLAIN, 14));
			sendMsgButton.setForeground(java.awt.Color.blue);
			sendMsgButton.setBackground(new java.awt.Color(204,204,255));
			sendMsgButton.setLocation(new java.awt.Point(479,3));
			sendMsgButton.setSize(new java.awt.Dimension(42,42));
			sendMsgButton.setToolTipText("发送消息");
			sendMsgButton.setIcon(new ImageIcon(getClass().getResource("/pictures/Send.JPG")));
			sendMsgButton.setText("");
			sendMsgButton.addActionListener(new SendMsgListener());
			sendMsgButton.setMnemonic(KeyEvent.VK_S);
		}
		return sendMsgButton;
	}

	private JCheckBox getOnlyCheckBox() {
		if (onlyCheckBox == null) {
			onlyCheckBox = new JCheckBox();			
			onlyCheckBox.setBounds(new java.awt.Rectangle(347,15,59,20));
			onlyCheckBox.setFont(new java.awt.Font("黑体", java.awt.Font.PLAIN, 14));
			onlyCheckBox.setForeground(java.awt.Color.blue);
			onlyCheckBox.setBackground(new java.awt.Color(204,255,204));
			onlyCheckBox.setText("私聊");
			onlyCheckBox.setEnabled(false);
					
		}
		return onlyCheckBox;
	}
	

	private JScrollPane getChatScrollPane() {
		if (chatScrollPane == null) {
			chatScrollPane = new JScrollPane();
			chatScrollPane.setViewportBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
			chatScrollPane.setViewportView(getChatTextPane());
		}
		return chatScrollPane;
	}



	private JScrollPane getUsersListScrollPane() {
		if (usersListScrollPane == null) {
			usersListScrollPane = new JScrollPane();
			usersListScrollPane.setLocation(new java.awt.Point(535,125));
			usersListScrollPane.setViewportView(getLogin_usersList());
			usersListScrollPane.setSize(new java.awt.Dimension(163,316));
		}
		return usersListScrollPane;
	}


	private JList getLogin_usersList() {
		if (login_usersList == null) {
			login_usersList = new JList();
			login_usersList.addMouseListener(new LoginUserListener());
		}
		return login_usersList;
	}

	private JButton getShowUserInfoButton() {
		if (showUserInfoButton == null) {
			showUserInfoButton = new JButton();
			showUserInfoButton.setFont(new Font("\u9ed1\u4f53", Font.PLAIN, 14));
			showUserInfoButton.setText("显示用户资料");
			showUserInfoButton.setLocation(new java.awt.Point(535,441));
			showUserInfoButton.setSize(new java.awt.Dimension(163,21));
			showUserInfoButton.setForeground(java.awt.Color.blue);
			showUserInfoButton.setIcon(new ImageIcon(getClass().getResource("/pictures/ask.gif")));
			showUserInfoButton.setBackground(new java.awt.Color(251,225,251));
			showUserInfoButton.addActionListener(new ShowUserInfoListener());
		}
		return showUserInfoButton;
	}
	
	private JTextPane getChatTextPane() {
		if (chatTextPane == null) {
			chatTextPane = new JTextPane();
			chatTextPane.setBackground(new java.awt.Color(217,235,251));			
			chatTextPane.setFont(new java.awt.Font("黑体", java.awt.Font.PLAIN, 15));
			chatTextPane.setForeground(new java.awt.Color(0,100,0));
			chatTextPane.setText("\t\t--------  沟通无极限! --------\n\n");			
			chatTextPane.setEditable(false);
		}
		return chatTextPane;
	}		
		
	public ChatClient(String inuserID) 
	{
		super();
		initialize(inuserID);
		
		//  启动接收服务器消息线程
		try
		{
			Thread clientThread = new Thread(this);
			clientThread.start();
		}
		catch(Exception e)
		{
			System.out.println("不能启动该线程!" + e);
			dispose();
			return;
		}								
	}

	private void initialize(String inuserID) 
	{		
		this.userID = inuserID;					
		this.setSize(708, 547);
		this.setResizable(false);
		this.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		this.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/pictures/logo1.jpg")));
		this.setForeground(java.awt.Color.cyan);
		this.setContentPane(getJContentPane());
		this.setTitle("Fetion聊天室");
		this.addWindowListener(new java.awt.event.WindowAdapter() 
		{
			public void windowClosed(java.awt.event.WindowEvent evt) 
			{
				System.out.println(userID + "离开聊天室");
				TransData toServerData = new TransData();
				toServerData.flag = TProtocal.EXIT_USER;
				toServerData.dataObj = (String)userID;
				try
				{
					Socket toServer = new Socket(ServerIP.ADDRESS,ServerIP.PORT);
					ObjectOutputStream streamToServer = new ObjectOutputStream(toServer.getOutputStream());
					streamToServer.writeObject((TransData)toServerData);
				}
				catch(Exception e)
				{
					System.out.println("发送用户离开消息到服务器失败!" + e);
					throw new RuntimeException(e);
				}
				System.exit(0);
			}
		}
		);

		this.setVisible(true);
		this.setLocationRelativeTo(null);
		
		
	}

	private JPanel getJContentPane() {
		if (jContentPane == null) {
			pictureLabel = new JLabel();
			pictureLabel.setIcon(new ImageIcon(getClass().getResource("/pictures/Wecome1.JPG")));
			pictureLabel.setSize(new java.awt.Dimension(529,32));
			pictureLabel.setLocation(new java.awt.Point(1,350));
			pictureLabel.setText("");
			welcomeLabel = new JLabel();
			welcomeLabel.setBounds(new java.awt.Rectangle(1,1,528,75));
			welcomeLabel.setIcon(new ImageIcon(getClass().getResource("/pictures/wel.JPG")));
			welcomeLabel.setFont(new java.awt.Font("宋体", java.awt.Font.PLAIN, 22));
			welcomeLabel.setForeground(java.awt.Color.magenta);
			welcomeLabel.setText("");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.setToolTipText("欢迎进入飞信聊天室!");
			jContentPane.setBackground(new java.awt.Color(188,222,255));
			jContentPane.add(getChatTabbedPane(), null);
			jContentPane.add(getMyTabbedPane(), null);
			jContentPane.add(welcomeLabel, null);
			jContentPane.add(getNoticTitleTextField(), null);
			jContentPane.add(getNoticeTextArea(), null);
			jContentPane.add(getUserTitleTextField(), null);
			jContentPane.add(pictureLabel, null);
			jContentPane.add(getToolPanel(), null);
			jContentPane.add(getExitButton(), null);			
			jContentPane.add(getUsersListScrollPane(), null);			
			jContentPane.add(getShowUserInfoButton(), null);
		}
		return jContentPane;

⌨️ 快捷键说明

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