📄 qqclient.java
字号:
import java.net.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
public class QQClient {
String name;
BufferedReader in;
PrintWriter out;
JTextField jtf;
JTextArea jta;
JComboBox jcb;
public static void main(String[] args) {
if (args.length==0 || args[0].equals("All")||args[0].indexOf(":")!=-1){
System.out.println("Error!");
System.exit(0);
}
Socket s=null;
try {
s = new Socket("127.0.0.1",7755);
QQClient client=new QQClient(s,args[0]);
client.prepare();
client.receive();
} catch (Exception e) {
e.printStackTrace();
}
}
public QQClient(Socket s,String name) throws Exception{
this.name=name;
this.in=new BufferedReader(new InputStreamReader(s.getInputStream()));
this.out=new PrintWriter(s.getOutputStream());
JFrame frame=new JFrame("Client "+name);
frame.setSize(400,300);
JPanel panel=new JPanel();
this.jcb=new JComboBox();
jcb.addItem("All");
this.jtf=new JTextField(25);
panel.add(jcb);
panel.add(jtf);
this.jta=new JTextArea();
this.jta.setEditable(false);
frame.getContentPane().add(new JScrollPane(jta));
frame.getContentPane().add(panel,"South");
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent arg0) {
end();
}
});
this.jtf.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
send();
}
});
}
void prepare() throws Exception{
out.println("1:"+name+":");
out.flush();
}
void end(){
out.println("2:"+name+":");
out.flush();
System.exit(0);
}
void send(){
String receiver=(String)(this.jcb.getSelectedItem());
if (receiver==null) return;
String text=this.jtf.getText();
if ((text.indexOf(":"))!=-1){
this.jtf.setText("");
return;
}
out.println("3:"+receiver+":"+text);
out.flush();
this.jtf.setText("");
}
void receive(){
while(true){
try {
String str=in.readLine();
String[] info=parseString(str);
if (info[0].equals("1")){
this.jta.append(info[1]+" entered!"+"\n");
this.jcb.addItem(info[1]);
}
else if (info[0].equals("2")){
this.jta.append(info[1]+" exit!"+"\n");
this.jcb.removeItem(info[1]);
}
else{
this.jta.append(info[1]+" said:"+info[2]+"\n");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
private String[] parseString(String s){
String[] ss=new String[3];
int i=0;
StringTokenizer st=new StringTokenizer(s,":");
while(st.hasMoreTokens()){
ss[i]=st.nextToken();
i++;
}
return ss;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -