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

📄 client.java

📁 一个简单的java聊天程序
💻 JAVA
字号:
import javax.swing.*;

import sun.reflect.generics.scope.Scope;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class Client extends JFrame implements Runnable, ActionListener {

	public static final int PORT = 8765;

	JPanel jPanel1 = new JPanel();
	JPanel jPanel2 = new JPanel();
	JPanel jPanel3 = new JPanel();
	JPanel jPanel4 = new JPanel();

	BorderLayout borderLayout1 = new BorderLayout();
	JScrollPane jScrollPane2 = new JScrollPane();
	FlowLayout flowLayout1 = new FlowLayout();
	GridLayout gridLayout1 = new GridLayout();

	JButton jButton1 = new JButton();
	JButton jButton2 = new JButton();
	JButton jButton3 = new JButton();

	JTextField jTextField1 = new JTextField();
	JTextField jTextField2 = new JTextField();
	JTextField jTextField3 = new JTextField();
	JTextField jTextField4 = new JTextField();
	JTextArea jTextArea1 = new JTextArea();

	//
	JLabel jLabel1 = new JLabel();
	JLabel jLabel2 = new JLabel();
	JLabel jLabel3 = new JLabel();

	Socket socket;
	Thread thread;

	String name, chat_txt, chat_in;
	String ip = null;

	DataInputStream in;
	DataOutputStream out;
	boolean bool = false;

	public Client() {
		getContentPane().setLayout(null);

		jTextArea1.setText("");
		jTextField1.setText("用户名输入");
		jTextField1.setBounds(new Rectangle(103, 4, 77, 25));
		jTextField2.setText("");
		jTextField2.setBounds(new Rectangle(4, 6, 311, 22));
		jTextField3.setText("");
		jTextField4.setText("8765");

		jButton1.setBounds(new Rectangle(210, 4, 89, 25));
		jButton1.setToolTipText("");
		jButton1.setText("进入聊天室");
		jButton1.addActionListener(this);
		jButton2.setBounds(new Rectangle(322, 5, 73, 25));
		jButton2.setText("发送");
		jButton2.addActionListener(this);
		jButton3.setBounds(new Rectangle(302, 5, 97, 24));
		jButton3.setText("退出聊天室");
		jButton3.addActionListener(this);

		jPanel1.setLayout(null);
		jPanel1.setBounds(new Rectangle(0, 0, 400, 35));
		jPanel2.setBounds(new Rectangle(264, 35, 136, 230));
		jPanel2.setLayout(gridLayout1);
		jPanel3.setBounds(new Rectangle(0, 35, 255, 230));
		jPanel3.setLayout(borderLayout1);
		jPanel4.setBounds(new Rectangle(0, 265, 400, 35));
		jPanel4.setLayout(null);

		//
		jLabel1.setText("用户名");
		jLabel1.setBounds(new Rectangle(46, 5, 109, 23));
		jLabel2.setText("服务器地址");
		jLabel3.setText("端口");

		//
		gridLayout1.setColumns(1);
		gridLayout1.setRows(4);
		this.getContentPane().add(jPanel1, null);
		jPanel4.add(jTextField2);
		jPanel4.add(jButton2);
		jPanel1.add(jTextField1, null);
		
		jPanel1.add(jButton1, null);
		jPanel1.add(jButton3);
		jPanel1.add(jLabel1);
		this.getContentPane().add(jPanel2, null);
		jPanel2.add(jLabel2);
		jPanel2.add(jTextField3);
		jPanel2.add(jLabel3);
		jPanel2.add(jTextField4);
		this.getContentPane().add(jPanel3, null);
		this.getContentPane().add(jPanel4, null);
		
		jPanel3.add(jScrollPane2, java.awt.BorderLayout.CENTER);
		jScrollPane2.getViewport().add(jTextArea1);

		this.setSize(400, 400);
		this.setVisible(true);

	}

	public static void main(String[] args) {
		Client client = new Client();
		client.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}

	public void run() {

		while (true) {
			try {
				chat_in = in.readUTF();
				jTextArea1.append(chat_in);
			} catch (IOException e) {

				System.out.println("in the client");
				e.printStackTrace();
			}
		}
	}

	public void actionPerformed(ActionEvent e) {

		if (e.getSource() == jButton1) {
			name = jTextField1.getText();
			ip = jTextField3.getText();
			if (name != "用户名输入" && ip != null) {
				try {
					socket = new Socket(ip, PORT);
					in = new DataInputStream(socket.getInputStream());
					out = new DataOutputStream(socket.getOutputStream());
				} catch (IOException e1) {
					System.out.println("can not connect");
					System.out.println("in the client");
				}
				
				thread = new Thread(this);
				thread.start();
				bool = true;
			}
		} else if (e.getSource() == jButton2) {
			chat_txt = jTextField2.getText();
			if (chat_txt != null) {
				try {
					out.writeUTF(jTextField1.getText() + "对大家说" + chat_txt
							+ "\n");

				} catch (IOException e2) {
					System.out.println("in the client");
				}

			} else {
				try {
					out.writeUTF("青说话");
				} catch (IOException e3) {System.out.println("in the client");
				}
			}
		} else if (e.getSource() == jButton3) {
			if (bool == true) {
				try {
					socket.close();

				} catch (IOException e4) {System.out.println("in the client");
				}

			}
			thread.destroy();
			bool = false;
			this.setVisible(false);
		}
	}

}

⌨️ 快捷键说明

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