📄 qqclient.java
字号:
package QQ;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class QQClient{
private JFrame fm;
private JTextArea jta;
private JComboBox jcb;
private JTextField jtf;
private JButton jb;
private String name;
public Socket s=null;
public ObjectInputStream in=null;
public ObjectOutputStream out=null;
public QQClient(String n){
this.name=n;
this.fm=new JFrame("Chat Room "+n);
this.jta=new JTextArea(20,25);
this.jta.setEditable(false);
this.jcb=new JComboBox();
this.jcb.addItem("All");
this.jtf=new JTextField(30);
this.jtf.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
submit();
}
});
fm.setSize(450,300);
fm.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
exit();
fm.dispose();
}
});
fm.getContentPane().add(new JScrollPane(jta),"Center");
JPanel p=new JPanel();
p.add(jcb);
p.add(jtf);
fm.getContentPane().add(p,"South");
fm.setVisible(true);
}
public void connect() throws Exception{
this.s=new Socket("127.0.0.1",1228);
out=new ObjectOutputStream(this.s.getOutputStream());
in=new ObjectInputStream(this.s.getInputStream());
ServerInfo si=new ServerInfo(this.name+" entered!",this.name,ServerInfo.ADD);
out.writeObject(si);
out.flush();
}
public void output() {
try{
while(true){
if (s.isClosed()){
break;
}
else{
Object o=in.readObject();
if (o==null) continue;
ServerInfo si=(ServerInfo)o;
int type=si.getType();
String user=si.getUser();
String str=si.getContent();
if (type==ServerInfo.ADD){
this.jcb.addItem(user);
}
else if(type==ServerInfo.DEL){
this.jcb.removeItem(user);
}
jta.append(str+"\n");
}
}
}
catch(Exception e){}
}
public void submit(){
try{
String str=this.jtf.getText();
String user=(String)jcb.getSelectedItem();
if (user==null) return;
ServerInfo si=new ServerInfo(str,user,ServerInfo.INFO);
out.writeObject(si);
out.flush();
this.jtf.setText("");
}
catch(Exception e){e.printStackTrace();}
}
public void exit() {
try{
ServerInfo si=new ServerInfo(this.name+" exit!",this.name,ServerInfo.DEL);
out.writeObject(si);
out.flush();
}
catch(Exception e){e.printStackTrace();}
if (s!=null){try{s.close();}catch(Exception e){}}
if (in!=null){try{in.close();}catch(Exception e){}}
if (out!=null){try{out.close();}catch(Exception e){}}
}
public static void main(String[] args) throws Exception{
if (args.length==0) {
System.out.println("Use java QQClient NAME");
System.exit(0);
}
QQClient cc=new QQClient(args[0]);
cc.connect();
cc.output();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -