📄 chatroomclient.java
字号:
package chatclient;
//chat room client
//author Hailiang Yue
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class ChatRoomClient extends Frame implements Runnable{
/**
*
*/
private static final long serialVersionUID = 1L;
Panel panel1,panel2;
Button button1,button2,button3;
TextField textField1,textField2;
Label label1;
TextArea textArea;
ScrollPane sPanel;
//net
PrintWriter out;
BufferedReader in=null;
Socket sock;
public final static int DEFAULT_PORT=6666;
//创建线程 从server处读取信息
Thread readThread;
boolean isTrue=true;
public ChatRoomClient(){
super("ChatRoomClient");//设定标题栏
try{
jbInit();
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args){//主方法
ChatRoomClient c=new ChatRoomClient();
c.setVisible(true);
}
private void jbInit(){//设置大小,位置和组建的字
button1=new Button("连接");//设置字
button2=new Button("发送");
button3=new Button("退出");
textField1=new TextField("Input Server IP address here!");
textField2=new TextField("Input Message here and Send to Server");
label1=new Label("消息");
panel1=new Panel();
panel2=new Panel();
sPanel=new ScrollPane();
textArea=new TextArea();
//panel
//按button1:连接 使clients和server进行连接
button1.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
button1_actionPerformed(e);
}
});
//textField1:for input the address of server; be registered to KeyListener
//press key:enter to connect the client to server
textField1.addKeyListener(new textField1_KeyAdapter(this));
panel1.add(button1);
panel1.add(textField1);
//sPanel ScrollPane
sPanel.add(textArea);
//panel2
//press button2: 发送 to send message
button2.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
button2_actionPerformed(e);
}
});
button3.addActionListener(new java.awt.event.ActionListener(){//退出
public void actionPerformed(ActionEvent e){
button3_actionPerformed(e);
}
});
//textField2:for input message;be registered to KeyListener.
//press key:Enter to send message
textField2.addKeyListener(new textField2_KeyAdapter(this));
panel2.add(label1);
panel2.add(textField2);
panel2.add(button2);
panel2.add(button3);
this.addWindowFocusListener(new ChatFrame_WindowAdapter(this));
//frame is registered to WindowListener
this.setLayout(new BorderLayout());
this.setSize(500,400);
this.add(panel1,BorderLayout.NORTH);
this.add(sPanel,BorderLayout.CENTER);
this.add(panel2,BorderLayout.SOUTH);
}
public void startConnect(){//开始连接
try{
sock=new Socket(textField1.getText(),DEFAULT_PORT);
if(sock!=null){//连接成功
processMsg("连接成功");
}
in=new BufferedReader(new InputStreamReader(sock.getInputStream()));
out=new PrintWriter(sock.getOutputStream());
}catch(IOException ex){
processMsg(ex.toString());
processMsg("连接失败");
}
readThread=new Thread(this);
readThread.start();
}
public void sendInformation(){
out.println(textField2.getText());
out.flush();
}
private void button3_actionPerformed(ActionEvent e){
exit();
}
private void button1_actionPerformed(ActionEvent e){//连接按钮
startConnect();
}
private void button2_actionPerformed(ActionEvent e){//发送按钮
sendInformation();
}
public void stopRun(){//to stop the running thread
isTrue=false;
}
public void processMsg(String msg){//客户端处理消息
textArea.append(msg);
textArea.append("\n");
}
public void run(){
String msg;
isTrue=true;
while(isTrue){
try{
msg=in.readLine();
if(msg.equals("Server exit!")){//server exit
processMsg(msg);
stopRun();//中止线程
}else if(msg!=null){
processMsg(msg);
}
Thread.sleep(1000);
}catch(IOException e){
processMsg(e.toString());
}catch(InterruptedException ei){
processMsg(ei.toString());
}
}
try{//服务器退出关闭连接和相关的“流”
sock.close();
in.close();
out.close();
}catch(IOException ioe){}
}
public void exit(){//窗口关闭;如果有连接则关闭连接和相关的“流”
try{//send "client exit!"to server
out.println("Client exit!");
out.flush();
}catch(Exception exc){}
try{//close iostream
sock.close();
in.close();
out.close();
}catch(IOException ioe){}
finally{
System.exit(0);
}
}
}
//文本框textfield1的键击事件适配器
class textField1_KeyAdapter extends java.awt.event.KeyAdapter{
ChatRoomClient chatFrame;
public textField1_KeyAdapter(ChatRoomClient chatFrame){
this.chatFrame=chatFrame;
}
public void keyPressed(KeyEvent e){//输入的是enter,开始连接!
int j=e.getKeyCode();
if(j==e.VK_ENTER){
chatFrame.startConnect();
}
}
}
//文本框textfield2的键击事件适配器
class textField2_KeyAdapter extends java.awt.event.KeyAdapter{
ChatRoomClient chatFrame;
public textField2_KeyAdapter(ChatRoomClient chatFrame){
this.chatFrame=chatFrame;
}
public void keyPressed(KeyEvent e){//单击enter,发送消息
int j=e.getKeyCode();
if(j==e.VK_ENTER){
chatFrame.sendInformation();
}
}
}
class ChatFrame_WindowAdapter extends java.awt.event.WindowAdapter{
ChatRoomClient chatFrame;
public ChatFrame_WindowAdapter(ChatRoomClient chatFrame){
this.chatFrame=chatFrame;
}
public void windowClosing(WindowEvent e){//退出程序
chatFrame.exit();//应用chatroomclient里的方法
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -