当前位置: 首页 > 知识库问答 >
问题:

如何用非抽象类实现ActionListener?JAVA

穆彬郁
2023-03-14

我最近刚刚开始学习如何使用swing,并且一直在遵循我在网上找到的教程。我基本上遵循了“逐字逐句”的教程,但我发现了一个错误:

记分板不是抽象的,并且不会覆盖ActionListener中的抽象方法actionPerformed(ActionEvent)

所以我的问题是,如果类不是抽象的,我如何将ActionListener实现到我的类(ScoreBoard)中?

下面是整个代码:(因为我不知道问题可能在哪里)

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

另外,如果你能解释什么是抽象类,这也会有帮助,但实际上我只需要知道如何让JButtons现在工作。。。

谢谢

共有3个答案

刘意
2023-03-14

您需要检查是否实现了ActionListener的所有抽象方法。

您缺少void actionPerformed(ActionEvent e)

文心思
2023-03-14

把这个方法放到你的记分板课上-

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

您也可以通过这种方式添加侦听器,如果您不希望ScoreBoard类实现ActionListener-

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

如果想共享侦听器,请创建其实例并将其添加到所有按钮中。

要了解抽象类,请阅读以下内容http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html

孙阳舒
2023-03-14

编译器之所以抱怨,是因为您的类不是抽象的,但它没有实现声明要实现的一个或多个方法(特别是actionPerformedActionListener)。我认为您只需要重命名操作方法:

public void actions(ActionEvent e){. . .
public void actionPerformed(ActionEvent e){. . .
 类似资料:
  • 这似乎是一个基本问题。但在采访前需要澄清。 我在抽象类中有一个非抽象方法。它的具体类重写了该方法。但我想调用父类的原始方法来调用,而不是重写方法。有什么办法吗? 据我所知,没有办法调用原始方法?

  • 问题内容: 如何在Go中实现抽象类?由于Go不允许我们在接口中包含字段,因此这将是一个无状态的对象。因此,换句话说,Go中的方法是否可以具有某种默认实现? 考虑一个例子: 由于无法将接口用作接收器,因此无法编译。 实际上,我已经回答了我的问题(请参见下面的答案)。但是,这是实现这种逻辑的惯用方式吗?除了语言的简单性之外,还有什么理由不使用默认实现吗? 问题答案: 一个简单的解决方案是移至参数列表(

  • 我有扩展抽象类的类,我不想把放在所有子类的顶部。 有没有办法为抽象类实现Lombok?

  • 有人可以向我解释为什么它总是给我这个错误 错误:MyPanel不是抽象的,并且不重写ActionListener公共类MyPanel extends JPanel实现ActionListener中的抽象方法actionPerformed(ActionEvent){ 我想我做的一切都是对的,我不知道我做错了什么,这段代码用于测试使图像水平移动 这是我的密码 Main.java 我的框架。Java语言

  • 问题内容: 实施接口的最佳方法是什么? 让您的类实现ActionListener并将其添加为ActionListener: 或添加匿名ActionListener类的对象: 问题答案: 有些人(jeanette / kleopatra)表示几乎 从不 使用ActionListener,而是使用诸如AbstractAction之类的Action。让GUI类实现侦听器几乎总是一个糟糕的理想选择,因为这

  • 我有两个方法和抽象类的接口,它们实现了这个接口并从它重写了一个方法。我可以实例这个抽象类,而不重写接口的其他方法吗?或者我可以用具有另一个签名的方法替换此方法吗? UPD:谢谢你的回答,我真的在我的问题上犯了错误。我是否可以使用匿名类来扩展我的抽象类,而不重写来自实现的所有方法?我如何理解DragonK的回答,不,我需要创建类,这将是扩展抽象类和重写其他方法?