Java Can';t从另一个类向JTextArea追加文本

Java Can';t从另一个类向JTextArea追加文本,java,swing,reference,jtextarea,Java,Swing,Reference,Jtextarea,在这里找到了一个类似的主题,但提供的解决方案不起作用(或者我只是没有看到)。以下是我的代码缩减到最低限度: 下面是我想从中调用print方法的类: { HumanInterface gui = new HumanInterface(); gui.init(); gui.printToArea("from Main Class"); } 这是HumanInterface类,它扩展了JFrame { 当我从HumanInterface类之外的任何地方调用printToAre

在这里找到了一个类似的主题,但提供的解决方案不起作用(或者我只是没有看到)。以下是我的代码缩减到最低限度:

下面是我想从中调用print方法的类:

{
    HumanInterface gui = new HumanInterface();
    gui.init();
    gui.printToArea("from Main Class");
}
这是HumanInterface类,它扩展了JFrame {


当我从HumanInterface类之外的任何地方调用printToArea(…)时,它都不起作用。我缺少什么?

您看到的GUI不是您“想要”看到的GUI,只是第二个;-)

init()
函数中,创建第二个
HumanInterface

HumanInterface gst = new HumanInterface();
gst.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gst.pack();
gst.setVisible(true);
我认为您可能想要(未经测试):


为什么要将
createArea()
中的JTextArea设置为final?此外,在
printToArea()
中,什么是
updateArea(区域,字符串)
函数??您有
updateTextArea(区域,字符串)
definedI我正要指出…你需要在printToArea方法中调用UpdateExtrade函数,而不是你现在使用的不存在的updateArea。在那次更改后仍然不工作?甚至没有注意到。但是你的答案是正确的!非常感谢,这让我发疯了。我真傻!
HumanInterface gst = new HumanInterface();
gst.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gst.pack();
gst.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);