gpt4 book ai didi

java - 尝试让cardLayout工作

转载 作者:行者123 更新时间:2023-12-02 04:34:49 24 4
gpt4 key购买 nike

我想做的就是设置一个卡片布局样式按钮。理论上,您应该单击该按钮,并且应该出现另一个(在本例中)名称,直到完成。 (当用户关闭时)

我有这个,我觉得它应该完全有效,但是 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

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

最佳答案

您的错误消息很好地表明了问题所在:

JDisappearingFriends.java:8: 错误:JDisappearingFriends 不是抽象的,并且不会重写 ActionListener 中的抽象方法 actionPerformed(ActionEvent)

actionPreformed 拼写错误,应为 actionPerformed

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

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

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

您还应该在 EDT 的上下文中创建 UI。请参阅Initial Threads了解更多详情。

关于java - 尝试让cardLayout工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30971424/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com