有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java addActionListener不工作

我遵循了一个关于如何做到这一点的教程-以下是我使用的代码:

package soundboard;
import javax.swing.*;
import java.awt.event.*;

public class Soundboard {

    JButton Button1;

    public void windowCreate() {
        JFrame frame = new JFrame();
        mainsPanel = new JPanel();

        Button1 = new JButton("1");
        Button1.addActionListener(this);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.add(Button1);
        frame.add(mainsPanel);

        frame.setSize(183,245);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
    }

    public void actionPerformed(ActionEvent event){
    }

    public static void main(String[] args){
    Soundboard window = new Soundboard();
    window.windowCreate();
    }
}

代码似乎不起作用。有人能解释一下原因吗

JPanel用作背景。问题在于Button1.addActionListener(this);,因为它说“this”不能转换为ActionListener或类似的东西


共 (4) 个答案

  1. # 1 楼答案

    我成功了。下面是我如何实现它的,其他按钮还没有完成一件事。 所有代码都在一个名为Soundboard的类中,该类实现了ActionListener,而javax。荡秋千 JAVAawt。事件*也被导入

    JButton loadButton;
    JButton clearButton;
    JButton Button1;
    JButton Button2;
    JButton Button3;
    JButton Button4;
    JPanel mainsPanel;
    int times;
    
    public void windowCreate() {
    
    
        JFrame frame = new JFrame();
        mainsPanel = new JPanel();
    
    
        loadButton = new JButton("Load...");
        loadButton.setSize(80, 30);
        loadButton.setLocation(4, 4);
    
        clearButton = new JButton("Clear");
        clearButton.setSize(80, 30);
        clearButton.setLocation(92, 4);
    
    
        Button1 = new JButton("1");
        Button1.setSize(80, 80);
        Button1.setLocation(4, 45);
        Button2 = new JButton("2");
        Button2.setSize(80, 80);
        Button2.setLocation(92, 45);
        Button3 = new JButton("3");
        Button3.setSize(80, 80);
        Button3.setLocation(4, 133);
        Button4 = new JButton("4");
        Button4.setSize(80, 80);
        Button4.setLocation(92, 133);
    
        loadButton.addActionListener(this);
    
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
    
        frame.add(loadButton);
        frame.add(clearButton);
        frame.add(Button1);
        frame.add(Button2);
        frame.add(Button3);
        frame.add(Button4);
        frame.add(mainsPanel);
    
        frame.setSize(183,245);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);                
    }
    @Override
    public void actionPerformed(ActionEvent event){
        times += 1;
        System.out.println("Test successful - this was the #"
                            + times + " press");
    }
    
    public static void main(String[] args){
        Soundboard window = new Soundboard();
        window.windowCreate();
    }
    
  2. # 2 楼答案

    如果要将类添加为Onclicklistener:

    Button1.addActionListener(this);
    

    然后类必须实现适当的接口ActionListener,如下所示:

    public class Soundboard implements ActionListener{
        //...
    
        @Override
        public void actionPerformed(ActionEvent e){
        //...
        }
    }
    

    编辑

    如果您有多个按钮,需要单独实现,您可以使用匿名类:

    mybutton.addActionListener(new ActionListener(){
    
        @Override
        public void actionPerformed(ActionEvent e){
             //does something, that probably interests only mybutton
             //declare mybutton as **final** if you must use it
        }
    });
    
  3. # 3 楼答案

    只能将ActionListener添加到带有addActionListener()Component

    你的类必须实现ActionListener,例如

    public class Soundboard implements ActionListener {
    
  4. # 4 楼答案

    如果要重写actionPerformed方法,需要实现ActionListener接口:

    public class Soundboard implements ActionListener {