📄 guiclient.java
字号:
import java.io.*;
import java.net.*;
import java.applet.*;
import java.util.*;
import javax.swing.*;
public class GUIClient extends JApplet
{
public static final int port = 1111;
public static final String host = "localhost";
public static final int maxChars = 400;
//消息到来的监听对象
protected Listener listener = null;
protected Talk talk = null;
//客户端socket
protected Socket s = null;
public void start()
{
System.err.println("start() called");
try
{
s = new Socket(host, port);
}
catch (IOException e)
{
System.err.println("Couldn't make socket: "+ e);
this.cleanUp();
return;
}
//实例化监听对象
listener = new Listener(s, displayText, this);
//实例化发送对象
talk = new Talk(s, sendText, this);
}
public void AppendText(String toBeAppended)
{
System.out.println("appendText "+toBeAppended);
JTextArea w = displayText;
String text = w.getText();
if(text.length() + toBeAppended.length() > maxChars)
{
//找到最早的一行
int upTo = text.indexOf("\n");
System.out.println("content before "+upTo+" is delete");
//消除一行
w.replaceRange("", 0, upTo+1);
//text = w.getText();
}
w.append("\n"+toBeAppended);
}
public void init()
{
initComponents();
}
private void initComponents()
{
displayText = new javax.swing.JTextArea();
sendText = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jList1 = new javax.swing.JList();
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
addMouseListener(new java.awt.event.MouseAdapter()
{
public void mouseReleased(java.awt.event.MouseEvent evt)
{
formMouseReleased(evt);
}
});
getContentPane().add(displayText, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 290, 240));
displayText.setLineWrap(true);
displayText.setText("you must login first!!!");
sendText.setText("jTextField1");
sendText.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
jTextField1ActionPerformed(evt);
}
});
getContentPane().add(sendText, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 270, 290, -1));
jLabel1.setText("jLabel1");
getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 250, 290, -1));
jList1.setBorder(new javax.swing.border.MatteBorder(null));
jList1.setBackground(java.awt.Color.darkGray);
jList1.setPreferredSize(new java.awt.Dimension(50, 80));
getContentPane().add(jList1, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 0, 120, 290));
}
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt)
{
if (talk != null)
{
//调用talk线程的发送方法
talk.sendLine();
}
}
private void formMouseReleased(java.awt.event.MouseEvent evt)
{
// 此处读者自己添加
}
// 聊天显示区
private javax.swing.JTextArea displayText;
// 消息发送区
private javax.swing.JTextField sendText;
//发送对象,读者自己完成
private javax.swing.JLabel jLabel1;
//在线用户列表,读者自己完成
private javax.swing.JList jList1;
public void destroy()
{
System.err.println("destroy() called");
}
public void finalize() throws Throwable
{
System.err.println("finalize() called");
}
public void stop()
{
System.err.println("stop() called");
cleanUp();
super.stop();
}
public void cleanUp()
{
System.err.println("cleanUp() called");
if (talk != null && talk.running)
{
talk.stop();
}
talk = null;
System.err.println("talk thread stopped and forgotten");
if (listener != null && listener.running)
{
System.err.println("about to stop listener...");
listener.stop();
System.err.println("listener stopped ok");
}
listener = null;
System.err.println("listener thread stopped and forgotten");
removeAll();
System.err.println("windows deleted");
try
{
if (s != null)
{
s.close();
System.err.println("socket closed ok");
}
}
catch (IOException e)
{
System.err.println("Couldn't close socket: " + e);
}
s = null;
}
}
class Talk extends Thread {
protected PrintStream sout;
protected JTextField w;
protected boolean running = false;
protected GUIClient applet;
public Talk(Socket s, JTextField window, GUIClient applet)
{
//后台打印信息
System.err.println("Making a talk...");
//初始化文本域
w = window;
//初始化applet
this.applet = applet;
try
{
//得到输出流
sout = new PrintStream(s.getOutputStream());
System.err.println("Talk thread opened output stream to server");
}
catch (IOException e)
{
System.err.println ("Talk thread can't open output stream to server");
running = false;
applet.cleanUp();
return;
}
//启动线程
this.start();
}
public void run()
{
System.err.println("Talk thread running...");
running = true;
}
protected void finalize() throws Throwable
{
sout.close();
sout = null;
System.err.println("Talk thread closed its output stream.");
System.out.println("Talk thread stopped.");
}
//该方法在发送按钮被点击或用户回车时触发
public void sendLine()
{
String line = w.getText();
w.setText("");
if (line == null||line.equals(""))
{
return;
}
//此处请读者添加协议处理
//提供一个断行符
line = line.trim() + "\n";
sout.print(line);
sout.flush();
}
}
class Listener extends Thread
{
protected DataInputStream sin;
protected JTextArea w;
//监听类是否运行标志
protected boolean running = false;
protected GUIClient applet;
public Listener(Socket s, JTextArea window, GUIClient applet)
{
//后台打印初始化信息
System.out.println("Making a listener...");
//初始化文本域
w = window;
//初始化容器
this.applet = applet;
try
{
//得到输入流对象
sin = new DataInputStream(s.getInputStream());
System.out.println("Listener thread opened its input stream.");
}
catch (IOException e)
{
System.err.println ("Listener thread can't open input stream: " + e);
running = false;
applet.cleanUp();
return;
}
//初始化以后启动线程
this.start();
}
public void run()
{
//打印后台运行信息,设置运行标志
System.out.println("Listener thread starting...");
running = true;
String line;
while (true)
{
try
{
//得到收到的信息
line = sin.readLine();
}
catch (IOException e)
{
System.err.println("Listener: error in readLine: " + e);
running = false;
applet.cleanUp();
return;
}
// 如果连接中断
if (line == null)
{
System.out.println("Listener find connection closed: closing");
running = false;
applet.cleanUp();
return;
}
applet.AppendText(line);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -