给分一百!哥们我败家问个简单到无聊的问题!关于把一个程序改成applet的问题!
这是我从书上抄的,哪位大哥可以帮我看看能不能把下面的这个程序改成applet,把改好的源代码贴出来,让我们这些菜鸟学习一下!
//Client.java
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Client extends JFrame{
private JTextField tf;
private JTextArea display;
private ObjectOutputStream output;
private ObjectInputStream input;
private String message="";
private String chatServer;
private Socket client;
public Client(String host){
super("Client");
chatServer=host;
Container container=getContentPane();
tf=new JTextField();
tf.setEnabled(false);
tf.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
sendData(event.getActionCommand());
}
});
container.add(tf,BorderLayout.NORTH);
display=new JTextArea();
container.add(new JScrollPane(display),BorderLayout.CENTER);
setSize(300,150);
setVisible(true);
}
public void runClient(){
try{
connectToServer();
getStreams();
processConnection();
closeConnection();
}
catch(EOFException eofException){
System.out.println("Server terminated connection");
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
private void getStreams()throws IOException{
output=new ObjectOutputStream(client.getOutputStream());
output.flush();
input=new ObjectInputStream(client.getInputStream());
display.append("\nGot I/O streams\n");
}
private void connectToServer()throws IOException{
display.setText("Attempting connection\n");
client=new Socket(InetAddress.getByName(chatServer),5000);
display.append("Connected to: "+client.getInetAddress().getHostName());
}
private void processConnection()throws IOException{
tf.setEnabled(true);
do{
try{
message=(String)input.readObject();
display.append("\n"+message);
display.setCaretPosition(display.getText().length());
}
catch(ClassNotFoundException classNotFoundException){
display.append("\nUnknown object type received");
}
}
while(!message.equals("SERVER>>>TERMINATE"));
}
private void closeConnection()throws IOException{
display.append("\nClosing connection");
output.close();
input.close();
client.close();
}
private void sendData(String message){
try{
output.writeObject("CLIENT>>>"+message);
output.flush();
display.append("\nCLIENT>>>"+message);
}
catch(IOException ioException){
display.append("\nError writing object");
}
}
public static void main(String args[]){
Client application;
if(args.length==0)
application=new Client("127.0.0.1");
else
application=new Client(args[0]);
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.runClient();
}
}