Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java addActionListener(此)创建错误_Java_Button_Compiler Errors - Fatal编程技术网

Java addActionListener(此)创建错误

Java addActionListener(此)创建错误,java,button,compiler-errors,Java,Button,Compiler Errors,当我尝试按照我在网上找到的示例添加ActionListener时,它会显示一条错误消息,表示无法在静态上下文中使用它。有没有解决办法,或者我应该尝试完全不同的方法 //Variable Declaration Drink drink = new Drink(); //Create type1Button JButton type1Button = new JButton (drink.typeArray[1]); type1Button.

当我尝试按照我在网上找到的示例添加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中使用它时,它会抱怨我无法对非静态方法进行静态引用,但我还能如何运行它呢

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

您应该将方法更改为非静态,并针对类的实例调用它

例如:

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

   ...
}

使方法非静态。在静态上下文中不存在这种情况,这是没有意义的。您要么需要传入要使用的组件来代替它,要么需要使封闭方法成为非静态的

cheers

addActionListener方法希望收到的是实现ActionListener接口的类的实例。换句话说,它需要一些可以用来在事件发生时传递事件的东西

但是在Java中,this这个词仅指从类的方法或构造函数内部使用的类的实例。一个类可以有实例方法,但也可以有静态方法;这些静态方法不附加到任何特定实例,只定义与类一般相关的内容。因此,如果您有一个类Dog,例如,您可能有一个.woof的实例方法,这将使Dog的特定实例能够woof;但您也可能有其他静态方法,如.listDoggyTreats,它可以执行与狗相关但不与特定狗相关的操作

您所做的是从静态方法调用.addActionListenerthis。因此,当没有当前实例时,您试图告诉Java使用类的当前实例作为ActionListener,因为它是从静态方法执行的

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

不要将方法声明为静态。然后还需要确保该类实现ActionListener接口。 使用单独ActionListener实现的单独实例来传递,而不是此实例。
这两种方法都是合理的,但是如果您的方法正在执行这类操作,比如操作按钮等,那么如果是静态的,则可能表明代码中存在问题。这是你应该首先集中注意力的地方。

你能把整个代码都写进去吗谢谢你的帮助。问题是我试图在publicstaticvoidmain中创建侦听器。我现在用下面的代码创建了一个方法来实现这一点。请让我知道它是否会工作,因为我不能测试它,直到我添加按钮需要做什么。代码在编辑中有问题。@DJOwen好的,您可以在构造函数中调用该方法。