0

I am coding one applet that will show the number of mouse clicks on a button, and another that will show the coordinates of a mouse click and change the background color to a random color.

In both cases, I encountered the "is not abstract and does not override abstract method" error in BlueJ, and I need help understanding what I am doing wrong. I'm very new to this, so any tips/proofreadings are welcome as well :)

here's the first applet:

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

public class Option1 extends Frame implements ActionListener {
public void main (String[] args) {
int click = 0;

JFrame base = new JFrame ("Button Click Counter");
base.getContentPane().setLayout(null);
base.setSize(500,500);
base.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JButton button = new JButton("Click Me!");
button.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed (ActionEvent e) {
        click++; }
    }); 

    JTextField count = new JTextField(click);

        this.add(button);
        this.add(count);

    }}    

and the coordinates one:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;

public class Option2 extends Frame implements MouseListener {
double x;
double y;
public void init() {
addMouseListener(this);


JFrame base = new JFrame("Mouse Coordinates");
base.getContentPane().setLayout(null);
base.setSize(500,500);
base.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


JTextField answer = new JTextField(x + "," + y);
}
public void mouseClicked( MouseEvent e ) {
    x = e.getX();
    y = e.getY();

    this.setBackground(new Color((int)(Math.random() * 0x1000000)));
}}

Thanks in advance!

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • You have an interface `implements ActionListener`, did you implement it? – Elliott Frisch Jul 31 '14 at 06:56
  • this.setBackground(new Color((int)(Math.random() * 0x1000000))); going to the MilkyWay, awt.Frame doesn't react to setBackground (bug or feature) – mKorbel Jul 31 '14 at 07:08
  • @mKorbel, I was trying to use a method described here http://stackoverflow.com/questions/4246351/creating-random-colour-in-java to create a random color... how would you recommend I fix this then? – user3872950 Jul 31 '14 at 07:25
  • use Swing JFrame and its getContentPane react to Color – mKorbel Jul 31 '14 at 07:41
  • public class Option1 extends Frame implements ActionListener { and JFrame base = new JFrame ("Button Click Counter"); to test which one is visible on the screen :-) – mKorbel Jul 31 '14 at 07:42
  • @mKorbel but I need the color of the JFrame to change upon a mouse click, would I need to declare a variable to do this now? – user3872950 Jul 31 '14 at 07:49

2 Answers2

0

you're implementing an interface...

at least you pronouce you do it when you write

public class Option1 extends Frame implements ActionListener

but you don't keep your contract! if you say you implement ActionListener, then you guaranteed that you have this method

public void actionPerformed(ActionEvent e) {        
}

but your code lacks this method - and that's your error message...

Martin Frank
  • 3,445
  • 1
  • 27
  • 47
  • But is that not what is used in the 'JButton button = new JButton("Click Me!"); button.addActionListener(new ActionListener() { @Override public void actionPerformed (ActionEvent e) { click++; } }); – user3872950 Jul 31 '14 at 07:23
  • When you say "button.addActionListener(new ActionListener(){[...]" you create a NEW (anonymous) ActionListern. But you still have the case that you don't implement the actionListener from your class Option1.... – Martin Frank Jul 31 '14 at 07:37
  • if you don't want to use class Option1 as actionListener, then just remove the 'implements ActionListener' part from your class. – Martin Frank Jul 31 '14 at 07:38
0

Option1 declares to implement the ActionListener interface:

public class Option1 extends Frame implements ActionListener {

If a class does so, it must be either abstract (which Option1 is not) or it must implement the methods declared in the interface. When you take a look at the API for ActionListener, you'll find one method there:

void actionPerformed(ActionEvent e)

You need to implement this method in Option1, e.g.

@Override
public void actionPerformed(ActionEvent e) {
    click++;
}

and then register Option1 as ActionListener:

button.addActionListener(this);

The thing you tried is to implement the ClickListener in an anonymous inner class, which is also ok, but in this case Option1 must not implement ActionListener.

Your main method in Option1 is not declared properly, a main method usually is

public static void main(String[] args)

But the initialization you do in the main method should be done in the constructor or (as with Option2) in a separate method

public class Option1 extends Frame implements ActionListener {

    private int click = 0;

    public Option1() {

        JFrame base = new JFrame("Button Click Counter");
        base.getContentPane().setLayout(null);
        base.setSize(500, 500);
        base.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton button = new JButton("Click Me!");
        button.addActionListener(this);
        JTextField count = new JTextField(click);
        this.add(button);
        this.add(count);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        click++;
    }
}

The problem with Option2 is that you only implement the method mouseClicked from the interface MouseListener. There are a couple others that need to be implemented:

void mouseEntered(MouseEvent e)
void mouseExited(MouseEvent e)
void mousePressed(MouseEvent e)
void mouseReleased(MouseEvent e)

Even if you do not want to handle these events, you need to have the empty methods.

A final tip: I don't know your IDE, but many IDEs (Eclipse, Netbeans, IntelliJ) have an option to generate the required methods for an interface for you, which saves a lot of typing ;)

Ralf Wagner
  • 1,467
  • 11
  • 19
  • Thanks very much for this, I learned quite a bit... just out of curiosity, why do you change the 'click' variable to private? Also, do you know why I am not able to get the 'Run applet' option in BlueJ and am instead getting these options? ==> (http://imgur.com/F85aOPW) – user3872950 Jul 31 '14 at 07:53
  • making click private: I usually start with the least accessibility level (private) for variables - see check out "Encapsulation" as a principle for object oriented programming – Ralf Wagner Jul 31 '14 at 08:03
  • Run applet: I'm not familiar with BlueJ, but Option1 is not an applet i.e. does not extend the Applet class. Maybe you need another class that implements Applet and shows the UI. – Ralf Wagner Jul 31 '14 at 08:12