📄 clientframe.java
字号:
//ClientFrame 类
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.net.*;
public class ClientFrame extends JFrame
{
private JComboBox combobox; //用户列表
private JTextArea textarea; //聊天信息显示框
private JTextField textfield; //输入聊天信息
private JButton login; //登录按钮
private JButton logout; //登出按钮
private JTextField serverIP;
Socket socket; //客户机socket
ObjectOutputStream output; //输出流
ObjectInputStream input; //输入流
ClientReceiveThread recvThread; //客户机接受线程对象
public ClientFrame()
{
super("client");
login = new JButton("登录");
logout = new JButton("登出");
logout.setEnabled(false);
combobox = new JComboBox();
combobox.addItem("所有人");
combobox.setSelectedIndex(0);
textarea = new JTextArea(20,20);
textarea.setEditable(false);
textfield = new JTextField("请输入您的昵称!",20);
JLabel Label = new JLabel("服务器IP:");
serverIP = new JTextField("172.18.17.206",15);
login.setBounds(10,50,90,30);
logout.setBounds(150,50,90,30);
textarea.setBounds(10,110,300,300);
combobox.setBounds(10,425,90,30);
textfield.setBounds(10,470,300,20);
Label.setBounds(10,10,70,20);
serverIP.setBounds(100,10,100,20);
Container c = getContentPane();
c.setLayout(null);
c.add(login,null);
c.add(logout,null);
JScrollPane JP = new JScrollPane(textarea);
JP.setBounds(10,110,300,300);
c.add(JP);
c.add(combobox,null);
c.add(textfield,null);
c.add(serverIP,null);
c.add(Label,null);
setSize(330, 550);
setVisible(true);
login.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
LogIn();
}
});
logout.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
LogOut();
}
});
textfield.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
SendMessage();
}
});
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public static void main(String[] args)
{
new ClientFrame();
}
public void LogIn()
{
try
{
socket = new Socket(serverIP.getText(),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){}
}
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 = "";
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(message);
output.flush();
}
catch(Exception e){}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -