类型AbstractButton中的addActionListener不适用于参数(DigiUhr)

时间:2019-01-19 14:06:28

标签: java eclipse user-interface jbutton actionlistener

我已经搜索了这个问题,并且找到了一些线程,无论它们没有解决我的问题,因为它们都使用了ActionListener接口的不同实现。我的程序应该显示一个带有按钮的简单数字手表,以选择每个位置(时分秒),并在选择时通过按另一个按钮来增加。为了实现这一点,我使用了ActionListener接口。 Zustand->状态,基本上'z'记录我们所处的状态,然后更改按下每个按钮时执行的操作。我忘了导入东西吗,我只是在监督什么?

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);
    }
}

错误消息如下:

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)

1 个答案:

答案 0 :(得分:1)

出现错误时,代码中是否包含implements ActionListener部分?当您没有此部分时,会发生此错误。

您发布的这段代码可以正确编译。