2

アクションイベントに問題があります。JButtonグローバル変数(boolean tc1)とを宣言しましたJButton t1。JButtonを押すとき、ブール変数の値を「true」に変更する必要があります。誰かが私を助けることができますか?私のコードはここにあります。

class Tracker extends JPanel {
    public static void main(String[] args) {
        new Tracker();    
   }

    public Tracker() {

        JButton tr=new JButton("TRACKER APPLET");
        JButton rf=new JButton("REFRESH");

        boolean tc1=false,tc2=false,tc3=false,tc4=false;
        JButton t1=new JButton(" ");

        t1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                tc1=true;
            }
        });
        System.out.println(tc1);
        //remaining part of the code...
        // here i need to use the value of tc1 for further process..


    }
}

ありがとう。

4

4 に答える 4

2

tc1インスタンス変数でなければなりません。
ローカル変数が変数でない限り、メソッド内で定義された別のクラスでローカル変数を使用できfinalます。全体の可視性への
宣言を取り出しますtc1constructorclass

  class Tracker extends JPanel {
  boolean tc1=false,tc2=false,tc3=false,tc4=false;
  public static void main(String[] args) {
    new Tracker();    
  }

public Tracker() {

    JButton tr=new JButton("TRACKER APPLET");
    JButton rf=new JButton("REFRESH");

    
    JButton t1=new JButton(" ");

    t1.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            tc1=true;
        }
    });
    System.out.println(tc1);
    //remaining part of the code...
    // here i need to use the value of tc1 for further process..


   }
}

私もすでにこの問題に遭遇しており、幸運にも解決策を見つけました

于 2013-02-16T11:20:49.980 に答える
1

クリック ハンドラーを定義します。

public void onClick(Boolean tc1){
  System.out.println(tc1) ;
  //Write some logic here and use your updated variable
}

その後:

public Tracker()
{

JButton tr=new JButton("TRACKER APPLET");
JButton rf=new JButton("REFRESH");

boolean tc1=false,tc2=false,tc3=false,tc4=false;
JButton t1=new JButton(" ");

     t1.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e){
          tc1 = true ;
          onClick(tc1) ;
      }
});
于 2013-02-16T10:48:51.770 に答える
1

匿名内部クラス (アクション リスナー) のローカル メソッド変数の値を変更することはできません。ただし、外部クラスのインスタンス変数を変更することはできます...そのため、tc1 を Tracker に移動するだけです。

class Tracker extends JPanel {
    public static void main(String[] args) {
        new Tracker();
    }

    private boolean tc1; // <=== class level instead...

    public Tracker() {

        JButton t1 = new JButton(" ");

        t1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                tc1 = true;
            }
        });
    }
}
于 2013-02-16T11:00:01.980 に答える
1

まず第一にi have declared a global variable(boolean tc1)、ここで tc1 はグローバル変数ではありません。グローバル変数が必要な場合は、その変数を として宣言する必要がありますstatic

次に、その変数にアクセスしたい場合button click eventは、次のコードを記述できます。

class Tracker extends JPanel
{
    boolean tc1=false,tc2=false,tc3=false,tc4=false;
    public static void main(String[] args) {
        new Tracker();    
    }
    public Tracker()
    {
        JButton tr=new JButton("TRACKER APPLET");
        JButton rf=new JButton("REFRESH");

        JButton t1=new JButton(" ");

        t1.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            tc1=true;
            getValue();
        }
        });
    }
    public void getValue()
    {
        System.out.println("values is: "+ tc1);
    }
}
于 2013-02-16T11:00:19.190 に答える