【问题标题】:How can I implement ActionListener with a non abstract class? Java如何使用非抽象类实现 ActionListener?爪哇
【发布时间】:2013-03-11 20:54:50
【问题描述】:

我最近才开始学习如何使用 swing,并且一直在学习我在网上找到的教程。我基本上按照教程“逐字逐句”进行操作,但出现错误:

ScoreBoard 不是抽象的,不会覆盖抽象方法 ActionListener 中的 actionPerformed(ActionEvent)

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

这是完整的代码:(因为我不知道问题出在哪里)

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 工作......

谢谢!

【问题讨论】:

    标签: java swing abstract-class actionlistener


    【解决方案1】:

    你需要检查ActionListener的所有abstract方法是否都实现了。

    您缺少void actionPerformed(ActionEvent e) 的定义

    【讨论】:

      【解决方案2】:

      将此方法放入您的 ScoreBoard 类中-

      @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

      【讨论】:

      • 谢谢!也感谢你的链接,看起来它会有很大帮助!
      【解决方案3】:

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

      罢工>

      public void actions(ActionEvent e){. . .
      

      public void actionPerformed(ActionEvent e){. . .
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-05-14
        • 1970-01-01
        • 1970-01-01
        • 2014-04-17
        • 2017-08-27
        • 1970-01-01
        • 2018-12-25
        • 1970-01-01
        相关资源
        最近更新 更多