📄 myclient.java
字号:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.PrintWriter;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
public class myclient extends JFrame implements Runnable {
/**
* Launch the application
*
* @param args
*/
public static void main(String args[]) {
try {
myclient frame = new myclient();
frame.setVisible(true);
Thread th = new Thread(frame);
th.start();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the frame
*/
final JLabel label = new JLabel();
final JTextArea txtareasay = new JTextArea();
String myName = "";
final JTextField txtsay = new JTextField();
final JButton btsay = new JButton();
DefaultListModel defaultmodel = new DefaultListModel();
private PrintWriter out;
private BufferedReader in;
public void init(BufferedReader in, PrintWriter out, String name) {
this.out = out;
this.in = in;
label.setText("欢迎你:" + name);
myName = name;
Thread th = new Thread(this);
th.start();
// defaultmodel.addElement(name);// 把人名加到list中
}// null 模型显示元素。
public myclient() {
super();
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setTitle("聊天室");
getContentPane().setLayout(null);
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
label.setText("New JLabel");
label.setBounds(180, 10, 215, 35);
getContentPane().add(label);
txtareasay.setBounds(15, 55, 350, 185);
txtareasay.setEnabled(false);
getContentPane().add(txtareasay);
txtsay.setBounds(15, 275, 348, 20);
getContentPane().add(txtsay);
btsay.setText("发言");
btsay.addActionListener(new ActionListener() {//发言
public void actionPerformed(final ActionEvent e) {
String outmsg;
String mywords;
if (e.getSource() == btsay) {
try {
mywords = txtsay.getText();
if ((mywords.trim()).length() != 0) {
outmsg = myName+"&"+mywords;
out.println(outmsg);// 发送到服务器
out.flush();// 不能发送空消息也不能都发空格
//txtareasay.append(myName + ":" + mywords + "\n");
}
} catch (Exception ee) {
System.out.println(ee);
txtareasay.append("与服务器连接中断,请重新登陆!\n");
} finally {
// 清空输入区
txtsay.setText("");
}
}
}
});
btsay.setBounds(384, 273, 60, 20);
getContentPane().add(btsay);
}
public void run() {
String inmsg;
while (true) {
try {
inmsg = in.readLine();//获得从服务器发送来的对话信息
String []str=inmsg.split("&");
System.out.println(str.length);
if(str.length==2){
txtareasay.append(str[0]+" 说:"+str[1]+"\n");
}
} catch (Exception ee) {
System.out.println("Error at run()" + ee);
txtareasay.append("与服务器连接中断,请重新登陆ddddd!\n");
// 输出流,输入流设置为 null
in = null;
out = null;
return;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -