Java 试图创建可单击按钮,但在编译时抛出错误

Java 试图创建可单击按钮,但在编译时抛出错误,java,swing,jframe,abstract,jbutton,Java,Swing,Jframe,Abstract,Jbutton,CountingNumbers不是抽象的,并且不会覆盖java.awt.event.ActionListener中的抽象方法actionPerformed(java.awt.event.ActionEvent) 这是我得到的错误 我的代码: //Create an application that counts the number of each letter in a given word/phrase //have a box that has a text box a button an

CountingNumbers不是抽象的,并且不会覆盖java.awt.event.ActionListener中的抽象方法actionPerformed(java.awt.event.ActionEvent)

这是我得到的错误

我的代码:

//Create an application that counts the number of each letter in a given word/phrase
//have a box that has a text box a button and output box

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

 public class CountingNumbers extends JFrame implements ActionListener
 {

   public void count()

{

    JFrame frame = new JFrame("Counting Letters Application");
    JTextField textEntry = new JTextField("enter your word/phrase here", 1);
    JButton count = new JButton("Count Letters");
    count.addActionListener(this);

    frame.setPreferredSize(new Dimension(400, 300));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Color teal = new Color(0, 128, 128);
    frame.getContentPane().setBackground(teal);
    frame.getContentPane().add(textEntry, BorderLayout.NORTH);
    frame.getContentPane().add(count, BorderLayout.EAST);

    frame.pack();
    frame.setVisible(true);

   }

   public void actionPerformed(ActionEvent e, JFrame frame) 
   {

       JTextField output = new JTextField("output box", 4);
       frame.getContentPane().add(output, BorderLayout.WEST);
       output.setText("button was clicked");
   }

}      

我不知道为什么会发生这种情况,我对java相当陌生,刚刚从我学校的一个班级学到。

当一个班级实现一个接口时,它必须按照接口中定义的方式实现该接口的方法。您的类实现了ActionListener,虽然它有一个actionPerformed方法,但方法签名与接口的签名并不完全匹配,因为该方法应该只有一个参数ActionEvent参数,而您的方法有2个参数ActionEvent参数和JFrame参数。因为这会改变方法,编译器会抱怨您没有实现接口的所有方法

一些建议:

  • 去掉actionPerformed方法(见下文)中的JFrame参数,以便actionPerformed方法的签名与接口的签名匹配
  • 将JFrame变量设为类字段,使其在整个类中可见。这样,actionPerformed方法就不需要参数
  • 不要在count方法(或类构造函数)中重新声明JFrame,而是使用class字段(见下文)
  • 不要让类扩展JFrame,因为它不是JFrame
  • 修复代码格式,尤其是使缩进符合标准。这将使您和我们更容易理解您的代码并更好地调试它
  • 另外,不要在每次按下按钮时向gui添加JTextField,因为我强烈怀疑这是您想要做的。。。如果多次按下按钮,则添加多个JTextFields
  • 相反,您希望将输出JTextField设置为类字段,以便它在整个应用程序中都可见,您希望将其添加到JFrame中,或者在将其他组件添加到GUI的同时将其添加到JFrame所持有的容器中。然后,您可以简单地从actionPerformed方法中设置JTextField的文本,而不是创建一个新字段
所以不是这样:

public void actionPerformed(ActionEvent e, JFrame frame)
public class CountingNumbers extends JFrame implements ActionListener {

   public void count() {
      JFrame frame = new JFrame("Counting Letters Application");
      JTextField textEntry = new JTextField("enter your word/phrase here", 1);
但这是:

public void actionPerformed(ActionEvent e)
而不是这个:

public void actionPerformed(ActionEvent e, JFrame frame)
public class CountingNumbers extends JFrame implements ActionListener {

   public void count() {
      JFrame frame = new JFrame("Counting Letters Application");
      JTextField textEntry = new JTextField("enter your word/phrase here", 1);
但是这个

public class CountingNumbers implements ActionListener {
   private JFrame frame = new JFrame("Counting Letters Application");
   private JTextField outputField = new JTextField(10);

   public void count() {
      JTextField textEntry = new JTextField("enter your word/phrase here", 1);

      // add your outputField to the GUI from somewhere in this method

稍后,我们可以讨论为什么让GUI类实现侦听器接口通常是个坏主意,但我们不要一下子用太多的更改和建议来压倒您。:)

当一个类实现一个接口时,它必须完全按照接口中定义的方式实现该接口的方法。您的类实现了ActionListener,虽然它有一个actionPerformed方法,但方法签名与接口的签名并不完全匹配,因为该方法应该只有一个参数ActionEvent参数,而您的方法有2个参数ActionEvent参数和JFrame参数。因为这会改变方法,编译器会抱怨您没有实现接口的所有方法

一些建议:

  • 去掉actionPerformed方法(见下文)中的JFrame参数,以便actionPerformed方法的签名与接口的签名匹配
  • 将JFrame变量设为类字段,使其在整个类中可见。这样,actionPerformed方法就不需要参数
  • 不要在count方法(或类构造函数)中重新声明JFrame,而是使用class字段(见下文)
  • 不要让类扩展JFrame,因为它不是JFrame
  • 修复代码格式,尤其是使缩进符合标准。这将使您和我们更容易理解您的代码并更好地调试它
  • 另外,不要在每次按下按钮时向gui添加JTextField,因为我强烈怀疑这是您想要做的。。。如果多次按下按钮,则添加多个JTextFields
  • 相反,您希望将输出JTextField设置为类字段,以便它在整个应用程序中都可见,您希望将其添加到JFrame中,或者在将其他组件添加到GUI的同时将其添加到JFrame所持有的容器中。然后,您可以简单地从actionPerformed方法中设置JTextField的文本,而不是创建一个新字段
所以不是这样:

public void actionPerformed(ActionEvent e, JFrame frame)
public class CountingNumbers extends JFrame implements ActionListener {

   public void count() {
      JFrame frame = new JFrame("Counting Letters Application");
      JTextField textEntry = new JTextField("enter your word/phrase here", 1);
但这是:

public void actionPerformed(ActionEvent e)
而不是这个:

public void actionPerformed(ActionEvent e, JFrame frame)
public class CountingNumbers extends JFrame implements ActionListener {

   public void count() {
      JFrame frame = new JFrame("Counting Letters Application");
      JTextField textEntry = new JTextField("enter your word/phrase here", 1);
但是这个

public class CountingNumbers implements ActionListener {
   private JFrame frame = new JFrame("Counting Letters Application");
   private JTextField outputField = new JTextField(10);

   public void count() {
      JTextField textEntry = new JTextField("enter your word/phrase here", 1);

      // add your outputField to the GUI from somewhere in this method

稍后,我们可以讨论为什么让GUI类实现侦听器接口通常是个坏主意,但我们不要一下子用太多的更改和建议来压倒您。:)

气垫船是正确的,但我只想补充一点,你可能是这个意思:

public void actionPerformed(ActionEvent e) 
   {
       JTextField output = new JTextField("output box", 4);
       this.getContentPane().add(output, BorderLayout.WEST);
       output.setText("button was clicked");
   }
因为“CountingNumbers”类扩展了JFrame,所以它本身实际上就是一个JFrame。这就是为什么可以调用“this.getContentPane()”而不是“frame.getContentPane()”。但只有当您希望“CountingNumbers”对象不断向自身添加文本字段时,才会出现这种情况。如果要将textfields添加到另一个帧,则必须将该帧存储为CountingNumber的私有字段(您可以在构造期间初始化该字段):


因为“CountingNumbers”还实现了接口ActionListener,所以它也是ActionListener,这意味着它需要与ActionListener接口中定义的方法相同。(如前所述)

气垫船是正确的,但我只想补充一点,你可能是指:

public void actionPerformed(ActionEvent e) 
   {
       JTextField output = new JTextField("output box", 4);
       this.getContentPane().add(output, BorderLayout.WEST);
       output.setText("button was clicked");
   }
因为“CountingNumbers”类扩展了JFrame,所以它本身实际上就是一个JFrame。这就是为什么可以调用“this.getContentPane()”而不是“frame.getContentPane()”。但只有当您希望“CountingNumbers”对象不断向自身添加文本字段时,才会出现这种情况。如果你想要我