📄 客户端.txt
字号:
package day15.chat;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.*;
public class Client {
JPanel jp1=new JPanel();
JFrame jf=new JFrame("聊天室");
JOptionPane jop=new JOptionPane();
JTextField jtfHost = new JTextField("127.0.0.1");//设置初始界面
JTextField jtfPort = new JTextField("7456");
JTextField jtfSend=new JTextField("",20);//设置聊天界面
JTextArea jtfMessage=new JTextArea("",300,200);
JFrame jf2=new JFrame("聊天室");
private Socket so;//Socket
private PrintStream ps;////客户端写
private InputStreamReader isr;//客户端读
private BufferedReader br;//读
private static ThreadLocal<String> tl=new ThreadLocal<String>();
ClientThread ctFresh=new ClientThread();
private boolean freshGoon=true;//是否刷新
public Client (){
System.out.println("begin");
// jf.setLayout(new FlowLayout());
JLabel jlHost=new JLabel("服务器");
JLabel jlPort = new JLabel("端口");
JButton jbStart=new JButton("确认");
jbStart.addActionListener(new ActionListener(){//添加 确认按钮 的 起动客户端的事件
public void actionPerformed(ActionEvent e) {
String host=jtfHost.getText().trim();
try {
so=new Socket(host, Integer.parseInt(jtfPort.getText().trim()));
} catch (NumberFormatException e1) {
JOptionPane.showMessageDialog(jf,"IP 地址或 端口 错误");
return;
} catch (UnknownHostException e1) {
JOptionPane.showMessageDialog(jf,"找不到该主机");
return;
} catch (java.net.ConnectException e1) {
JOptionPane.showMessageDialog(jf,"无法连接到服务器");
return;
}catch (Exception e1) {
JOptionPane.showMessageDialog(jf,"未知错误");
return;
}
jf.setVisible(false);
jf2.setVisible(true);
try{
ps= new PrintStream(so.getOutputStream());//客户端写
isr=new InputStreamReader(so.getInputStream());//客户端读
br=new BufferedReader(isr);//读
}
catch(Exception ee){
JOptionPane.showMessageDialog(jf,"未知错误");
}
ctFresh.start();//起动Fresh 刷新Thread " ClientThread"
tl.set("main");//设置主线程的ThreadLocal变量
}
});
jp1.add(jlHost);
jp1.add(jtfHost);
jp1.add(jlPort);
jp1.add(jtfPort);
jp1.add(jbStart);
jf.add(jp1,BorderLayout.NORTH);
//设置聊天界面
jtfSend.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if(jtfSend.getText()!=""){
sendMessage();
jtfSend.setText("");
}
}
});
jf2.add(jtfSend,BorderLayout.NORTH);
jf2.add(jtfMessage,BorderLayout.CENTER);
jf.setSize(200,300);//设置位置等
jf.setLocation(200,300);
jf2.setSize(200,300);//设置位置等
jf2.setLocation(200,300);
jf2.addWindowListener(new WindowListener(){
public void windowOpened(WindowEvent e) {
}
public void windowClosing(WindowEvent e) {
JOptionPane.showMessageDialog(jop,"hah closing");
try {
freshGoon=false;
ps.println("bye");//客户端写
ps.close();
br.close();
so.close();
} catch (IOException e1) {
JOptionPane.showMessageDialog(jop,"Error:When closing");
// e1.printStackTrace();
}
jf.dispose();
jf2.dispose();
}
public void windowClosed(WindowEvent e) {
try {
ps.println("bye");//客户端写
ps.close();
br.close();
so.close();
} catch (IOException e1) {
e1.printStackTrace();
}
jf.dispose();
jf2.dispose();
}
public void windowIconified(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowActivated(WindowEvent e) {
}
public void windowDeactivated(WindowEvent e) {
}
});
jp1.setVisible(true);//设置显示
jf.setVisible(true);
jf.pack();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public synchronized void sendMessage(){//主线程会和刷新线程共用这个方法
try {
if(this.tl==null)System.out.println("tl null");
if("Fresh".equals(tl.get())){//刷新线程
ps.println("\u0000");//客户端写
}else{//主线程
ps.println(this.jtfSend.getText());//客户端写
}
this.jtfMessage.setText(br.readLine().replaceAll("\u0001","\n"));//客户端读
}catch (IOException e) {
JOptionPane.showMessageDialog(jf,"发送失败");
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
new Client ();
}
private class ClientThread extends Thread{
public ClientThread(){
tl.set("Fresh");//设置刷新线程的ThreadLocal变量
}
public void run(){
while(freshGoon){
try {
Thread.sleep(5000);//每5秒刷新一次
} catch (InterruptedException e) {
e.printStackTrace();
}
sendMessage();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -