我在使用socket的java Chat应用程序上遇到了不同的错误

时间:2016-03-19 12:26:59

标签: java

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


public class ChatServer implements Runnable, ActionListener {

    private JFrame jfrm;
    private ServerSocket serverSocket;
    private Socket clientSocket;
    private ObjectOutputStream oos;
    private ObjectInputStream ois;
    private JTextArea jta;
    private JScrollPane jsp;
    private JTextField jtflInput;
    private JButton jbtnSend;

    public void ChatServer
    {
                    jfrm = new JFrame("Chat Server");
                    jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    jfrm.setLayout(new FlowLayout());
                    jfrm.setSize(300,320);
                    Thread myThread = new Thread(this);
                    myThread.start();
                    jta = new JTextArea(15,15);
                    jta.setEditable(false);
                    jta.setLineWrap(true);
                    jsp = new JScrollPane(jta);
                    jtflInput.addActionListener(this);
                    jbtnSend  = new JButton("Send");
                    jbtnSend.addActionListener(this);

                    jfrm.getContentPane().add(jsp);
                    jfrm.getContentPane().add(jtflInput);
                    jfrm.getContentPane().add(jbtnSend);
                    jfrm.setVisible(true);

                public void run()
                {
                    try
                    {
                        serverSocket = new ServerSocket(4444);
                        clientSocket = serverSocket.accept();
                        oos = new ObjectOutputStream(clientSocket.getOutputStream());
                        ois = new ObjectInputStream(clientSocket.getInputStream());
                        while(true)
                        {
                            Object input = ois.readObject();
                            jta.setText(jta.getText()+ "Client Says" + (String)input + "\n");

                        }
                    }
                    catch(IOException e)
                    {
                        e.printStackTrace();
                    }
                    catch(ClassNotFoundException e)
                    {
                        e.printStackTrace();
                    }
                }


                public void actionPerformed(ActionEvent ae)
                {
                    if(ae.getActionCommand().equals("Send") || ae.getSource() instanceof JTextField)
                    {
                        try
                        {
                            oos.writeObject(jtflInput.getText());
                            jta.setText(jta.getText() + "You say:" + jtflInput.getText() +"\n");
                        }
                        catch(IOException e)
                        {
                            e.printStackTrace();
                        }
                    }
                }
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        SwingUtilties.invokeLater(new Runnable(){

           public void run()
           {
                new ChatServer();
           }

        });
    }

}

我正在尝试使用教程中的套接字创建聊天应用程序。我得到了不同的编译时错误:

  1. public void run
  2. 的非法表达开始 预计在;
  3. public void run
  4. actionPerformed()
  5. 的非法表达开始 预计在;
  6. actionPerformed()

    任何帮助

1 个答案:

答案 0 :(得分:1)

定义构造函数的语法错误。

public void ChatServer()是定义方法的有效语法,但我想你应该根据代码的左边定义一个构造函数。

试试这个:

public ChatServer() // add () here and remove void
{
    jfrm = new JFrame("Chat Server");

    // snipped

    jfrm.setVisible(true);
} // add } here

而不是:

public void ChatServer
{
                jfrm = new JFrame("Chat Server");

                // snipped

                jfrm.setVisible(true);