?? 客戶端.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 + -