I'm trying to make a button that toggles the state of the button (changes color and text) when pressed. But I can't get the addActionListener to work and I can't find the ToggleState method from my main class. I am new to java and would appreciate your help.
I get the errors - “The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments” - "ActionListener cannot be resolved to a type"
package Grafiktest;
import javax.swing.*;
import java.awt.*;
public class coolgrafik extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JPanel panel;
private JLabel label;
public coolgrafik(){
//super(title);
setSize(400,60);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
panel.setBackground(Color.GRAY);
label = new JLabel("test");
panel.add(label);
MyButton button = new MyButton(Color.green, Color.red, "RUN","STOP");
panel.add(button);
button.addActionListener(this);
add(panel);
}
public static void main(String[] args){
new coolgrafik().setVisible(true);
//toggleState(Color.green, Color.red, "RUN","STOP");
}
public void actionPerformed(ActionEvent e) {
toggleState(Color.green, Color.red, "RUN","STOP");
}
}
and this is the button class
package Grafiktest;
import javax.swing.*;
import java.awt.*;
public class MyButton extends JButton{
private static final long serialVersionUID = 1L;
public MyButton(Color c1, Color c2, String s1, String s2){
setText(s1);
setForeground(c1);
setBackground(c1);
setOpaque(true);
}
public void toggleState(Color c1, Color c2, String s1, String s2){
setText(s2);
setForeground(c2);
setBackground(c2);
setOpaque(true);
}
}