【问题标题】:Keep getting error "(CLASS) is not abstract and does not override abstract method"不断收到错误“(CLASS)不是抽象的,不会覆盖抽象方法”
【发布时间】:2014-10-03 18:54:34
【问题描述】:

当我尝试编译我的代码时,我得到了两个相同的错误(标题中的那个)。我基本上只是从我拥有的示例中复制而来,我不明白为什么会出现错误。我还在学习 Java,所以我提前为我犯的任何错误道歉:P。代码如下:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ColorFactory extends JFrame {
 private final int winWidth=500;
 private final int winHeight=300;
 private JPanel topPanel;
 private JPanel bottomPanel;
 private JButton redButton;
 private JButton yellowButton;
 private JButton orangeButton;
 private JRadioButton greenRadio;
 private JRadioButton blueRadio;
 private JRadioButton cyanRadio;
 private JLabel message;

    public ColorFactory() {
     setTitle("Color Factory");
     setSize(winWidth, winHeight);
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     setLayout(new BorderLayout());
     topPanel=new JPanel();
     bottomPanel=new JPanel();  
     message=new JLabel("Top buttons change the panel color and bottom radio buttons change the text color.");
     add(topPanel, BorderLayout.NORTH);
     add(bottomPanel, BorderLayout.SOUTH);
     add(message, BorderLayout.CENTER);
    }

    private void topButtons(){
     redButton=new JButton("Red");
     orangeButton=new JButton("Orange");
     yellowButton=new JButton("Yellow");
     setLayout(new FlowLayout());
     topPanel.setBackground(Color.WHITE);
     add(redButton);
     add(orangeButton);
     add(yellowButton);
     redButton.setBackground(Color.RED);
     yellowButton.setBackground(Color.YELLOW);
     orangeButton.setBackground(Color.ORANGE);
     redButton.addActionListener(new ButtonListener());
     orangeButton.addActionListener(new ButtonListener());
     yellowButton.addActionListener(new ButtonListener());
    }

    private void bottomButtons(){
      greenRadio=new JRadioButton("Green");
      blueRadio=new JRadioButton("Blue");
      cyanRadio=new JRadioButton("Cyan");
      setLayout(new FlowLayout());
      bottomPanel.setBackground(Color.WHITE);
      add(greenRadio);
      add(blueRadio);
      add(cyanRadio);
      greenRadio.setForeground(Color.GREEN);
      blueRadio.setForeground(Color.BLUE);
      cyanRadio.setForeground(Color.CYAN);
      greenRadio.addActionListener(new RadioButtonListener());
      blueRadio.addActionListener(new RadioButtonListener());
      cyanRadio.addActionListener(new RadioButtonListener());
    }

    private class ButtonListener implements ActionListener{
     public void actionPerfomed(ActionEvent e){
      String command=e.getActionCommand();
      if(command.equals("Red")){
       message.setBackground(Color.RED);
      }
      else if(command.equals("Yellow")){
       message.setBackground(Color.YELLOW);
      }
      else if(command.equals("Orange")){
       message.setBackground(Color.ORANGE); 
      }
     }
    }

    private class RadioButtonListener implements ActionListener{
     public void actionPerfomed(ActionEvent i){
      if(i.getSource()==greenRadio){
       message.setForeground(Color.GREEN);
      }
      else if(i.getSource()==blueRadio){
       message.setForeground(Color.BLUE);
      }
      else if(i.getSource()==cyanRadio){
       message.setForeground(Color.CYAN);   
      }
     }
    }

    public static void main(String[] args){
     new ColorFactory();
    }
}

这是我得到的错误:

error: ColorFactory.RadioButtonListener is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener
    private class RadioButtonListener implements ActionListener{
            ^

提前致谢!

【问题讨论】:

    标签: methods overriding abstract


    【解决方案1】:

    方法名应该是actionPerformed而不是actionPerfomed。你错过了r

    【讨论】:

    • 真是个白痴,我也瞎了哈哈。感谢您的回复!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-05
    • 1970-01-01
    • 2014-06-20
    • 2014-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多