?? chatclient.java
字號:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
public class chatClient extends Frame implements ActionListener
{ Label label=new Label("聊天");
Panel panel=new Panel( );
TextField tf=new TextField(10);
TextArea ta=new TextArea( );
Socket client;
InputStream in;
OutputStream out;
public chatClient( )
{ super("客戶機");
setSize(250,250);
panel.add(label);
panel.add(tf);
tf.addActionListener(this);
add("North",panel);
add("Center",ta);
addWindowListener(new WindowAdapter( )
{ public void windowClosing(WindowEvent e)
{ System.exit(0);}} );
show( );
try
{ client=new Socket(InetAddress.getLocalHost( ),5000);
ta.append("已連接到服務器:"+client.getInetAddress( ).getHostName( )+"\n\n");
in=client.getInputStream( );
out=client.getOutputStream( );
}
catch(IOException ioe) { }
while(true)
{ try
{ byte[ ] buf=new byte[256];
in.read(buf);
String str=new String(buf);
ta.append("服務器說:"+str);
ta.append("\n");
}
catch(IOException e) { }
}
}
public void actionPerformed(ActionEvent e)
{ try
{ String str=tf.getText( );
byte[ ] buf=str.getBytes( );
tf.setText(null);
out.write(buf);
ta.append("我說:"+str);
ta.append("\n");
}
catch(IOException ioe) { }
}
public static void main(String args[ ])
{ new chatClient( );
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -