?? testclient.java
字號:
package talk;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class TestClient extends JFrame implements ActionListener {
DataInputStream dis;
DataOutputStream dos;
JTextField tf;
JTextArea ta;
String s11, s22;
// 聊天程序客戶端主窗口用戶初始界面。
public TestClient(String s1, String s2) {
this.setTitle("聊天程序客戶端");
JScrollPane jp = new JScrollPane();
ta = new JTextArea(10, 10);
Panel p = new Panel();
tf = new JTextField(20);
JButton b = new JButton("發送");
b.addActionListener(this);
tf.addActionListener(this);
p.add(tf);
p.add(b);
jp.setViewportView(ta);
this.getContentPane().add(jp);
this.getContentPane().add("South", p);
this.setSize(350, 250);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.s11 = s1; // 接受用戶昵稱信息
this.s22 = s2; // 接受服務器地址信息
this.setVisible(true);
tf.requestFocus();
this.connect();
this.createReadThread();
}
public void connect() {
try {
Socket s2 = new Socket(s22, 911);
InputStream is = s2.getInputStream();
dis = new DataInputStream(is);
OutputStream os = s2.getOutputStream();
dos = new DataOutputStream(os);
} catch (IOException e) {
System.out.println("連接服務器故障!");
}
}
public void createReadThread() {
ReadThread rt = new ReadThread(this.ta, this.dis);
rt.start();
}
public void actionPerformed(ActionEvent e) {
try {
String s = tf.getText();
dos.writeUTF(s11 + "說: " + s);
ta.append("自己說:" + s);
ta.append("\n");
tf.setText("");
tf.requestFocus();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -