Java addActionListener(this)会产生错误

时间:2014-11-17 21:43:28

标签: java button compiler-errors

当我尝试按照我在网上找到的示例添加ActionListener时,它会显示一条错误消息,说明"不能在静态上下文中使用它。"有没有解决方法,或者我应该完全尝试不同的方法?

        //Variable Declaration 
    Drink drink = new Drink(); 

    //Create type1Button 
    JButton type1Button = new JButton (drink.typeArray[1]); 
    type1Button.setMnemonic(KeyEvent.VK_1);
    type1Button.setActionCommand("drink1");

    //Create type2Button 
    JButton type2Button = new JButton (drink.typeArray[2]); 
    type2Button.setMnemonic(KeyEvent.VK_2); 
    type2Button.setActionCommand("drink2"); 

    //Create type3Button 
    JButton type3Button = new JButton (drink.typeArray[3]); 
    type3Button.setMnemonic(KeyEvent.VK_3); 
    type3Button.setActionCommand("drink3");

    //Create type4Button 
    JButton type4Button = new JButton (drink.typeArray[4]); 
    type4Button.setMnemonic(KeyEvent.VK_4);
    type4Button.setActionCommand("drink4");

    //Create type5Button 
    JButton type5Button = new JButton (drink.typeArray[5]); 
    type5Button.setMnemonic(KeyEvent.VK_5); 
    type5Button.setActionCommand("drink5"); 

    //Create Action Listeners 
    type1Button.addActionListener(this); 
    type2Button.addActionListener(this); 
    type3Button.addActionListener(this); 
    type4Button.addActionListener(this); 
    type5Button.addActionListener(this); 

编辑:

    public final void createListeners (JButton type1Button, JButton type2Button, JButton type3Button, JButton type4Button, JButton type5Button) {
    //Create Action Listeners 
    type1Button.addActionListener((ActionListener) this); 
    type2Button.addActionListener((ActionListener) this); 
    type3Button.addActionListener((ActionListener) this); 
    type4Button.addActionListener((ActionListener) this); 
    type5Button.addActionListener((ActionListener) this); 
}

现在的问题是当我尝试在静态void main中使用它时,它抱怨我不能对非静态方法进行静态引用,但我怎么能运行它?

3 个答案:

答案 0 :(得分:0)

错误“不能在静态上下文中使用它”意味着您在this方法中引用了类的当前实例(即static)。静态方法绑定到类而不是实例,因此在这种情况下您不能引用this

您应该将您的方法更改为非static,并针对您班级的实例进行调用。

示例:

public class MyClass implements ActionListener {

   ...

   private JButton type1Button;
   private JButton type2Button;
   private JButton type3Button;
   private JButton type4Button;
   private JButton type5Button;

   public MyClass() {
      ..
      Drink drink = new Drink(); 

      //Create type1Button 
      type1Button = new JButton (drink.typeArray[1]); 
      type1Button.setMnemonic(KeyEvent.VK_1);
      type1Button.setActionCommand("drink1");

      //Create type2Button 
      type2Button = new JButton (drink.typeArray[2]); 
      type2Button.setMnemonic(KeyEvent.VK_2); 
      type2Button.setActionCommand("drink2"); 

      //Create type3Button 
      type3Button = new JButton (drink.typeArray[3]); 
      type3Button.setMnemonic(KeyEvent.VK_3); 
      type3Button.setActionCommand("drink3");

      //Create type4Button 
      type4Button = new JButton (drink.typeArray[4]); 
      type4Button.setMnemonic(KeyEvent.VK_4);
      type4Button.setActionCommand("drink4");

      //Create type5Button 
      type5Button = new JButton (drink.typeArray[5]); 
      type5Button.setMnemonic(KeyEvent.VK_5); 
      type5Button.setActionCommand("drink5"); 

      //Create Action Listeners 
      createListeners (type1Button, type2Button, type3Button, type4Button, type5Button);
   }

   ...
}

答案 1 :(得分:0)

使方法非静态。在静态环境中没有“this”,没有任何意义。你需要传递你想要使用的组件,代替“this”,或者你需要使封闭方法非静态。

欢呼声

答案 2 :(得分:0)

.addActionListener()方法期望接收的是实现ActionListener接口的类的实例。换句话说,它需要一些可以用来将事件传递给它们的东西。

但Java中的单词this仅在从类的方法或构造函数中使用时才引用类的实例。一个类可以有实例方法,但它也可以有static个方法;这些static方法并未附加到任何特定实例,而只是定义与该类相关的内容。例如,如果你有一个类Dog,你可能有.woof()的实例方法,这将使Dog的特定实例成为低音;但是你可能还有其他static方法,例如.listDoggyTreats(),它们会做一些与狗有关的事情,但不会与特定的狗绑在一起。

您正在做的是从.addActionListener(this)方法调用static。因此,当没有当前实例时,您试图告诉Java将您的班级的当前实例用作ActionListener,因为它从{{执行' 1}}方法。

要解决此问题,您需要以下两种方法之一:

  1. 不要将您的方法声明为static。然后,您还需要确保该类实现static接口。
  2. 使用单独ActionListener实施的单独实例来代替ActionListener
  3. 要么是合理的事情要做,不过如果你的方法是做这种事情(操纵按钮等),它可能表明你的代码中有问题{{1 }}。那就是你应该首先关注你的注意力。

相关问题