?? chatclient.java
字號:
import java.awt.*;
import java.awt.event.*;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.*;
public class ChatClient extends Frame{
TextField tfTxt = new TextField();
TextArea taContent = new TextArea();
Socket s = null;
DataOutputStream dos = null;
public static void main(String[] args) {
new ChatClient().launchFrame();
}
public void launchFrame(){
this.setLocation(150, 200);
this.setSize(300, 300);
this.add(tfTxt,BorderLayout.SOUTH);
this.add(taContent,BorderLayout.NORTH);
pack();
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
disconnect();
}
});
tfTxt.addActionListener(new TFListener());
this.setVisible(true);
connect();
}
private class TFListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String temp = tfTxt.getText();
//taContent.setText(temp);
tfTxt.setText("");
try{
dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF(temp);
dos.flush();
}catch (IOException e1){
e1.printStackTrace();
}
}
}
public void connect(){
try{
s = new Socket("127.0.0.1",5000);
System.out.println("connected!");
}catch(UnknownHostException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
public void disconnect(){
try{
if (dos !=null) dos.close();
if (s !=null) s.close();
}catch(IOException e)
{
e.printStackTrace();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -