我有两个类GamewampusGUI类。在wampusGUI类中,我在textarea方法下有一个名为displayTextAreatextarea1()
我正在尝试将append结果从textarea类转换为Game。但是当我试图从那个 class 访问时。 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();
            }
        });
    }

//这里是Game类的代码
      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);
     }

这将工作...

10-02 00:08
查看更多