简体   繁体   English

JFrame和不同的“页面”内容

[英]JFrame and different “pages” of content

I'm quite new to Java, and right now I'm playing around with GUI. 我对Java非常陌生,现在我正在使用GUI。 I now have a JFrame - lets call it page1 - with a bit of content (text, images etc). 我现在有一个JFrame让我们称它为page1带有一些内容(文本,图像等)。 So what I want to do is to create several such "pages" with different content, and be able to switch between these pages within my program. 因此,我要做的是创建几个具有不同内容的“页面”,并能够在程序中的这些页面之间进行切换。

So my question is, what's the best way to do this? 所以我的问题是,做到这一点的最佳方法是什么? Say I want to create a page2 with different images and text, what should I be looking at to make that happen? 假设我想用不同的图像和文本创建一个page2 ,我应该怎么看才能做到这一点?

I hope this is somewhat understandable. 我希望这是可以理解的。 I just need to be pushed in the right direction so I know what to dig into. 我只需要朝正确的方向前进,这样我就知道要深入研究什么。

You may want to use CardLayout . 您可能要使用CardLayout Here is a tutorial how to use CardLayout 这是一个如何使用CardLayout的教程

Example : 范例:

//Where instance variables are declared:
JPanel cards;
final static String BUTTONPANEL = "Card with JButtons";
final static String TEXTPANEL = "Card with JTextField";

//Where the components controlled by the CardLayout are initialized:
//Create the "cards".
JPanel card1 = new JPanel();
...
JPanel card2 = new JPanel();
...

//Create the panel that contains the "cards".
cards = new JPanel(new CardLayout());
cards.add(card1, BUTTONPANEL);
cards.add(card2, TEXTPANEL);

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

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