Reputation: 3
I followed a tutorial on how to do this - Here's the code I used:
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();
}
}
The code seems to not be working. Could anyone explain why?
The JPanel
is used as a background. The problem is in Button1.addActionListener(this);
, as it says that "this" is not convertible to ActionListener
or something like so.
Upvotes: 0
Views: 4669
Reputation: 3
I got it working. Here's how I implemented it, the other buttons don't do a thing quite yet. All the code is in a class called Soundboard, which implements ActionListener, while javax.swing* and java.awt.event* are also imported.
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();
}
Upvotes: 0
Reputation: 1890
If you want to add your class as an Onclicklistener:
Button1.addActionListener(this);
then your class must implement the appropriate interface ActionListener
like this:
public class Soundboard implements ActionListener{
//...
@Override
public void actionPerformed(ActionEvent e){
//...
}
}
EDIT
If you have multiple buttons, that need separated implementation, you could f.e. use anonymous classes:
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
}
});
Upvotes: 4
Reputation: 9954
You can only add ActionListener
s to a Component
with addActionListener()
.
Your class has to implement ActionListener
e.g.
public class Soundboard implements ActionListener {
Upvotes: 3
Reputation: 2920
You need to implement the ActionListener
interface if you want to override the actionPerformed
method:
public class Soundboard implements ActionListener {
Upvotes: 4