简体   繁体   English

JTextArea 的 append() 方法似乎不起作用

[英]JTextArea's append () method doesn't seem to work

We were assigned to create a simple compiler as a homework that will take set of instructions (containing variables, conditions, jumps, etc.) and evaluate them.我们被分配创建一个简单的编译器作为作业,它将接受一组指令(包含变量、条件、跳转等)并评估它们。 That's already done, but I thought I'd make my program little bit more… “shiny”, and add the ability to load instructions from a text file, just for the sake of user comfort;这已经完成了,但我想我应该让我的程序更......“闪亮”,并添加从文本文件加载指令的能力,只是为了用户舒适; however, it seems that the JTextArea 's append () method doesn't seem to really like me, as it does exactly nothing.然而,似乎JTextAreaappend ()方法似乎并不真正喜欢我,因为它什么都不做。 Here's the relevant code:这是相关的代码:

BufferedReader bufferedReader;
File file;
FileDialog fileDialog = new FileDialog (new Frame (), "Open File", FileDialog.LOAD);
String line;

fileDialog.setVisible (true);

if (fileDialog.getFile () != null) {
    file = new File (fileDialog.getDirectory () + fileDialog.getFile ());
    input.setText (""); // delete old first

    try {
        bufferedReader = new BufferedReader (new FileReader (file));
        line = bufferedReader.readLine ();

        while (line != null) {
            input.append (line);
            System.out.println (line);
            line = bufferedReader.readLine ();
        }
    } catch (IOException ioe) {
        ioe.printStackTrace ();
    }
}

(I'm using Awt's FileDialog instead of Swing's JFileChooser because it simply looks better on Mac, as seen in Apple's official recommendation .) (我使用 Awt 的 FileDialog 而不是 Swing 的 JFileChooser 因为它在 Mac 上看起来更好,正如Apple 的官方推荐中所见。)

The input variable used in this code points to the JTextArea instance.此代码中使用的input变量指向 JTextArea 实例。 The funny thing is – the file reading part must be working flawlessly, as I can see the file content being written to the standard output thanks to the System.out.println () call within the while loop.有趣的是 - 文件读取部分必须完美无缺,因为我可以看到文件内容被写入标准输出,这要归功于while循环中的System.out.println ()调用。 However, nothing appears in the JTextArea , and I've tried all the existing solutions I've found here on StackOverflow – that includes calling the repaint () , revalidate () and updateUI () methods.但是, JTextArea没有出现任何JTextArea ,我已经尝试了我在 StackOverflow 上找到的所有现有解决方案——包括调用repaint ()revalidate ()updateUI ()方法。

What am I missing?我错过了什么?

The code probably is called on the event handling loop, where you cannot have drawing.该代码可能是在事件处理循环中调用的,在该循环中您不能进行绘图。 One would normally use一个通常会使用

final String line = bufferedReader.relineadLine();
// final+local var so usable in Runnable.

SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {
        input.append(line + "\n");
    }
} 

Unfortunately it takes some care where to place the invokeLatere (as looping).不幸的是,它需要小心放置 invokeLatere(作为循环)。 Better use @AndrewThompson's solution.更好地使用@AndrewThompson 的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM