简体   繁体   English

尝试使cardLayout正常工作

[英]Attempting to get cardLayout to work

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: 我有这个,我觉得应该完全可以使用,但是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);
   }
}

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!! 公共类JDisappearingFriends扩展JFrame实现ActionListener ^如果有人可以给我一个提示,将不胜感激!

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 actionPreformed错误,应为actionPerformed

You should also get use to using the @Override annotation 您还应该@Override使用@Override批注

@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. 您还应该在EDT的上下文中创建UI。 See Initial Threads for more details. 有关更多详细信息,请参见初始线程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM