简单改了一下,写了一个main方法,其中用了JFrame,在放到页面中时应去掉此方法.否则javax.swing找不到.
服务器不用applet,就不用改了.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;
import java.net.*;public class AppletClient extends Applet {
    Label label=new Label("交谈");
    Panel panel=new Panel();
    TextField tf=new TextField(10);
    TextArea ta=new TextArea();
    Socket client;
    InputStream in;
    OutputStream out;    boolean isStandalone = false;
    //Get a parameter value
    public String getParameter(String key, String def) {
        return isStandalone ? System.getProperty(key, def) :
            (getParameter(key) != null ? getParameter(key) : def);
    }    //Construct the applet
    public AppletClient() {
    }
    //Initialize the applet
    public void init() {
        try {
            jbInit();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
    //Component initialization
    private void jbInit() throws Exception {
        this.setLayout(new BorderLayout());
        this.setSize(new Dimension(250,250));
        tf.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
                tf_actionPerformed(e);
            }
        });
        panel.add(label);
        panel.add(tf);
        this.add(panel,BorderLayout.NORTH);
        this.add(ta,BorderLayout.CENTER);
        show();
        try{
        client=new Socket(InetAddress.getLocalHost(),4000);
        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){}
        }    }
    //Get Applet information
    public String getAppletInfo() {
        return "Applet Information";
    }
    //Get parameter info
    public String[][] getParameterInfo() {
        return null;
    }    //static initializer for setting look & feel
    static {
        try {
            //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            //UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
        }
        catch(Exception e) {
        }
    }    void tf_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){
        javax.swing.JFrame frame = new javax.swing.JFrame("¿Í»§»ú");
        AppletClient ac = new AppletClient();
        frame.setContentPane(ac);
        frame.setSize(ac.getSize());
        frame.pack();
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        frame.setVisible(true);
        ac.init();
    }
}

解决方案 »

  1.   

    我的服务器程序在这里!import java.io.*;
    import java.net.*;
    import java.awt.*;
    import java.awt.event.*;class Server extends Frame implements ActionListener{
    Label label=new Label("交谈");
    Panel panel=new Panel();
    TextField tf=new TextField(10);
    TextArea ta=new TextArea();
    ServerSocket server;
    Socket client;
    InputStream in;
    OutputStream out;public Server(){
    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{
    server=new ServerSocket(4000);
    client=server.accept();
    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 Server();
    }
    }
     
      

  2.   

    Server改好了,main方法中用了JFrame,在放到页面中时应去掉此方法.否则javax.swing找不到.import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    import java.io.*;
    import java.net.*;
    /**
     * <p>Title: </p>
     * <p>Description: </p>
     * <p>Copyright: Copyright (c) 2002</p>
     * <p>Company: </p>
     * @author unascribed
     * @version 1.0
     */public class AppletServer extends Applet {
        boolean isStandalone = false;
        Label label=new Label("交谈");
        Panel panel=new Panel();
        TextField tf=new TextField(10);
        TextArea ta=new TextArea();
        ServerSocket server;
        Socket client;
        InputStream in;
        OutputStream out;    //Get a parameter value
        public String getParameter(String key, String def) {
            return isStandalone ? System.getProperty(key, def) :
                (getParameter(key) != null ? getParameter(key) : def);
        }    //Construct the applet
        public AppletServer() {
        }
        //Initialize the applet
        public void init() {
            try {
                jbInit();
            }
            catch(Exception e) {
                e.printStackTrace();
            }
        }
        //Component initialization
        private void jbInit() throws Exception {
            setSize(250,250);
            this.setLayout(new BorderLayout());
            tf.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    tf_actionPerformed(e);
                }
            });
            panel.add(label);
            panel.add(tf);
            add("North",panel);
            add("Center",ta);        try{
                server=new ServerSocket(4000);
                client=server.accept();
                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){}
            }
        }    //Get Applet information
        public String getAppletInfo() {
            return "Applet Information";
        }
        //Get parameter info
        public String[][] getParameterInfo() {
            return null;
        }    void tf_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){
            javax.swing.JFrame frame = new javax.swing.JFrame("服务器");
            AppletServer as = new AppletServer();
            frame.setContentPane(as);
            frame.setSize(as.getSize());
            frame.pack();
            frame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
            frame.setVisible(true);
            as.init();
        }
    }