return a value when an JButton actionperformed eve

2020-02-15 02:10发布

问题:

I have some problem with JButton action events, I have declared a global variable (boolean tc1) and a JButton t1. When I press the JButton I need to change the value of the boolean variable to 'true'. Can any one help me out? My code goes here.

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..


    }
}

Thanks.

回答1:

tc1 must be a global variable.
You can use a local variable in an another class defined inside the method, unless the local variable is a final variable.
Take out the declaration of tc1 from the constructor to the visibility of whole 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..


   }
}

I have also encountered this problem already and luckily I found the solution



回答2:

You cannot change the value of a local method variable in an anonymous inner class (the action listener). You can however change an instance variable of the outer class... so you could just move tc1 to 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;
            }
        });
    }
}


回答3:

First of all you said that i have declared a global variable(boolean tc1), here tc1 is not a global variable, if you want a global variable then you must declare that variable as static.

Then if you want to access that variable on button click event then you can write following code:

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);
    }
}


回答4:

Define click handler:

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

And then:

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) ;
      }
});