Sahil Singh
Sahil Singh

Reputation: 9

addActionListener in type AbstractButton not applicable for arguments (DigiUhr)

I have searched for this Problem and I found some threads, regardless they did not solve my issue since they all used different implementations of the ActionListener Interface. My Program is supposed to display a simple digital watch with a button to choose each position (hour min sec) and when chosen increase by pressing the other button. In order to achieve that I have used the ActionListener Interface. Zustand -> State, basically 'z' registers what state we are in and then changes the action performed when pressed each button. Did I forget importing something, am I just overseeing something?

package semester2;
import javax.swing.*;
import java.awt.event.*;
import java.util.Calendar;
import java.awt.*;

@SuppressWarnings("serial")
public class DigiUhr extends JFrame implements ActionListener {
    Container c;
    int h, m, s;
    JButton choose, inc;
    JPanel Display;
    Zustand z = new Ausgang();
    JLabel hour, min, sec, brand;

    public DigiUhr(){
        c = getContentPane();

        //Buttons
        choose = new JButton(">");
        choose.setBackground (Color.black);
        choose.setForeground(Color.yellow);
        inc = new JButton ("+");
        inc.setBackground(Color.black);
        inc.setForeground(Color.yellow);
        choose.addActionListener(this);
        inc.addActionListener(this);
        choose.setPreferredSize(new Dimension(80,80));
        inc.setPreferredSize(new Dimension(80,80));

        //Uhrzeit
        Calendar jz = Calendar.getInstance();
        h = jz.get(Calendar.HOUR_OF_DAY);
        m = jz.getMaximum(Calendar.MINUTE);
        s = jz.get (Calendar.SECOND);

        //Display
        Display = new JPanel (new BorderLayout());
        Display.setBackground(Color.yellow);
        Display.setPreferredSize(new Dimension (300,300));

        //Labels
        brand = new JLabel ("ROLEX");
        hour = new JLabel();
        min = new JLabel();
        sec = new JLabel();
        hour.setForeground(Color.black);
        min.setForeground(Color.black);
        sec.setForeground(Color.black);

        //add Labels to Display
        Display.add(brand, BorderLayout.NORTH);
        Display.add(hour, BorderLayout.WEST);
        Display.add(min, BorderLayout.CENTER);
        Display.add(sec, BorderLayout.EAST);

        //Configure Labels
        hour.setText(getHour());
        hour.setHorizontalTextPosition(JLabel.CENTER);
        min.setText(getMin());
        min.setHorizontalTextPosition(JLabel.CENTER);
        sec.setText(getSec());
        sec.setHorizontalTextPosition(JLabel.CENTER);

        c.setLayout(new FlowLayout(FlowLayout.CENTER, 30, 0));
        c.add(inc);
        c.add(Display);
        c.add(choose);
    }

    public void actionPerformed (ActionEvent e){
        if (e.getSource() == choose){
            z.Button1Pressed();
        }
        if(e.getSource() == inc){
            z.Button2Pressed();
        }
    }
    abstract class Zustand {
        abstract void Button1Pressed();
        abstract void Button2Pressed();
    }
    class Ausgang extends Zustand {
        void Button1Pressed(){
            hour.setForeground(Color.GRAY);
            min.setForeground(Color.black);
            sec.setForeground(Color.black);
            z = new StundenStellen();
        }
        void Button2Pressed(){}
    }
    class StundenStellen extends Zustand {
        void Button1Pressed(){
            hour.setForeground(Color.black);
            min.setForeground(Color.gray);
            sec.setForeground(Color.black);
            z = new MinutenStellen();
        }
        void Button2Pressed(){
            h = h++;
            hour.setText(getHour());
        }
    }
    class MinutenStellen extends Zustand{
        void Button1Pressed(){
            hour.setForeground(Color.black);
            min.setForeground(Color.black);
            sec.setForeground(Color.gray);
            z = new SekundenStellen();
        }
        void Button2Pressed(){
            m = m++;
            min.setText(getMin());
        }
    }
    class SekundenStellen extends Zustand{
        void Button1Pressed(){
            hour.setForeground(Color.black);
            min.setForeground(Color.black);
            sec.setForeground(Color.black);
            z = new Ausgang();
        }
        void Button2Pressed(){
            s = s++;
            sec.setText(getSec());
        }
    }
    String getHour(){
        return(String.valueOf(h));
    }
    String getMin(){
        return(String.valueOf(m));
    }
    String getSec(){
        return(String.valueOf(s));
    }

    public static void main (String [] args){
        DigiUhr Rolex = new DigiUhr();
        Rolex.setSize(700,700);
        Rolex.setVisible(true);
        Rolex.setTitle("Rolex Submariner");
        Rolex.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

The Error message looks as following:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (DigiUhr)
    The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (DigiUhr)

    at semester2.DigiUhr.<init>(DigiUhr.java:26)
    at semester2.DigiUhr.main(DigiUhr.java:139)

Upvotes: 1

Views: 444

Answers (1)

Prasad Karunagoda
Prasad Karunagoda

Reputation: 2148

Did you have implements ActionListener part in your code when you got the error? This error occurs when you don't have this part.

This code you have posted compiles without any errors.

Upvotes: 2

Related Questions