[英]Append() doesn't work on JTextArea element
我想创建图形控制台,它将向用户提供有关程序当前状态的信息。 我计划将其用于JTextArea,但我对append()方法有问题。 即使在主类中使用了它之后,我仍然有空的JTextArea。 我究竟做错了什么?
这是控制台的gui代码:
package com.meh;
import javax.swing.*;
public class Controller extends JFrame {
public JPanel ControlPanel;
public JTextArea Log;
static void setView() {
JFrame frame = new JFrame("Controller");
frame.setContentPane(new Controller().ControlPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
这是主类的代码:
package com.meh;
public class Main {
public static void main(String[] args) {
Controller controller = new Controller();
controller.setView();
controller.Log.append("Hello");
}
}
如果您查找jTextArea
append方法,您将看到:
Appends the given text to the end of the document
但是,如果String
为null
或null
,它将不执行任何操作。
您可以在这种情况下使用setText()
。
如果调用getText()
是否返回新的字符串值? 如果是这样,您可能需要在controller
和/或controller.Log
上调用repaint()和/或revalidate() 。更改文本后登录。
如我所见,您永远不会初始化'ControlPanel',它将始终为null,因此您无法对其进行任何操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.