没有封闭的类型实例可访问,我的代码中出错

时间:2015-01-09 11:26:09

标签: java class

我收到以下错误

  

无法访问类型的封闭实例

当我尝试添加此行时:

Receiver.this.getMyField()

尝试从另一个名为receiver的类中获取值。

  private void initSenderAndReceiver(final Socket clientSocket) {

    Receiver receiver = new Receiver(clientSocket, chatView);
    final Sender sender = new Sender(clientSocket, chatView);

    sendButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            sender.sendMessage(chatBox.getText());
            //sender.sendMessage(Receiver.this.Clientcoordinate1,Receiver.this.Clientcoordinate2);
            //sender.sendMessage();
            Receiver.this.getMyField();
            chatBox.setText(""); //Clear the chat box
        }
    });

    Thread receiverThread = new Thread(receiver);
    receiverThread.run();
}

1 个答案:

答案 0 :(得分:1)

尝试使用receiver.getMyField();final声明为sender对象。