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

📄 chatclient.java

📁 本人大二学习汇编语言程序设计时的全部源代码
💻 JAVA
字号:
package com.paradise.chat;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
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;
import java.net.SocketException;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JOptionPane;
import javax.swing.border.TitledBorder;

public class ChatClient extends JFrame {
	
	private static final long serialVersionUID = -6026557676891249006L;
	private JTextArea display = null;
	private JTextArea input = null;
	private JLabel label = null;
	private JButton sendBt = null;
	private JButton exitBt = null;
	private JPanel pn, ps, pb, pl;

	private Socket s = null;
	private DataInputStream dis = null;
	private DataOutputStream dos = null;
	private boolean isConn = false;

	private Thread recvThread = null;
	private int id= 0;

	public ChatClient() {
		setLayout(new BorderLayout());
		setTitle("MyChatRoom1.0");
		pn = new JPanel();
		pn.setLayout(new BorderLayout());
		display = new JTextArea();
		display.setBorder(new TitledBorder("Client chat room"));
		display.setEditable(false);
		pn.add(new JScrollPane(display), BorderLayout.CENTER);

		pl = new JPanel(new BorderLayout());
		label = new JLabel("input message below:");
		pl.add(label, BorderLayout.WEST);
		pn.add(pl, BorderLayout.SOUTH);

		pb = new JPanel(new FlowLayout());
		sendBt = new JButton("send");
		sendBt.addActionListener(new ButtonListener());
		exitBt = new JButton("exit");
		exitBt.addActionListener(new ButtonListener());
		pb.add(sendBt);
		pb.add(exitBt);

		ps = new JPanel(new BorderLayout());
		input = new JTextArea(3, 15);
		ps.add(new JScrollPane(input), BorderLayout.CENTER);
		ps.add(pb, BorderLayout.SOUTH);

		this.add(pn, BorderLayout.CENTER);
		this.add(ps, BorderLayout.SOUTH);
		this.setSize(400, 350);
		this.setLocation(450, 350);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		this.getConn();
		recvThread = new Thread(new RecvThread());
		recvThread.start();

	}

	public void getConn() {
		try {
			s = new Socket("127.0.0.1", 2008);
			display.append("Get connection successful!" + "\n");
			dis = new DataInputStream(s.getInputStream());
			dos = new DataOutputStream(s.getOutputStream());
			isConn = true;
		} catch (SocketException e) {
			close();
			System.exit(-1);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void send(String str) {
		try {
			dos.writeUTF(str);
			input.setText("");
			dos.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}

	public void close() {
		try {
			if (dos != null)
				dos.close();
			if (dis != null)
				dis.close();
			if (s != null)
				s.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		new ChatClient();
	}

	private class ButtonListener implements ActionListener {

		public void actionPerformed(ActionEvent e) {

			if (e.getSource() == sendBt) {
				String str = input.getText();
				if(str == null || str.trim().equals("")) {
					JOptionPane.showMessageDialog(null, "发送内容不能为空!");
				} else {
					send(str);
				}
				
			} else if (e.getSource() == exitBt) {
				close();
				System.exit(0);
			}
		}
	}

	private class RecvThread implements Runnable {

		public void run() {
			String message = null;
			try {
				while (isConn) {					
					message = dis.readUTF();
					display.append(message + "\n");
				}
			} catch (IOException e) {
				System.out.println("Client closed!");
			} finally {
				try {
					if(dis != null) dis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

}

⌨️ 快捷键说明

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