我要做的就是设置卡片布局样式按钮。从理论上讲,您应该单击按钮,然后假设另一个名称(在这种情况下)出现直到完成。 (当用户关闭时)

我有这个,我觉得应该完全可以使用,但是JGrasp给我一个错误:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JDisappearingFriends extends JFrame implements ActionListener
{
   private JButton intro = new JButton("Click to see Friends!");
   private JButton nb = new JButton("Mini Vinny");
   private JButton sb = new JButton("Makayla");
   private JButton eb = new JButton("Aurora");
   private JButton wb = new JButton("Alyssa");
   private JButton cb = new JButton("And My Bestest Friend: SAMMY!!");
   CardLayout cardLayout = new CardLayout();
   public JDisappearingFriends()
   {
      setLayout(new CardLayout());
      add("Click to see Friends!", intro);
      add("MiniVinny", nb);
      add("Makayla", sb);
      add("Aurora", eb);
      add("Alyssa", wb);
      add("And Finally My Best Friend Sammy!", cb);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      intro.addActionListener(this);
      nb.addActionListener(this);
      sb.addActionListener(this);
      eb.addActionListener(this);
      wb.addActionListener(this);
   }
   public void actionPreformed(ActionEvent e)
   {
      cardLayout.next(getContentPane());
   }
   public static void main(String[] args)
   {
      JDisappearingFriends jbl = new JDisappearingFriends();
      jbl.setSize(400, 400);
      jbl.setVisible(true);
   }
}


当我尝试编译时,出现以下错误消息:

JDisappearingFriends.java:8: error: JDisappearingFriends is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener


公共类JDisappearingFriends扩展JFrame实现ActionListener
       ^
如果有人可以给我一个提示,将不胜感激!

最佳答案

您的错误消息为您提供了有关问题所在的良好指示:

JDisappearingFriends.java:8: error: JDisappearingFriends is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener

actionPreformed拼写错误,应为actionPerformed

您还应该习惯使用@Override批注

@Override
public void actionPreformed(ActionEvent e)
{
    cardLayout.next(getContentPane());
}


当您从其他类覆盖方法时,这会有所帮助,并提供一个编译时间保护,以确保您不会意外拼写错误的方法名称

您还应该在EDT的上下文中创建UI。有关更多详细信息,请参见Initial Threads

08-05 06:44