找不到JavaFX符号,从类引用

时间:2013-08-26 10:11:42

标签: java class javafx symbols

这可能是一个愚蠢的问题,但我正试图通过控制器从我的新Gui访问文本区域,该控制器使用自己的类(就像它们一样简单)当我运行程序时它出现了符号而不是定义。我不确定我哪里出错了。

以下是代码:

外部课程

package dataget;

import java.io.*;

public class ReadWrite {

    public void ReadFileContents(String fileName) {
        try {
            BufferedReader in = new BufferedReader(new FileReader(fileName));
            int lineNumber = 0;
            int currentLine;
            String lines[];

            while (in.readLine() != null) {
                lineNumber++;
            }
            in.close();

            lines = new String[lineNumber];
            BufferedReader inLines = new BufferedReader(new FileReader(fileName));

            for (currentLine = 0; currentLine < lineNumber;) {
                lines[currentLine] = inLines.readLine();
                txtareaOutput.appendText(lines[currentLine]);
                currentLine++;
            }

            inLines.close();

            txtareaStatus.appendText("Lines in File: " + lineNumber);



        } catch (IOException ioe) {
            System.out.println("File not found!");
        }
    }

现在是控制器类:

public class MainController implements Initializable {

    @FXML
    private TextField txtName;
    @FXML
    private TextField txtAge;
    @FXML
    private ComboBox comboGender;
    @FXML
    private Button btnRead;
    @FXML
    private Button btnWrite;
    @FXML
    private TextArea txtareaStatus;
    @FXML
    private TextArea txtareaOutput;

    @FXML
    public void clickRead(){
        ReadWrite read = new ReadWrite();
        read.ReadFileContents("data/dylans/data.txt");
    }
}

我省略了几行,因为它们无关(它们都在同一个DataGet包中)

我不断收到“错误:无法找到符号”:

Compiling 2 source files to C:\Apps\NetBeans 7.3.1\Dylans Projects\DataGet\build\classes
C:\Apps\NetBeans 7.3.1\Dylans Projects\DataGet\src\dataget\ReadWrite.java:28:
error: cannot find symbol txtareaOutput.appendText(lines[currentLine]);
symbol: variable txtareaOutput
location: class ReadWrite
C:\Apps\NetBeans 7.3.1\Dylans Projects\DataGet\src\dataget\ReadWrite.java:34:
error: cannot find symbol txtareaStatus.appendText("Lines in File: " + lineNumber);
symbol: variable txtareaStatus
location: class ReadWrite 2 errors 

我是否必须在创建对象的原始类文件中声明textarea?

感谢。

1 个答案:

答案 0 :(得分:0)

您无法简单地访问MainController中的txtareaStatus字段ReadWrite。 如果你真的想这样做(这是一个坏主意),你必须:

  • 公开txtareaStatus,或提供公众吸气。
  • 实例化MainController并获取txtareaStatus

作为建议,您必须将获取文本的过程与更新UITextArea)的过程分开,请注意我在另一个线程中读取文件而不是{ {1}}以避免在大文本文件的情况下出现活动问题。

首先,您必须设计一个文件阅读器类,它将返回一个行列表(JavaFX Thread

String

在引用public class TextFileReader { @SuppressWarnings("NestedAssignment") public List<String> read(File file) { List<String> lines = new ArrayList<String>(); String line; try { BufferedReader br = new BufferedReader(new FileReader(file)); while ((line = br.readLine()) != null) { lines.add(line); } br.close(); } catch (IOException ex) { Logger.getLogger(TextFileReader.class.getName()).log(Level.SEVERE, null, ex); } return lines; } 的{​​{1}}控制器和更新它的FXML中:

TextArea

您可以访问此gist中的完整源代码。

enter image description here