简体   繁体   English

JTextArea:如何将字符串从另一个类追加到JTextArea

[英]JTextArea: How to append string to JTextArea from another class

I have a simple GUI which has a JTextArea . 我有一个具有JTextArea的简单GUI。 I have created a method to get a message from the user and another to append text to the text area as follows 我创建了一种从用户处获取消息的方法,并提供了一种将文本追加到文本区域的方法,如下所示

Message m = new Message();

...

private void messageBtnActionPerformed(java.awt.event.ActionEvent evt) {   
     m.getMessage();
}

public void printOnTextArea(String text){
    jTextArea.append(text);
}

I am calling this method from my Message.java class like so 我像这样从我的Message.java类中调用此方法

SimpleGUI simpleGUI = new SimpleGUI():

...

public void getMessage(){
    String message = JOptionPane.showInputDialog("Please enter message");
    printMessage(message);
}

public void printMessage (String message){
    simpleGUI.printOnTextArea(message);
}

But even though the message entered to the JOptionPane is being passed to the printOnTextArea() correctly, it is not appearing on the JTextArea, does anybody see what i am doing wrong? 但是,即使输入到JOptionPane的消息已正确传递到printOnTextArea() ,它也没有出现在JTextArea上,有人看到我在做什么吗?

Ive found months ago this TextAreaWriter .. it could be helpful i think 我已经在几个月前找到了这个TextAreaWriter ..我认为这可能会有所帮助

http://pastebin.com/THGccgGu http://pastebin.com/THGccgGu

Its for SWT but should work with awt too 它适用于SWT,但也应该与awt一起使用

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

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