2017-04-10 71 views
1

我正在创建WindowBuilder GUI,并且需要将使用单选按钮创建的变量传递给EventHandler类以用于进一步处理。单选按钮事件的输出成功;但是,在EventHanler类中未解析actionPerformed方法中声明的变量“df”。任何帮助将不胜感激。“public void actionPerformed(ActionEvent event)”中声明的变量无法传递

public TestClass() { 

    /* INSERT RADIOBUTTON INTO FRAME. */ 
    JRadioButton rdbtnNo = new JRadioButton("No"); 
    rdbtnNo.setFont(new Font("Tahoma", Font.BOLD, 12)); 
    rdbtnNo.setBounds(332, 509, 63, 23); 
    frame.getContentPane().add(rdbtnNo); 

    /* LISTEN FOR RADIOBUTTON BUTTON. */ 
    rdbtnNo.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent event) { 
      String command = event.getActionCommand(); 
      System.out.println(command); 
      int df = 20;      
     }   
    }); 

    rdbtn.setActionCommand("event"); 
    rdbtn.addActionListener(new EventHandler()); 

} 

public class EventHandler implements ActionListener { 
    public void actionPerformed(ActionEvent event) { 

     System.out.println(df); 
    } 
}        
+0

TestClass()方法返回什么? – Omore

+0

阅读关于变量范围:http://www.java-made-easy.com/variable-scope.html – ControlAltDel

回答

-1

首先是摆脱匿名内部类和使用lambdas代替。它使你的代码更容易理解。

public TestClass() { 

    /* INSERT RADIOBUTTON INTO FRAME. */ 
    JRadioButton rdbtnNo = new JRadioButton("No"); 
    rdbtnNo.setFont(new Font("Tahoma", Font.BOLD, 12)); 
    rdbtnNo.setBounds(332, 509, 63, 23); 
    frame.getContentPane().add(rdbtnNo); 

    /* LISTEN FOR RADIOBUTTON BUTTON. */ 
    rdbtnNo.addActionListener(event -> pressedTheButton(event)); 

    rdbtn.setActionCommand("event"); 
    rdbtn.addActionListener(new EventHandler()); 

} 

public void pressedTheButton(ActionEvent event) { 
    String command = event.getActionCommand(); 
    System.out.println(command); 
    int df = 20; 
    printStuff(df); 
} 

public void printStuff(int input) { 
    System.out.println(input); 
} 



      ///DELETE THIS. This is unneeded, use Java 8 stuff, it's awesome//// 
public class EventHandler implements ActionListener { 
    public void actionPerformed(ActionEvent event) { 

     System.out.println(df); 
    } 
}  
0
rdbtnNo.addActionListener(new EventHandler()); 
在你的代码

。 您也有其他错误,但是当您在下面声明了内部类时,您必须在addActionListener方法中实例化它以利用它。

0

它不能在其它类来解决,因为该变量的范围是仅在该方法:

public void actionPerformed(ActionEvent event) { 
       String command = event.getActionCommand(); 
       System.out.println(command); 
       int df = 20;      
      } 
+0

你想添加2个eventListeners吗? – strash

0

变量“DF”,这是在actionPerformed方法宣布没有解决在EventHanler类中。

这是因为variable scope。在你的例子中,你声明df作为你传递给addActionListener(ActionListener)的匿名内部类的actionPerformed(ActionEvent)方法内的局部变量。局部变量只能在其创建的代码块中访问。这意味着您的df变量不能在除actionPerformed(ActionEvent)方法以外的任何其他位置访问。

解决此问题的第一步是在Test类中使df为实例变量,以便可以在actionPerformed(ActionEvent)方法内部和外部访问它。

从这里有两种可能的方法:

  1. 使用第二个匿名内部类的两个按钮

    public class Test { 
    
        private int df; 
    
        public Test() { 
         // ... 
         final JButton button = new JButton("Click Me"); 
         button.addActionListener(new ActionListener() { 
    
          @Override 
          public void actionPerformed(ActionEvent e) { 
           df = 20; 
          } 
    
         }); 
         final JButton button2 = new JButton("Click Me Again"); 
         button2.addActionListener(new ActionListener() { 
    
          @Override 
          public void actionPerformed(ActionEvent e) { 
           if(df == 20) { // got instance 
            // TODO do a thing 
           } 
          } 
    
         }); 
         // ... 
        } 
    
    } 
    
  2. 通行证dfEventHandler

    public class Test { 
    
        private int df; 
    
        public Test() { 
         // ... button1 ... 
         final JButton button2 = new JButton("Click Me Again"); 
         button2.addActionListener(new EventHandler()); 
         // ... 
        } 
    
    } 
    
    // different file 
    public class EventHandler implements ActionListener { 
    
        private int df; 
    
        public EventHandler(int df) { 
         this.df = df; // got instance 
        } 
    
        @Override 
        public void actionPerformed(ActionEvent e) { 
         if(df == 20) { 
          // TODO do a thing 
         } 
        } 
    } 
    
构造

边注意:不要使用null布局!请参阅Why is it frowned upon to use a null layout in Swing?