2012年6月6日 星期三

Intermediate Java(51) - Setting Up the Client for Chatting


import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Client extends JFrame{
private JTextField userText;
private JTextArea chatwindow;
private ObjectOutputStream output;
private ObjectInputStream input;
private String message = "";
private String serverIP;
private Socket connection;
//constructor
public Client(String host){
super("Client mofo!");
serverIP = host;
userText = new JTextField();
userText.setEditable(false);
userText.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
sendData(event.getActionCommand());
userText.setText("");
}
}
);
add(userText, BorderLayout.NORTH);
chatwindow = new JTextArea();
add(new JScrollPane(chatwindow), BorderLayout.CENTER);
setSize(400,200);
setVisible(true);
}

//連接到服務器(Server)
public void startRunning(){
try{
connectToServer();
setupStreams();
whileChatting();
}catch(EOFException eofException){
showMessage("\n Client terminated connection");
}catch(IOException ioException){
ioException.printStackTrace();
}finally{
closeCrap();
}
}
}


在這個class中的所有的意義,比如Server一樣

沒有留言:

張貼留言