📄 chatroomclient.java
字号:
package package0;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
public class ChatRoomClient extends Frame implements Runnable{
Panel panel1,panel2;
JButton button1,button2,button3;
TextField textField;
TextArea textArea,textField2;
ScrollPane sPanel;
PrintWriter out;
BufferedReader in=null;
Socket sock;
public final static int DEFAULT_PORT=8189;
public ChatRoomClient()
{
try{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
private void jbInit() //按钮设置
{
button1=new JButton("<html><font color=green>连接</font></html>");
button2=new JButton("<html><font color=green>发送</font></html>");
button3=new JButton("<html><font color=green>关闭</font></html>");
textField=new TextField("input Server address here!");
textField2=new TextArea("input Message here and send to server");
panel1=new Panel();
panel2=new Panel();
sPanel=new ScrollPane();
textArea=new TextArea();
button1.addActionListener(new java.awt.event.ActionListener(){//连接按钮
public void actionPerformed(ActionEvent e){
button1_actionPerformed(e);
}
});
textField.addKeyListener(new textField_KeyAdapter(this));//连接的IP
panel1.add(textField);
panel1.add(button1);
sPanel.add(textArea);//对话框
button2.addActionListener(new java.awt.event.ActionListener(){//发送按钮
public void actionPerformed(ActionEvent e){
button2_actionPerformed(e);
}
});
textField2.addKeyListener(new textField_KeyAdapter(this));//发送信息
button3.addActionListener(new java.awt.event.ActionListener(){//关闭按钮
public void actionPerformed(ActionEvent e){
button_actionPerformed(e);
}
});
Panel tempPanel = new Panel(new GridLayout());
tempPanel.add(new JLabel());
tempPanel.add(button2);
tempPanel.add(new JLabel());
tempPanel.add(button3);
tempPanel.add(new JLabel());
panel2.setLayout(new BorderLayout());
panel2.add(textField2,BorderLayout.NORTH);
panel2.add(tempPanel,BorderLayout.CENTER);
this.addWindowListener(new ChatFrame_WindowAdapter(this));
this.setLayout(new BorderLayout());
this.setTitle(" ChatRoom");
this.setSize(400,500);
this.add(panel1,BorderLayout.NORTH);
this.add(sPanel,BorderLayout.CENTER);
this.add(panel2,BorderLayout.SOUTH);
}
private void button1_actionPerformed(ActionEvent e){//连接按钮
startConnect();
}
public void startConnect(){//开始连接
try{
sock=new Socket(textField.getText(),DEFAULT_PORT);// 连接服务器
if(sock!=null){
processMsg("Connect successfully!");
}
in=new BufferedReader(new InputStreamReader(sock.getInputStream()));
out=new PrintWriter(sock.getOutputStream());
}
catch(IOException ex){ processMsg(ex.toString());
processMsg("Connect failed!");
}
Thread readThread = new Thread(this);
readThread.start();
}
private void button2_actionPerformed(ActionEvent e){//发送按钮
sendInformation();
}
public void sendInformation(){
out.println(textField2.getText());
out.flush();
}
private void button_actionPerformed(ActionEvent e){
exit();
}
public void stopRun(){
boolean isTrue = false;
}
public void processMsg(String msg){//客户端处理消息
textArea.append(msg);
textArea.append("\n");
}
public void run(){
String msg;
boolean isTrue=true;
while(isTrue){
try{
msg=in.readLine();
if(msg.equals("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{
out.println("Client exit!");
out.flush();
}
catch(Exception exc){}
try{
sock.close();
in.close();
out.close();
}catch(IOException ioe){}
finally{
System.exit(0);
}
}
public static void main(String[] args) //main()函数
{
ChatRoomClient c=new ChatRoomClient();
c.show();
}
}
//文本筐textField的键击事件适配器
class textField_KeyAdapter extends java.awt.event.KeyAdapter{
ChatRoomClient chatFrame;
public textField_KeyAdapter(ChatRoomClient chatFrame){
this.chatFrame=chatFrame;
}
public void keyPressed(KeyEvent e){//输入的是enter,开始连接!
int j=e.getKeyCode();
if(j==e.VK_ENTER){
chatFrame.startConnect();
}
}
}
//文本筐textArea12的键击事件适配器
class textArea12_KeyAdapter extends java.awt.event.KeyAdapter{
ChatRoomClient chatFrame;
public textArea12_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();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -