我有一个游戏,已经运行和工作,我想添加一个标题屏幕到它。我正在尝试添加一个CardLayout,以便在游戏和标题屏幕之间轻松切换。我目前的问题是什么都没有显示出来。这是一张图片:http://i.imgur.com/kVIXYQ7.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);}
}发布于 2014-06-03 05:25:40
您还没有将Cards添加到任何内容。
尝试将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);仅供参考:您应该在设置框架大小之前使用setResizable,因为setResizable可以更改框架的边框插入
https://stackoverflow.com/questions/24003518
复制相似问题