我想创建图形控制台,它将为用户提供有关当前程序状态的信息。我打算使用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");
}
}
答案 0 :(得分:2)
如果您查找jTextArea
追加方法,您会看到:
Appends the given text to the end of the document
但是,如果String
为空或null
,则不会执行任何操作。
您可以在案例中使用setText()
。
答案 1 :(得分:1)
如果您致电getText()
,则返回新的字符串值?如果是这样,您可能需要在更改文本后在controller
和/或controller.Log
上致电repaint()和/或revalidate()。
答案 2 :(得分:1)
正如我所见,你永远不会初始化' ControlPanel',它总是为空,所以你不能对它做任何事情。