📄 login.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class Login implements ActionListener{
private Frame f;
private TextField tf;
private Label l;
private String tfContent="";
public Login(){
f = new Frame("欢迎登陆");
l = new Label("请输入用户名");
tf = new TextField(20);
f.setLayout(new FlowLayout());
f.add(l);
f.add(tf);
// f.setSize(20,100);
f.pack();
f.show();
tf.addActionListener(this);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e){
System.out.println ("ft clicked");
tfContent = tf.getText();
ClientFrame cf = new ClientFrame(tfContent);
f.dispose();
}
public static void main(String[] args){
new Login();
}
}
class ClientFrame extends Thread implements ActionListener{
private Frame f;
private TextField tf;
private Button btn;
TextArea ta;
private Panel pup;
String data;
BufferedReader InputS;
PrintStream OutputS;
String name;
ClientFrame(String str){
name = str;
Socket client;
try {
client = new Socket("localhost",6000);
InputS = new BufferedReader(new InputStreamReader(client.getInputStream()));
OutputS = new PrintStream(client.getOutputStream(),true);
}
catch (Exception ex) {
ex.printStackTrace();
}
OutputS.println("欢迎" + name + "光临");
f = new Frame("客户端");
tf = new TextField(30);
tf.addActionListener(this);
btn = new Button("发送");
ta = new TextArea();
pup = new Panel();
f.setLayout(new BorderLayout());
f.add(pup,BorderLayout.NORTH);
pup.add(tf);
pup.add(btn);
f.add(ta);
f.pack();
f.setSize(400,300);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
OutputS.println(name + "已经离开");
System.exit(0);
}});
btn.addActionListener(this);
f.show();
this.start();
}
public void run(){
try {
while(true){
System.out.println ("刷屏....");
OutputS.println("\u0001");
ta.setText(InputS.readLine().replaceAll("\u0001","\n"));
Thread.sleep(1000);
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public void actionPerformed(ActionEvent e){
String cmd = e.getActionCommand();
if (cmd.equals("发送")){
System.out.println ("发送 clicked");
data = tf.getText();
try {
OutputS.println(name + "说:" + data);
ta.setText(InputS.readLine().replaceAll("\u0001","\n"));
}
catch (Exception ex) {
ex.printStackTrace();
}
tf.setText("");
}
else{
System.out.println ("发送 clicked");
data = tf.getText();
try {
OutputS.println(name + "说:" + data);
ta.setText(InputS.readLine().replaceAll("\u0001","\n"));
}
catch (Exception ex) {
ex.printStackTrace();
}
tf.setText("");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -