⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 card.html

📁 jsf、swing的官方指南
💻 HTML
📖 第 1 页 / 共 2 页
字号:
    </script>    </head><body onload="load()">    <div id=TopBar> <div id=TopBar_tr> <div id=TopBar_tl> <div id=TopBar_br> <div id=TopBar_bl>                         <div id=TopBar_right>                             <a target="_blank"                                href="http://java.sun.com/javase/6/download.jsp">Download                                the JDK</a>                            <br>                            <a href="../../search.html" target="_blank">Search the                                Tutorials</a>                            <br>                            <a href="javascript:toggleLeft()"                                id="ToggleLeft">Hide the TOC</a>                        </div>                    </div> </div> </div> </div> </div>    <div class=PrintHeaders>        <b>Trail:</b> Creating a GUI with JFC/Swing        <br><b>Lesson:</b> Laying Out Components Within a Container    </div>    <div id=LeftBar class=LeftBar_shown>        <div id=Contents>            <div class="linkLESSON"><a href="index.html">Laying Out Components Within a Container</a></div><div class="linkAHEAD"><a href="visual.html">A Visual Guide to Layout Managers</a></div><div class="linkAHEAD"><a href="using.html">Using Layout Managers</a></div><div class="linkAHEAD"><a href="howLayoutWorks.html">How Layout Management Works</a></div><div class="linkAHEAD"><a href="layoutlist.html">How to Use Various Layout Managers</a></div><div class="linkAHEAD"><a href="border.html">How to Use BorderLayout</a></div><div class="linkAHEAD"><a href="box.html">How to Use BoxLayout</a></div><div class="nolinkAHEAD">How to Use CardLayout</div><div class="linkAHEAD"><a href="flow.html">How to Use FlowLayout</a></div><div class="linkAHEAD"><a href="gridbag.html">How to Use GridBagLayout</a></div><div class="linkAHEAD"><a href="grid.html">How to Use GridLayout</a></div><div class="linkAHEAD"><a href="group.html">How to Use GroupLayout</a></div><div class="linkBHEAD"><a href="groupExample.html">A GroupLayout Example</a></div><div class="linkAHEAD"><a href="spring.html">How to Use SpringLayout</a></div><div class="linkAHEAD"><a href="custom.html">Creating a Custom Layout Manager</a></div><div class="linkAHEAD"><a href="none.html">Doing Without a Layout Manager (Absolute Positioning)</a></div><div class="linkAHEAD"><a href="problems.html">Solving Common Layout Problems</a></div></div>    </div>    <div id=MainFlow class=MainFlow_indented>            <span id=BreadCrumbs>                <a href=../../index.html target=_top>Home Page</a>                &gt;                <a href=../index.html target=_top>Creating a GUI with JFC/Swing</a>                &gt;                <a href=index.html target=_top>Laying Out Components Within a Container</a>            </span>            <div class=NavBit>                <a target=_top href=box.html>&laquo;&nbsp;Previous</a>&nbsp;&bull;&nbsp;<a target=_top href=../TOC.html>Trail</a>&nbsp;&bull;&nbsp;<a target=_top href=flow.html>Next&nbsp;&raquo;</a>            </div>            <div id=PageTitle>How to Use CardLayout</div>            <blockquote>Here's a snapshot of an application that uses a<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/CardLayout.html"><code>CardLayout</code></a> to switch between two panels.<p align=center><IMG SRC="../../figures/uiswing/layout/CardLayoutDemo.png" WIDTH="265" HEIGHT="105 " ALT="A snapshot of CardLayoutDemo"><IMG SRC="../../figures/uiswing/layout/CardLayoutDemo-2.png" WIDTH="265" HEIGHT="105 " ALT="Another snapshot of CardLayoutDemo"><p>You can<a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/layout/examples/CardLayoutDemo.jnlp">run CardLayoutDemo</a>using<a class="TutorialLink" target="_top" href="../../information/javawebstart.html">Java<sup><font size=-2>TM</font></sup> Web Start</a>.Its code is in<a class="SourceLink" target="_blank" href="examples/CardLayoutDemo.java"><code>CardLayoutDemo.java</code></a>.<p>The <code>CardLayout</code> class helps you manage two or more components(usually <code>JPanel</code> instances)that share the same display space.When using <code>CardLayout</code>,you need to provide a way to let the user choose between the components.CardLayoutDemouses a combo box for this purpose.<p>An easierbut less flexible way to accomplish the same task is to use a<a class="TutorialLink" target="_top" href="../components/tabbedpane.html">tabbed pane</a>.Here's a picture of a tabbed pane version of the preceding example:<p><center><IMG SRC="../../figures/uiswing/layout/TabDemo.png" WIDTH="370" HEIGHT="98" ALIGN="BOTTOM" ALT="A snapshot of TabDemo"></center></p><p>Because a tabbed pane provides its own GUI,using a tabbed pane is simpler than using <code>CardLayout</code>.For example, reimplementing the preceding example to use a tabbed pane results in a program with fewer lines of code.You can<a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/layout/examples/TabDemo.jnlp">run TabDemo</a>using<a class="TutorialLink" target="_top" href="../../information/javawebstart.html">Java Web Start</a>,and find its code in<a class="SourceLink" target="_blank" href="examples/TabDemo.java"><code>TabDemo.java</code></a>.<p>Conceptually, each component a <code>CardLayout</code> managesis like a playing card or trading card in a stack,where only the top card is visible at any time.You can choose the card that's showingin any of the following ways:<ul><li> By asking for either the first or last card,     in the order it was added to the container.<li> By flipping through the deck backwards or forwards.<li> By specifying a card with a specific name.     This is the scheme CardLayoutDemo uses.</ul><p>The following code from<a class="SourceLink" target="_blank" href="examples/CardLayoutDemo.java"><code>CardLayoutDemo.java</code></a> creates the <code>CardLayout</code>and the components it manages.<blockquote><pre><em>//Where instance variables are declared:</em>JPanel cards;final static String BUTTONPANEL = "JPanel with JButtons";final static String TEXTPANEL = "JPanel with JTextField";<em>//Where the components controlled by the CardLayout are initialized:</em>//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);</pre></blockquote>When you add a component to acontainer that a <code>CardLayout</code> manages,you must specify a string that identifies the component being added.For example, in this example, the first panel has the string <code>"JPanel with JButtons"</code>,and the second panel has the string <code>"JPanel with JTextField"</code>.In this example,those strings are also used in the combo box.<p>To choose which component a <code>CardLayout</code> shows,you need some additional code.Here's how the example program does this:<blockquote><pre><em>//Where the GUI is assembled:</em>//Put the JComboBox in a JPanel to get a nicer look.JPanel comboBoxPane = new JPanel(); //use FlowLayoutString comboBoxItems[] = { BUTTONPANEL, TEXTPANEL };JComboBox cb = new JComboBox(comboBoxItems);cb.setEditable(false);cb.addItemListener(this);comboBoxPane.add(cb);...pane.add(comboBoxPane, BorderLayout.PAGE_START);pane.add(cards, BorderLayout.CENTER);...public void itemStateChanged(ItemEvent evt) {    CardLayout cl = (CardLayout)(cards.getLayout());    cl.show(cards, (String)evt.getItem());}</pre></blockquote>This example shows thatyou can use the <code>CardLayout</code> <code>show</code>method to set the currently showing component. The first argument to the <code>show</code> methodis the container the <code>CardLayout</code> controls &#151;that is, the container of the components the <code>CardLayout</code> manages.The second argument is the stringthat identifies the component to show.This string is the same as was used whenadding the component to the container.</blockquote><h3><a name="api">The CardLayout API</a></h3><blockquote>[PENDING: This section will be converted to use the sameAPI table format as is used in the components lesson.]<p>The following <code>CardLayout</code> methods let you choose a component.For each method, the first argument is the containerfor which the <code>CardLayout</code> is the layout manager(the container of the cards the <code>CardLayout</code> controls).<blockquote><pre>void first(Container)void next(Container)void previous(Container)void last(Container)void show(Container, String)</pre></blockquote></blockquote><h3><a name="eg">Examples that Use CardLayout</a></h3><blockquote>Only one example in this trailuses <code>CardLayout</code>:<a href="examples/index.html#CardLayoutDemo"><code>CardLayoutDemo</code></a>.Generally, our examples use <a class="TutorialLink" target="_top" href="../components/tabbedpane.html">tabbed panes</a> instead of <code>CardLayout</code>,since tabbed panes conveniently provide a nice GUI for the same functionality.        </blockquote>        <div class=NavBit>            <a target=_top href=box.html>&laquo; Previous</a>            &bull;            <a target=_top href=../TOC.html>Trail</a>            &bull;            <a target=_top href=flow.html>Next &raquo;</a>        </div>    </div>    <div id=Footer><div id=TagNotes>    Problems with the examples? Try <a target="_blank"        href=../../information/run-examples.html>Compiling and Running        the Examples: FAQs</a>.    <br>    Complaints? Compliments? Suggestions? <a target="_blank"        href="http://developer.sun.com/contact/tutorial_feedback.jsp">Give    us your feedback</a>.<br><br>    <a target="_blank" href="../../information/copyright.html">Copyright</a>    1995-2006 Sun Microsystems, Inc.  All rights reserved.    <span id=Download></span></div>     </div>    <div class=PrintHeaders>        <b>Previous page:</b> How to Use BoxLayout        <br><b>Next page:</b> How to Use FlowLayout    </div>    </body></html> 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -