Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I implement ActionListener with a non abstract class? Java

I've just recently started learning how to use swing, and have been following a tutorial I found online. I've basically followed the tutorial "word for word" but I get the error:

ScoreBoard is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener

So my question is, how can I implement ActionListener into my class (ScoreBoard) if the class is not abstract?

Here is the entire code: (Because I have no idea where the problem could be)

package scoreboard;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class ScoreBoard implements ActionListener{

//Class Variables
int redScore = 0;
int blueScore = 0;

//Class Objects
JPanel titlePanel, scorePanel, buttonPanel;
JLabel redLabel, blueLabel, redLabelT, blueLabelT;
JButton redButton, blueButton, resetButton;


public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable(){
        public void run(){
            createFrame();
        }
    });

}//End Method

public static void createFrame(){

    JFrame frame = new JFrame("ScoreBoard");
    JFrame.setDefaultLookAndFeelDecorated(true);

    ScoreBoard panel = new ScoreBoard();
    frame.setContentPane(panel.contentPane());

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);
    frame.setSize(250, 190);
    frame.setVisible(true);

}//End Method

public JPanel contentPane(){

    JPanel basePanel = new JPanel();
    basePanel.setLayout(null);
    basePanel.setBackground(Color.black);

    //Title
    titlePanel = new JPanel();
    titlePanel.setLayout(null);
    titlePanel.setOpaque(false);
    titlePanel.setLocation(10, 0);
    titlePanel.setSize(250, 30);
    basePanel.add(titlePanel);

    redLabelT = new JLabel("Red Team");
    redLabelT.setLocation(0, 0);
    redLabelT.setSize(100, 30);
    redLabelT.setForeground(Color.red);
    redLabelT.setHorizontalAlignment(0);
    titlePanel.add(redLabelT);

    blueLabelT = new JLabel("Blue Team");
    blueLabelT.setLocation(120, 0);
    blueLabelT.setSize(100, 30);
    blueLabelT.setForeground(Color.blue);
    blueLabelT.setHorizontalAlignment(0);
    titlePanel.add(blueLabelT);

    //Score
    scorePanel = new JPanel();
    scorePanel.setLayout(null);
    scorePanel.setOpaque(false);
    scorePanel.setLocation(10, 40);
    scorePanel.setSize(250, 30);
    basePanel.add(scorePanel);

    redLabel = new JLabel("" + redScore);
    redLabel.setLocation(0, 0);
    redLabel.setSize(100, 30);
    redLabel.setForeground(Color.white);
    redLabel.setHorizontalAlignment(0);
    scorePanel.add(redLabel);

    blueLabel = new JLabel("" + blueScore);
    blueLabel.setLocation(120, 0);
    blueLabel.setSize(100, 30);
    blueLabel.setForeground(Color.white);
    blueLabel.setHorizontalAlignment(0);
    scorePanel.add(blueLabel);

    //Buttons
    buttonPanel = new JPanel();
    buttonPanel.setLayout(null);
    buttonPanel.setOpaque(false);
    buttonPanel.setLocation(10, 80);
    buttonPanel.setSize(250, 70);
    basePanel.add(buttonPanel);

    redButton = new JButton("Red Score");
    redButton.setLocation(0, 0);
    redButton.setSize(100, 30);
    redButton.addActionListener(this);
    buttonPanel.add(redButton);

    blueButton = new JButton("Blue Score");
    blueButton.setLocation(120, 0);
    blueButton.setSize(100, 30);
    blueButton.addActionListener(this);
    buttonPanel.add(blueButton);

    resetButton = new JButton("Reset");
    resetButton.setLocation(0, 40);
    resetButton.setSize(220, 30);
    resetButton.addActionListener(this);
    buttonPanel.add(resetButton);

    return basePanel;

   }//End Method

public void actions(ActionEvent e){

    if(e.getSource() == redButton){

        redScore ++;
        redLabel.setText("" + redScore);

    }else if(e.getSource() == blueButton){

        blueScore++;
        blueLabel.setText("" + blueScore);

    }else if(e.getSource() == resetButton){

        redScore = 0;
        blueScore = 0;
        redLabel.setText("" + redScore);
        blueLabel.setText("" + blueScore);

    }

}//End Method

}//End Class

Also if you can explain what an Abstract class is, that'd help too, but really I just need to know how to get JButtons working for now...

Thanks!

like image 510
Nick Burt Avatar asked Jul 14 '26 19:07

Nick Burt


2 Answers

The compiler is complaining because your class is not abstract but it does not implement one or more methods that it is declared to implement (specifically the method actionPerformed of ActionListener). I think you simply need to rename your actions method:

public void actions(ActionEvent e){. . .

public void actionPerformed(ActionEvent e){. . .
like image 81
Ted Hopp Avatar answered Jul 16 '26 09:07

Ted Hopp


Put this method into your ScoreBoard class-

@Override
public void actionPerformed(ActionEvent ae) {
     // do something
}

You can also add listener in this way, if you don't want ScoreBoard class to implement ActionListener-

redButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent ae) {
         // do something
    }
};

If you want to share the listener, create its instance and add it to all the buttons.

To learn about abstract classes, read this http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html

like image 35
Sudhanshu Umalkar Avatar answered Jul 16 '26 10:07

Sudhanshu Umalkar