首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java CardLayout不显示

Java CardLayout不显示
EN

Stack Overflow用户
提问于 2014-06-03 05:22:37
回答 1查看 178关注 0票数 0

我有一个游戏,已经运行和工作,我想添加一个标题屏幕到它。我正在尝试添加一个CardLayout,以便在游戏和标题屏幕之间轻松切换。我目前的问题是什么都没有显示出来。这是一张图片:http://i.imgur.com/kVIXYQ7.png。当我运行它时,我只是得到一个空白的JPanel。我做错了什么?

代码语言:javascript
复制
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);}
}
EN

回答 1

Stack Overflow用户

发布于 2014-06-03 05:25:40

您还没有将Cards添加到任何内容。

尝试将Cards添加到框架,然后使框架可见

例如..。

代码语言:javascript
复制
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);

仅供参考:您应该在设置框架大小之前使用setResizable,因为setResizable可以更改框架的边框插入

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24003518

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档