actionListener无法正常工作

时间:2014-09-16 13:13:02

标签: java swing jbutton actionlistener

我遵循了如何执行此操作的教程 - 这是我使用的代码:

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);,因为它说"这个"不能转换为ActionListener或类似的东西。

4 个答案:

答案 0 :(得分:4)

如果要覆盖ActionListener方法,则需要实现actionPerformed接口:

public class Soundboard implements ActionListener {

答案 1 :(得分:4)

如果您想将您的课程添加为Onclicklistener:

Button1.addActionListener(this);

那么你的类必须实现适当的接口ActionListener,如下所示:

public class Soundboard implements ActionListener{
    //...

    @Override
    public void actionPerformed(ActionEvent e){
    //...
    }
}

修改

如果你有多个按钮,需要分开实现,你可以f.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
    }
});

答案 2 :(得分:3)

您只能ActionListener添加Component addActionListener()

您的班级必须实施ActionListener例如

public class Soundboard implements ActionListener {

答案 3 :(得分:0)

我得到了它的工作。这是我实现它的方式,其他按钮还没有做到。 所有代码都在一个名为Soundboard的类中,它实现了ActionListener,而javax.swing *和 java.awt.event *也被导入。

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