📄 clientframe.java
字号:
/*
* Created on 2005-12-19
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package chatClient;
import java.awt.*;
import java.awt.event.*;
//import java.io.Serializable;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.io.*;
import java.net.*;
/**
* @author hongyuan
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class ClientFrame extends JFrame{
private JComboBox comboBox;
private JTextArea textArea;
private JTextField textField;
private JCheckBox checkBox;
private JComboBox actionlist;
private JButton login;
private JButton logout;
private Border border;//用于设定按钮边框
private JTextField seperater;//用于显示提示信息
Socket socket;//用于连接服务器的套接字
ObjectOutputStream output;//用于发送信息
ObjectInputStream input;//用于接收信息
ClientReceiveThread recvThread;//接收线程类的实例
//客户端主框架类的构造函数
public ClientFrame()
{
super("聊天客户端");//设置标题栏
init();
setSize(370,570);
setVisible(true);
}
public void init()
{
Border bevelBorder=BorderFactory.createBevelBorder(BevelBorder.RAISED,Color.white,Color.white,Color.white,Color.white);
Border emptyBorder=BorderFactory.createEmptyBorder(1,1,1,1);
//定义按钮边框
border=BorderFactory.createCompoundBorder(bevelBorder,emptyBorder);
login=new JButton("登录");
logout=new JButton("注销");
//logout.setEnabled(false);//下线按钮为“禁止”状态
login.addActionListener(//为登录按钮注册事件处理程序
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
LogIn();//用户登录
}
}
);
logout.addActionListener(//为注销按钮注册事件处理程序
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
LogOut();//用户下线
}
}
);
//创建用户列表组合框
comboBox=new JComboBox();//创建用户列表组合框
comboBox.addItem("all");
comboBox.setSelectedIndex(0);//设置默认选中列表框中的all项
textArea=new JTextArea(20,20);
textArea.setEditable(false);//在线用户组合框为只读
textField=new JTextField("请输入您的昵称!",20);
seperater=new JTextField("",20);
seperater.setEditable(false);
checkBox=new JCheckBox("悄悄话");
checkBox.setSelected(false);
actionlist=new JComboBox();
actionlist.addItem("微笑着");
actionlist.addItem("生气地");
actionlist.addItem("小心地");
actionlist.setSelectedIndex(0);
login.setBorder(border);
login.setBounds(30,30,90,30);
logout.setBorder(border);
logout.setBounds(150,30,90,30);
seperater.setBounds(30,75,300,20);
textArea.setBounds(30,110,300,300);
comboBox.setBounds(30,425,90,30);
actionlist.setBounds(150,425,90,30);
checkBox.setBounds(270,425,90,30);
textField.setBounds(30,470,300,20);
Container c=getContentPane();//得到画布
c.setLayout(null);
c.add(login,null);
c.add(logout,null);
c.add(seperater,null);
c.add(textArea,null);
c.add(comboBox,null);
c.add(actionlist,null);
c.add(checkBox,null);
c.add(textField,null);
textField.addActionListener(//为系统消息框注册监听程序
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
SendMessage();//发送聊天消息
}
}
);
this.addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
LogOut();//用户下线
System.exit(0);
}
}
);
}
public void LogIn()
{
try
{
socket=new Socket("localhost",8000);//创建套接字并直接连接服务器
output=new ObjectOutputStream(socket.getOutputStream());
output.flush();//发送数据源头
input=new ObjectInputStream(socket.getInputStream());
//准备用户昵称
output.writeObject(textField.getText());
output.flush();
//创建接收线程
recvThread=new ClientReceiveThread(socket,output,input,comboBox,textArea);
recvThread.start();
login.setEnabled(false);
logout.setEnabled(true);
textField.setText("请输入聊天信息!");
}catch(Exception e){
e.printStackTrace();
}
}
public void LogOut()
{
login.setEnabled(true);
logout.setEnabled(false);
textField.setText("请输入您的昵称!");
if(socket.isClosed()) //判断套接字是否已经关闭
{
return;
}
try
{
output.writeObject("用户下线");
output.flush();
input.close();
output.close();
socket.close();
}catch(Exception e){}
}
public void SendMessage()
{
String toSomebody=comboBox.getSelectedItem().toString();//得到信息发送对象
String status="";
//得到信息发送状态
if(checkBox.isSelected())
{
status="悄悄话";
}
String action=actionlist.getSelectedItem().toString();//得到发送动作
String message=textField.getText();//得到信息内容
if(socket.isClosed())
{
return;
}
try
{
output.writeObject("聊天信息");
output.flush();
output.writeObject(toSomebody);//发送聊天对象
output.flush();
output.writeObject(status);//发送聊天状态
output.flush();
output.writeObject(action);//发送聊天表情
output.flush();
output.writeObject(message);//发送聊天信息内容
output.flush();
}catch(Exception e){}
}
public static void main(String [] args)
{
new ClientFrame();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -