如何从内部 class 中的 try 块中获取字符串

How to get a string from a try block in an inner class

我试图在实现 ActionListener 的内部 class 中将标签设置为 reader 接收的字符串。 但是我得到一个未知的符号错误。 我如何在内部 class

中引用该变量
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
public class DailyAdviceClient {
  JLabel label;
  JButton button;
  JFrame frame;
  JPanel mainPanel;

  public void gui() {
    frame = new JFrame("Daily Advice Client");
    button = new JButton("Get Advice");
    label = new JLabel("Text");
    mainPanel = new JPanel();
    mainPanel.add(button);
    mainPanel.add(label);

    button.addActionListener(new AdviceListener());
    frame.getContentPane().add(BorderLayout.CENTER, mainPanel);

    frame.setSize(300,400);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

  }
  public void go() {
    try {
      Socket s = new Socket("127.0.0.1", 4242);

      InputStreamReader streamReader = new InputStreamReader(s.getInputStream());
      BufferedReader reader = new BufferedReader(streamReader);

      String advice = reader.readLine();
      System.out.println("Today you should: " + advice);
      reader.close();
    }
    catch (IOException e) {
      System.out.println("Hayt tumhari ki");
      System.out.print("I didn't think we would get to this stage. Please ");
      System.out.println("kill the developer. \n");
      e.printStackTrace();
    }
  }

  public static void main(String[] args) {
    DailyAdviceClient client = new DailyAdviceClient();
    client.go();
    client.gui();
  }

  public class AdviceListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      label.setText(advice);
    }
  }
}

将您的字符串 advice 声明为 instance/field 变量 :

import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
public class DailyAdviceClient {
  JLabel label;
  JButton button;
  JFrame frame;
  JPanel mainPanel;
  String advice;

  public void gui() {
    frame = new JFrame("Daily Advice Client");
    button = new JButton("Get Advice");
    label = new JLabel("Text");
    mainPanel = new JPanel();
    mainPanel.add(button);
    mainPanel.add(label);

    button.addActionListener(new AdviceListener());
    frame.getContentPane().add(BorderLayout.CENTER, mainPanel);

    frame.setSize(300,400);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

  }
  public void go() {
    try {
      Socket s = new Socket("127.0.0.1", 4242);

      InputStreamReader streamReader = new InputStreamReader(s.getInputStream());
      BufferedReader reader = new BufferedReader(streamReader);

      advice = reader.readLine();
      System.out.println("Today you should: " + advice);
      reader.close();
    }
    catch (IOException e) {
      System.out.println("Hayt tumhari ki");
      System.out.print("I didn't think we would get to this stage. Please ");
      System.out.println("kill the developer. \n");
      e.printStackTrace();
    }
  }

  public static void main(String[] args) {
    DailyAdviceClient client = new DailyAdviceClient();
    client.go();
    client.gui();
  }

  public class AdviceListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      label.setText(advice);
    }
  }
}

这可以通过应用 MVC 模式轻松解决:

你只有一个 Controller 和一个 View,但是你缺少一个通用的 Model 他们都在努力。

最简单的实现是将局部变量advice提升为对象成员变量作为模型 :

public class DailyAdviceClient {
  private String advice;
  // ...
  advice = reader.readLine();
  // ...

并将 class AdviceListener 转换为 匿名内部 class 作为 lambda :

button.addActionListener(ae->label.setText(advice));