publicclassClientFrameextendsJFrame{/*..*/staticSocketsocket;staticObjectOutputStreamout;staticObjectInputStreamin;staticMessagemessage;publicClientFrame(){this.initNet()...
public class ClientFrame extends JFrame{
/*..*/
static Socket socket;
static ObjectOutputStream out;
static ObjectInputStream in;
static Message message;
public ClientFrame(){
this.initNet();
//文本输入发送
inputJButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
message=new Message(inputJTextArea.getText());
System.out.println("发送"+message.getMessage());
try {
out.writeObject(message);
out.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
inputJTextArea.setText("");
}
});
}
//建立连接
public void initNet(){
try {
socket = new Socket("localhost", 12345);
in= new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
out = new ObjectOutputStream(socket.getOutputStream());
} catch (Exception e) {
// TODO: handle exception
}
}
public static void main(String[] args){
ClientFrame frame=new ClientFrame();
//frame的一些设置
try {
Object obj = in.readObject();
if (obj != null) {
message = (Message)obj;
System.out.println("接收"+message.getMessage());
}
}
展开