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

📄 chat.java

📁 JAVA聊天室程序(TCP/IP)
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.net.*;

public class Chat
{
	private JFrame chatFrame;
	private Container container;

	private JLabel addLabel;
	private JTextField addTextField;
	private JButton connectButton;
	private JPanel connectPanel;
	private JLabel stateLabel;

	private JTextArea msgTextArea;
	private JScrollPane msgPane;

	private JTextField sendTextField;
	private JButton sendButton;
	private JPanel sendPanel;

	private ServerSocket server;
	private Socket clientSocket;
	private PrintWriter pw;
	

	public Chat()
	{
		chatFrame=new JFrame("聊天室 制作:杜晓东 2006年9月");
		container=chatFrame.getContentPane();
		
		addLabel=new JLabel("IP地址:");
		addTextField=new JTextField(15);
		connectButton=new JButton("连接");
		stateLabel=new JLabel("当前状态:");
		connectPanel=new JPanel();

		msgTextArea=new JTextArea();
		msgPane=new JScrollPane(msgTextArea);
		msgTextArea.setEditable(false);
		msgTextArea.setAutoscrolls(true);
		
		sendTextField=new JTextField(50);
		sendButton=new JButton("提交");
		sendPanel=new JPanel();

		initGUI();		
	}

	private void initGUI()
	{
		connectPanel.setLayout(new FlowLayout(FlowLayout.LEFT,25,5));
		connectPanel.add(addLabel);
		connectPanel.add(addTextField);
		connectPanel.add(connectButton);
		connectPanel.add(stateLabel);

		sendPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		sendPanel.add(sendTextField);
		sendPanel.add(sendButton);

		container.add(connectPanel,BorderLayout.NORTH);
		container.add(msgPane,BorderLayout.CENTER);
		container.add(sendPanel,BorderLayout.SOUTH);

		chatFrame.setSize(800,600);
		chatFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		ConnectListener conn=new ConnectListener();
		connectButton.addActionListener(conn);
		addTextField.addActionListener(conn);
		
		SendListener send=new SendListener();
		sendButton.addActionListener(send);
		sendTextField.addActionListener(send);
	}

	public void go()
	{
		chatFrame.setVisible(true);
		new Thread(new ConnectHost()).start();		
	}

	private class ConnectHost implements Runnable
	{
		public void run()
		{

			while(true)
			{
				if (clientSocket!=null)
				{
					stateLabel.setText("当前状态:已连接...");
					try
					{
						pw=new PrintWriter(clientSocket.getOutputStream(),true);
						BufferedReader br=new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
						String strResive;
						while(true)
						{
							strResive=br.readLine();
							if (!strResive.equals("null"))
								msgTextArea.append(strResive+"\n");
						
						}
					}
					catch(Exception e)
					{
						server=null;
						clientSocket=null;
					}
					
				}
				try
				{
					
					server=new ServerSocket(10000);
					stateLabel.setText("当前状态:正在监听...");
					clientSocket=server.accept();
					stateLabel.setText("当前状态:已连接...");
				}
				catch(Exception e)
				{
					stateLabel.setText("当前状态:本地端口已被占用...");
				}
			}
		}


	}

	private class SendListener implements ActionListener
	{
		public void actionPerformed(ActionEvent ae)
		{
			if (sendTextField.getText().trim().equals(""))
				return;
			if (clientSocket==null)
				return;
			
			pw.println(sendTextField.getText().trim());
			msgTextArea.append(sendTextField.getText()+"\n");
			sendTextField.setText("");
		}
	}

	private class ConnectListener implements ActionListener
	{
		public void actionPerformed(ActionEvent ae)
		{
			if (addTextField.getText().trim().equals(""))
				return;
			String ipAddress=addTextField.getText().trim();
			try
			{
				clientSocket=new Socket(ipAddress,10000);
				stateLabel.setText("当前状态:已连接");
				server.close();
				
			}
			catch(Exception e)
			{
				stateLabel.setText("当前状态:连接失败");
			}
		}
	}


	public static void main(String[] args)
	{
		new Chat().go();
	}
}

⌨️ 快捷键说明

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