Reputation: 65
I have been working on this project basically all day, and, something isn't working. Basically what I am trying to do is create a button that will, when clicked, make a colored circle appear. However, no matter how I declare the ActionListener
, it continually gives me the error that it is not abstract. My coding is below
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class button extends JPanel
{
public button()
{
JButton change;
Font font1 = new Font("sansserif", Font.PLAIN, 10);
setBackground(Color.black);
change = new JButton("Change Light");
change.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event, Graphics page)
{
page.setColor(Color.green);
page.fillOval(20, 220, 60, 60);
}
});
change.setPreferredSize(new Dimension(100, 30));
change.setFont(font1);
add(change, BorderLayout.SOUTH);
change.setVisible(true);
}
}
The error that I get when trying to compile this is:
"error: is not abstract and does not override abstract method
actionPerformed(ActionEvent)
inActionListener
change.addActionListener(new ActionListener(){"
I always end up with this result when I follow examples online, so, I know that I must be really close- but I have been doing this for hours and can use some help. Does anyone know what my error is?
A little note is that if I try to make anything abstract, I get a million MORE errors same as if I say the class as a whole implements ActionListener
Upvotes: 0
Views: 108
Reputation: 550
Have a look at this answer which draws a circle based on the state of a JToggleButton
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.SwingUtilities;
public class TestGUI {
JToggleButton draw = new JToggleButton("Draw");
public TestGUI() {
final JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
final CirclePanel panel = new CirclePanel();
draw.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
panel.setDraw(draw.isSelected());
frame.repaint();
}
});
frame.add(draw, BorderLayout.NORTH);
frame.add(panel, BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestGUI();
}
});
}
private class CirclePanel extends JPanel {
private boolean draw = false;
public void setDraw(boolean toDraw) {
draw = toDraw;
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
@Override
protected void paintComponent(Graphics g) {
if (draw) {
g.drawOval(200, 200, 50, 50);
}
}
}
}
Upvotes: 1
Reputation: 347284
ActionListener
does not define a method public void actionPerformed(ActionEvent event, Graphics page)
See the ActionListener
JavaDocs, How to Write an ActionListener
and How to Use Buttons, Check Boxes, and Radio Buttons for more details
For example
change.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent event) {...
Updated
Pass the reference of Graphics
which need to the constructor of Button
. Because of the way that anonymous classes reference variables, you will either need to make the parameter final
and reference it directly, or, create an instance variable to maintain a reference to the value you pass, for example...
public class Button extends JPanel
{
private Graphics page;
public Button(Graphics aPage)
{
page = aPage;
JButton change;
Font font1 = new Font("sansserif", Font.PLAIN, 10);
setBackground(Color.black);
change = new JButton("Change Light");
change.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent event)
{
page.setColor(Color.green);
page.fillOval(20, 220, 60, 60);
Have now looked more deeply into your code, I hope you are not passing the Graphics
context of a life component, as this will snap around and bite you
Upvotes: 2