📄 chatclient.java
字号:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.net.*;
import java.applet.Applet;
public class ChatClient extends Applet implements Runnable,ActionListener
{
static CFrame frame;
Thread kicker=null;
JTextField user=new JTextField(20);
JTextArea text=new JTextArea(40,35);
JTextArea msg=new JTextArea();
ButtonGroup bg=new ButtonGroup();
JButton connect=new JButton("Connect");
JButton send=new JButton("Send");
JButton exit=new JButton("Exit");
Socket kkSocket=null;
PrintStream os;
DataInputStream is;
public void init()//初始化
{
setLayout(new CardLayout());
//用户连接服务器界面
JPanel p12=new JPanel(new FlowLayout());
p12.add(new JLabel("Username"));
p12.add(user);
p12.add(connect);
add("login",p12);
//用户登录后的界面
JPanel p4=new JPanel(new BorderLayout());
JPanel p5=new JPanel(new BorderLayout());
p5.add("North",new JLabel("Message Text"));
text.setEditable(false); //不可编辑
p5.add("Center",new JScrollPane(text)); //滚动条
JPanel p6=new JPanel(new GridLayout(1,4));
p6.add(new JLabel(""));
p6.add(send); //发送按钮
p6.add(exit); //退出按钮
p6.add(new JLabel(""));
JPanel p7=new JPanel(new BorderLayout());
p7.add("North",new JLabel("My message"));
p7.add("Center",msg);
p7.add("South",p6);
p4.add("Center",p5);
p4.add("South",p7);
add("send",p4);
connect.addActionListener(this);
send.addActionListener(this);
exit.addActionListener(this);
}
public void start()
{
}
public void stop()
{ //停止执行
if(kicker!=null) kicker.stop();
if(kkSocket!=null)
{
sendData("exit");//发送退出信息
try
{
Thread.sleep(100);
}
catch(InterruptedException e)
{
System.out.println("Error:"+e);
}
closeSocket();
}
}
public void destroy()
{
}
public void run()
{
String fromServer=null;
while((fromServer=getData())!=null) //获取信息
text.append(fromServer+"\n"); //添加到聊天区域后
//否则关闭socket,回到登录界面
kicker=null;
closeSocket();
((CardLayout)getLayout()).show(this,"login");
frame.setTitle("Exit");
frame.pack();
frame.resize(500,400);
}
boolean connectHost()
{ //连接服务器
try
{
kkSocket=new Socket("localhost",4567);
os=new PrintStream(kkSocket.getOutputStream());
is=new DataInputStream(kkSocket.getInputStream());
}
catch(UnknownHostException e)
{
System.out.println("Error:"+e);
return false;
}
catch(Exception e)
{
text.setText("Exception: "+e);
System.out.println("Excepton: "+e);
return false;
}
return true;
}
String getData()//接受信息
{
String fromServer;
try
{
fromServer=is.readLine();
}
catch(Exception e)
{
text.setText("Error:"+e);
System.out.println("Error:"+e);
return null;
}
return fromServer;
}
boolean sendData(String toServer) //发送消息
{
try
{
os.println(toServer);
os.flush();
}
catch(Exception e)
{
text.setText("Error:"+e);
System.out.println("Error:"+e);
return false;
}
return true;
}
void closeSocket() //关闭流和socket
{
try
{
os.close();
is.close();
kkSocket.close();
kkSocket=null;
}
catch(Exception e)
{
text.setText("Error:"+e);
System.out.println("Error:"+e);
}
}
public void actionPerformed(ActionEvent e)//事件响应
{
if(e.getSource()==connect)
{
if(connectHost())//已连接
{
sendData(user.getText());//发送用户名和性别信息
kicker=new Thread(this);
kicker.start();
text.setText("");
msg.setText("");
((CardLayout)getLayout()).show(this,"send");//切换到聊天界面
frame.setTitle("Client-"+user.getText());
frame.pack();
frame.setSize(500,600);
}
}
else if(e.getSource()==send)//发送消息
{
if(sendData(msg.getText())) //发送成功清空发送栏
msg.setText("");
else //否则停止线程,切换到登录界面
{
kicker.stop();
kicker=null;
closeSocket();
((CardLayout)getLayout()).show(this,"login");
frame.setTitle("Client");
frame.resize(500,400);
}
}
else if(e.getSource()==exit)//用户退出,停止线程,切换到登录界面
{
kicker.stop();
kicker=null;
sendData("Exit");//发送退出信息
closeSocket();
((CardLayout)getLayout()).show(this,"exit");
frame.setTitle("Client");
frame.resize(500,400);
}
}
public static void main(String[]args)
{
ChatClient client=new ChatClient();
client.init();
client.start();
frame=new CFrame("ChatroomClient",client);
frame.setBackground(Color.lightGray);
frame.add("Center",client);
frame.resize(500,100);
frame.show();
}
}
class SimpleFrame extends Frame
{
ChatClient client;
public SimpleFrame(String name,ChatClient client)//构造函数
{
super(name);
this.client=client;
}
public boolean handleEvent(Event e)
{
switch(e.id)
{
case Event.WINDOW_DESTROY:
client.stop();
client.destroy();
System.exit(0);
}
return super.handleEvent(e);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -