Attempting to get cardLayout to work

98 views Asked by At

All I am trying to do is set up a card layout style buttons. In theory you are suppose to click the button and another (in this case) name is suppose to come up until finished. (when user is to close)

I have this, I feel it should TOTALLY work, but JGrasp is giving me an error:

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);
   }
}

When I attempt to compile, I get one error message, listed below:

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

public class JDisappearingFriends extends JFrame implements ActionListener ^ If anyone one can throw me a hint, it would be much appreciated!!

1

There are 1 answers

2
MadProgrammer On BEST ANSWER

Your error message is providing your with a good indication of what the problem is:

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

actionPreformed is mispelled and should be actionPerformed

You should also get use to using the @Override annotation

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

This helps when you are overriding methods from other classes and provides a compile time guard to ensure you don't accidentally misspell the method name

You should also be creating your UI from within the context of the EDT. See Initial Threads for more details.