我怎么做这个编译?

时间:2011-03-29 12:01:27

标签: java

这是我的代码段:

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

class chatboxServer {
  JFrame fr;
  JPanel p;
  JTextArea ta;
  JButton send;

  chatboxServer() {
    fr=new JFrame("ChatBox_SERVER");
    p=new JPanel();
    ta=new JTextArea();
    ta.setRows(20);
    ta.setColumns(20);
    send=new JButton("send");
    fr.add(p);
    p.add(ta);
    p.add(send);
    fr.setVisible(true);
    fr.setSize(500,500);
    fr.setResizable(false);
    try {                                    // Making server listen to port number 3000
      ServerSocket s=new ServerSocket(3000);
    } catch(Exception exc) {
      JOptionPane.showMessageDialog(new JFrame(),"Cannot Listen :3000");
    }
    try {
      Socket s=null;
      s=ServerSocket.accept();
    } catch(Exception exc)
      JOptionPane.showMessageDialog(new JFrame(),"Accept Failed :3000");
    } 
  }
  public static void main(String args[]) {
    new chatboxServer();
  }
}

我得到的错误是:

  

非静态方法不能从静态上下文引用。

请解释我怎样才能编译?

6 个答案:

答案 0 :(得分:1)

我认为问题在于,s=ServerSocket.accept()行正在尝试在accept类上调用ServerSocket而不是在类的实例上调用ServerSocket s = new ServerSocket(3000); Socket sock = s.accept(); accept是一种实例方法,而不是静态方法。

您可能需要以下内容:

try/catch

可能只是将它们包装在一个ServerSocket serverSocket; Socket socket; try { serverSocket = new ServerSocket(3000); socket = serverSocket.accept(); } catch (Exception e) { JOptionPane.showMessageDialog(new JFrame(), "Cannot Listen and accept on port 3000"); } 中。例如

{{1}}

答案 1 :(得分:1)

试试这个:

import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class chatboxServer
{
    protected JTextArea ta = new JTextArea();;
    protected JButton send;
    protected JFrame fr = new JFrame("ChatBox_SERVER");
    protected JPanel p = new JPanel();

    chatboxServer()
    {
        ta.setRows(20);
        ta.setColumns(20);
        send = new JButton("send");
        fr.add(p);
        p.add(ta);
        p.add(send);
        fr.setVisible(true);
        fr.setSize(500, 500);
        fr.setResizable(false);
        ServerSocket s = null;

        try
        { // Making server listen to port number 3000
            s = new ServerSocket(3000);
        }
        catch (Exception excep)
        {
            JOptionPane.showMessageDialog(new JFrame(), "Cannot Listen :3000");
        }

        Socket sock = null;

        try
        {
            sock = s.accept();
        }
        catch (Exception ex)
        {
            Logger.getLogger(chatboxServer.class.getName()).log(Level.SEVERE, null, ex);
        }

        JOptionPane.showMessageDialog(new JFrame(), "Accept Failed :3000");
    }

    public static void main(String args[])
    {
        new chatboxServer();
    }
}

它仍然存在问题,比如使用端口3000,但至少它会编译。

答案 2 :(得分:0)

您需要指定chatboxServer()构造函数是静态的。

例如

private static chatboxServer() {

这可以解决您的问题。

答案 3 :(得分:0)

我认为有问题的一行是下面的一行;

s=ServerSocket.accept();

ServerSocket的accept方法不是静态方法,它是实例方法..应该修正如下行:

 ServerSocket s = null;
 Socket socket=null;
 try {                                    // Making server listen to port number 3000
      s=new ServerSocket(3000);
    } catch(IOException exc) {
      JOptionPane.showMessageDialog(fr,"Cannot Listen :3000");
      return; 
    }
    try {

      socket=s.accept();
    } catch(IOException exc)
      JOptionPane.showMessageDialog(fr,"Accept Failed :3000");
    } 

答案 4 :(得分:0)

你应该写这样的东西:

ServerSocket server;
Socket socket;
 try {              
  server =new ServerSocket(3000);
  socket = server.accept();
 } catch(Exception exc) {
    JOptionPane.showMessageDialog(new JFrame(),"Cannot Listen & accept on port :3000");
  }

答案 5 :(得分:0)

您正在一个块中为一个Server分配一个ServerSocket,并在下一个块中使用另一个名为s的ServerSocket,这是第一个问题。在第二个块中,您在ServerSocket类上调用accept而不是在实例上调用它...

应该更改以下代码(只是为了使其工作):

try { // Making server listen to port number 3000
      ServerSocket s=new ServerSocket(3000);
    } catch(Exception exc) {
      JOptionPane.showMessageDialog(new JFrame(),"Cannot Listen :3000");
    }
    try {
      Socket s=null;
      s=ServerSocket.accept();
    } catch(Exception exc)
      JOptionPane.showMessageDialog(new JFrame(),"Accept Failed :3000");
    } 

为:

 ServerSocket s;
try { // Making server listen to port number 3000
      s=new ServerSocket(3000);
    } catch(Exception exc) {
      JOptionPane.showMessageDialog(new JFrame(),"Cannot Listen :3000");
    }
    try {
      s.accept();
    } catch(Exception exc)
      JOptionPane.showMessageDialog(new JFrame(),"Accept Failed :3000");
    }