2

Game classと classの 2 つのクラスがありwampusGUIます。wampusGUIクラスには、method の下に名前がtextarea付けられたものがあります。クラスから結果を出そうとしています。しかし、そのクラスからアクセスしようとしているとき。正常に実行され、結果もそのクラスに入っています(単純 な方法でテストしただけです)が、に追加されていません。これが私のコードです。displayTextAreatextarea1()appendtextareaGamefunctionSystem.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);            
     }
4

3 に答える 3

7

コードでこの変更を行います

In Your First Class 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);            
     }

これは動作します...

于 2012-09-12T14:42:44.847 に答える
4

テキストを追加するにはTextArea

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

それはあなたを助けるかもしれません。

于 2012-09-12T10:47:03.583 に答える
2

invokeLater 内run()に wampusGUI のインスタンスを 1 つ作成し、メソッド内に wampusGUI のインスタンスを 1 つ作成していますtesting()

実際に行っているのは、wampusGUI のもう 1 つのインスタンスが表示されているため、(おそらく) 表示されないテキスト領域にテキストを追加することです。

于 2012-09-12T13:32:04.450 に答える