return a value when an JButton actionperformed event is called

谁说我不能喝 提交于 2019-11-28 09:27:48

问题


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


来源:https://stackoverflow.com/questions/14909401/return-a-value-when-an-jbutton-actionperformed-event-is-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!