【问题标题】:return a value when an JButton actionperformed event is called调用 JButton actionperformed 事件时返回一个值
【发布时间】:2013-02-01 07:05:53
【问题描述】:

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


    }
}

谢谢。

【问题讨论】:

  • 你的代码有什么问题?什么不工作?

标签: java swing jbutton actionevent


【解决方案1】:

tc1 必须是实例变量。
您可以在方法内定义的另一个类中使用局部变量,除非局部变量是final 变量。
constructor中取出tc1的声明到整个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..


   }
}

我也遇到过这个问题,幸运的是我找到了解决办法

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      首先你说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);
          }
      }
      

      【讨论】:

        【解决方案4】:

        定义点击处理程序:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-03-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-02-13
          相关资源
          最近更新 更多