gpt4 book ai didi

Java CardLayout 不显示

转载 作者:行者123 更新时间:2023-12-01 12:54:27 26 4
gpt4 key购买 nike

我有一个已经运行并可以运行的游戏,我想为其添加一个标题屏幕。我正在尝试添加 CardLayout 以便在游戏和标题屏幕之间轻松切换。我当前的问题是没有显示任何内容。这是一张图片:/image/AooMO.png 。当我运行它时,我只是得到一个空白的 JPanel。我究竟做错了什么?

public class UI
{
private static JPanel cards;
private static CardLayout cardLayout;

public static void main(String args[])
{new UI();}

public UI()
{
JFrame frame = new JFrame("Scrolling Shooter");

frame.setResizable(false);
Toolkit tk = Toolkit.getDefaultToolkit();
int width = ((int) tk.getScreenSize().getWidth());
int height = ((int) tk.getScreenSize().getHeight());
frame.setSize(width, height);

TitleScreen title = new TitleScreen(frame);
ScrollingShooter game = new ScrollingShooter(frame);

cards = new JPanel(new CardLayout());
cards.add(title, "title");
cards.add(game, "game");
cards.setOpaque(true);
frame.getContentPane().add(cards);

cardLayout = (CardLayout) cards.getLayout();
cardLayout.first(cards);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

/**
* Switches to the next screen
*/
public static void nextScreen()
{cardLayout.next(cards);}

/**
* Returns to the first screen
*/
public static void firstScreen()
{cardLayout.first(cards);}
}

最佳答案

您尚未将卡片添加到任何内容。

尝试将卡片添加到框架中,然后使框架可见

例如...

JFrame frame = new JFrame("Scrolling Shooter");

TitleScreen title = new TitleScreen(frame);
ScrollingShooter game = new ScrollingShooter(frame);

cards = new JPanel(new CardLayout());
cards.add(title, "title");
cards.add(game, "game");
cards.setOpaque(true);

cardLayout = (CardLayout) cards.getLayout();
cardLayout.first(cards);

frame.add(cards);
//...
frame.setResizable(false);
frame.setSize(width, height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);

仅供引用:您应该在设置框架大小之前先来setResizing,因为setResizes可以更改框架边框插入

关于Java CardLayout 不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24003518/

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