附加另一个类的Textarea结果



我有两个类GamewampusGUI。在wampusGUI类中,我在方法textarea1()下有一个名为displayTextAreatextarea。我正在尝试从Game类将append结果textarea。但当我试图从该类访问时。function运行良好,结果也在该类中(我刚刚通过简单的System.out.print()方法进行了测试),但它没有附加到textarea。这是我的密码。

// Code of wampusGUI  class
public class wampusGUI extends javax.swing.JFrame {
    /**
     * Creates new form wampusGUI
     */
    public wampusGUI() {
        initComponents();
    }
    public void textArea1(String text) {
        System.out.print(text);
        displayTextArea.append(text); // this is not appending to textarea.
    }
           /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new wampusGUI().setVisible(true);
                   Game g = new Game();
                   g.testing();
            }
        });
    }

//这是游戏类的代码

      private wampusGUI gui;
      public void testing () {         
          String welCome=welcome();
          gui= new wampusGUI();
          gui.textArea1(welCome);            
     }

在代码中进行此更改

在您的一流wampusGUI 中

public class wampusGUI extends javax.swing.JFrame {
    /**
     * Creates new form wampusGUI
     */
    public wampusGUI() {
        initComponents();
    }
    public void textArea1(String text) {
        System.out.print(text);
        displayTextArea.append(text); // this is not appending to textarea.
    }
           /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                wampusGUI w=new wampusGUI();
                w.setVisible(true);
                Game g = new Game(w);
                g.testing();
            }
        });
    }

对于二等游戏

private wampusGUI gui;
//Add Contructor with Parameter
     public Game(wampusGUI w){
      //put this line of code at the end  
      gui=w;
     }
      public void testing () {         
          String welCome=welcome();          
          gui.textArea1(welCome);            
     }

这将起作用。。。

TextArea 中附加文本

String str = textarea.getText();
str+="appending text";
textarea.setText(str);

它可能会对你有所帮助。

您正在invokeLater的run()中创建一个wampusGUI实例,并在testing()方法中创建一个子实例。

实际上,您正在做的是将文本附加到您看不到的文本区域(可能),因为您有另一个wampusGUI实例可见。

最新更新