📄 clientgui.java
字号:
package client;
import java.awt.event.*;
import javax.swing.*;
public class ClientGUI {
String name;
int type;
JFrame f;
JTextArea jta;
JTextField jtf;
JComboBox jcb;
ClientAction action;
public ClientGUI(String name, int type) {
super();
this.name = name;
this.type = type;
initGui();
service();
}
public void initGui(){
f=new JFrame(name);
f.setSize(400,300);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent arg0) {
action.send("2:"+name);
System.exit(0);
}
});
jta=new JTextArea();
jta.setEditable(false);
f.add(jta);
jtf=new JTextField(25);
jcb=new JComboBox();
JPanel p=new JPanel();
p.add(jcb);
p.add(jtf);
f.add(p,"South");
f.setVisible(true);
jtf.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
String text=jtf.getText();
String to=(String)jcb.getSelectedItem();
action.send("0:"+name+":"+to+":"+text);
}
});
}
public void service(){
if (type==0) action=new TCPClientAction();
else action=new UDPClientAction();
action.send("1:"+name);
while(true){
String text=action.receive();
String[] ss=text.split(":");
if (ss[0].equals("0")){
jta.append(ss[1]+" said:"+ss[3]+"\n");
}
if (ss[0].equals("1")){
jta.append(ss[1]+" login!\n");
jcb.addItem(ss[1]);
}
if (ss[0].equals("2")){
jta.append(ss[1]+" logout!\n");
jcb.removeItem(ss[1]);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -