3

私のコード;

package com.test;

import java.awt.EventQueue;

public class TestGU {

    private JFrame frame;
    private JLabel la;

    /**
     * Launch the application.
     */
    public  void mainM() {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TestGU window = new TestGU();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void redefine(String text){
         la.setText(text);
    frame.repaint(); 

    }

    /**
     * Create the application.
     */
    public TestGU() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        la = new JLabel("New label");
        frame.getContentPane().add(null);
    }

}

以下に示すメイン メソッド (別のクラス) からラベルのテキストを変更しようとしています。

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {

        TestGU g = new TestGU();
        g.mainM();
        g.redefine("New Value");
}
}

1.) メイン メソッドが実行されると、ラベルに "New Value" というテキストが含まれているはずでしたが、まだテキストが含まれていますNew label。何も変わっていません。どうすれば修正できますか?

4

4 に答える 4

6

の 2 つのインスタンスを作成していて、表示されていないインスタンスのラベルの値をメソッドが変更しているようですTestGUredefine

今、私の理論を確認しています....

編集:
2 番目のインスタンスを作成する必要はありません。mainM メソッドは次のようになります。

public void mainM() {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

PS - 初期化メソッドに実際にこの行があると思いますか?

frame.getContentPane().add(la);

add(null)まったく機能しないというよりも。

于 2012-11-16T11:28:45.460 に答える
2

JFrame を最上位コンテナとして使用しないでください。

JFrame に追加されたコンポーネントは、AWTEvent キューによるリッスン用に登録されません。

したがって、 setText() は、再描画されるコンポーネントに適切なイベントを作成しません。

setContentPane( new JPanel(){{ add(la); }} );

また、ペイント/再ペイント メソッドを呼び出さずに、期待どおりに動作します。

于 2012-11-16T13:22:00.147 に答える
2

アイデアについては、この例をご覧ください。

TestGU

import java.awt.EventQueue;
import javax.swing.*;

public class AnotherClass {

   public static void main(String[] args) {

        Runnable r = new Runnable() {
            public void run() {
               TestGU g = new TestGU();
               g.redefine("New Value");
            }
        };
        EventQueue.invokeLater(r);
    }
}

class TestGU {

    private JFrame frame;
    private JLabel la;

    public void redefine(String text){
         la.setText(text);
    }

    /**
     * Create the application.
     */
    public TestGU() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        la = new JLabel("New label");
        frame.getContentPane().add(la);
        frame.pack();
        frame.setVisible(true);
   }
}
于 2012-11-16T11:38:37.773 に答える
1

TestGU(1インチTestと1mainM()インチ)の2つのインスタンスを作成しましたが、TestGU表示されたのは1つだけでした。mainM()メソッドを次のように変更します。

/**
 * Launch the application.
 */
public void mainM() {
    this.frame.setVisible(true);
}
于 2012-11-16T11:33:36.567 に答える