JTextArea#39;s append () method doesn#39;t seem to work(JTextArea 的 append() 方法似乎不起作用)
问题描述
我们被指派创建一个简单的编译器作为家庭作业,它将接受一组指令(包含变量、条件、跳转等)并评估它们.这已经完成了,但我想我会让我的程序更……闪亮",并添加从文本文件加载指令的能力,只是为了用户舒适;然而,似乎 JTextArea
的 append ()
方法似乎并不真正喜欢我,因为它什么也没做.以下是相关代码:
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. 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 ();
}
}
(我使用 Awt 的 FileDialog 而不是 Swing 的 JFileChooser,因为它在 Mac 上看起来更好,如 苹果官方推荐.)
(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.)
此代码中使用的 input
变量指向 JTextArea 实例.有趣的是——文件读取部分必须完美无缺,因为我可以看到文件内容被写入标准输出,这要归功于 中的
循环.但是,System.out.println ()
调用whileJTextArea
中没有出现任何内容,我已经尝试了在 StackOverflow 上找到的所有现有解决方案——包括调用 repaint ()
、revalidate()
和 updateUI()
方法.
The input
variable used in this code points to the JTextArea instance. 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. 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.
我错过了什么?
推荐答案
代码可能是在事件处理循环中调用的,在该循环中您无法进行绘图.通常会使用
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 + "
");
}
}
不幸的是,在哪里放置invokeLatere(作为循环)需要小心.更好地使用@AndrewThompson 的解决方案.
Unfortunately it takes some care where to place the invokeLatere (as looping). Better use @AndrewThompson's solution.
这篇关于JTextArea 的 append() 方法似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JTextArea 的 append() 方法似乎不起作用


基础教程推荐
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01