2

我对动作事件有一些问题JButton,我已经声明了一个全局变量 ( boolean tc1) 和一个JButton t1. 当我按下 JButton 时,我需要将布尔变量的值更改为“真”。谁能帮我吗?我的代码在这里。

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变量。
取出tc1constructor整体到可见性的声明class

  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

您不能更改匿名内部类(动作侦听器)中的局部方法变量的值。但是,您可以更改外部类的实例变量......所以您可以将 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 回答
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 回答