Main 必须是抽象的,Listener 的 ActionPerformed 方法问题

Main has to be astract, ActionPerformed method problem with Listener

这个程序只是为了自学Java。 在将 i 运行 编码成以下问题时: 我在 Main class.

中为按钮使用侦听器时出现错误(红色下划线)

由于我是新手 Java 如果解决方案是 obvious.I 已经尝试过将主要方法和 actionPerfomed 方法都抽象化,请原谅,但这会导致进一步的问题。我也在 actionPerformed 方法之前尝试了 @Override

代码如下:

// Java program to create a blank text
// field of definite number of columns.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Main extends JFrame implements ActionListener {
    // JTextField
    static JTextField t;

    // JFrame
    static JFrame f;

    // JButton
    static JButton b;

    // label to diaplay text
    static JLabel l;

    // default constructor
    Main()
    {
    }

    // main class
    public static void main(String[] args)
    {
        // create a new frame to stor text field and button
        f = new JFrame("textfield");

        // create a label to display text
        l = new JLabel("nothing entered");

        // create a new button
        b = new JButton("submit");

        // create a object of the text class
        Main te = new Main();

        // addActionListener to button
        b.addActionListener(te);

        // create a object of JTextField with 16 columns
        t = new JTextField(16);

        // create a panel to add buttons and textfield
        JPanel p = new JPanel();

        // add buttons and textfield to panel
        p.add(t);
        p.add(b);
        p.add(l);

        l.setOpaque(true);
        // add panel to frame
        f.add(p);

        // set the size of frame
        f.setSize(300, 300);

        p.setBackground(Color.cyan);

        f.show();
    }

    // if the button is pressed
    public void actionPerformed(java.awt.event.ActionEvent e, JPanel p)
    {
        String s = e.getActionCommand();
        if (s.equals("submit")) {
            // set the text of the label to the text of the field
            if(t.getText().equals("hue")) {

                p.setBackground(changeColor());
            }
            l.setText(t.getText());

            // set the text of field to blank
            t.setText(" ");
        }
    }
    public Color changeColor() {
        int r = (int)(Math.random())*256;
        int g = (int)(Math.random())*256;
        int b = (int)(Math.random())*256;
        Color color = new Color(r,g,b);
        return color;
    }
}

这里:

public void actionPerformed(java.awt.event.ActionEvent e, JPanel p)

应该是

public void actionPerformed(java.awt.event.ActionEvent e)

您必须完全匹配 预期的 签名!您不能只将 more 参数添加到您应该 override!

的方法中

您会看到,最终是而不是您的代码会在单击按钮时调用该方法。 Swing 框架可以做到这一点。你告诉它:当一个action发生在这个东西上,然后回调我的代码。你怎么希望这个框架知道你想要传递一个附加参数?!

除此之外:习惯将 @Override 放在您希望重写方法的任何方法前面(例如在本例中,您正在实现该接口的方法)。因为当你犯这样的错误时,编译器会告诉你!

当然,你添加了那个参数,以便听众可以使用它。所以:让听众知道它,例如通过在构造函数中初始化的 Main class 添加一个字段!因此,在为您的 Main 实例执行 new 时,不要将面板传递给那个方法(它不能去的地方)传递它。

当 class 实现接口时,它必须使用与接口中给定的参数相同的参数来实现所需的功能。 正如您在 https://docs.oracle.com/javase/7/docs/api/java/awt/event/ActionListener.html

中看到的那样,ActionListener 仅实现一个功能
void actionPerformed(ActionEvent e);

因此您必须在 class 中实现该功能。但是你实现了一个不同的函数,它具有相同的名称但不同的参数:

void actionPerformed(java.awt.event.ActionEvent e, JPanel p)