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

📄 custom.html

📁 jsf、swing的官方指南
💻 HTML
📖 第 1 页 / 共 2 页
字号:
                            <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="linkAHEAD"><a href="card.html">How to Use CardLayout</a></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="nolinkAHEAD">Creating a Custom Layout Manager</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=spring.html>&laquo;&nbsp;Previous</a>&nbsp;&bull;&nbsp;<a target=_top href=../TOC.html>Trail</a>&nbsp;&bull;&nbsp;<a target=_top href=none.html>Next&nbsp;&raquo;</a>            </div>            <div id=PageTitle>Creating a Custom Layout Manager</div>            <blockquote><blockquote><hr><strong>Note:</strong>&nbsp;Before you start creating a custom layout manager,make sure that no existing layout manager will work.In particular, layout managers such as<a href=gridbag.html><code>GridBagLayout</code></a>,<a href=spring.html><code>SpringLayout</code></a>, and<a href=box.html><code>BoxLayout</code></a>are flexible enough to work in many cases.You can also find layout managers from other sources,such as from the Internet.Finally, you can simplify layoutby grouping components into containerssuch as invisible <a class="TutorialLink" target="_top" href="../components/panel.html">panels</a>.<hr></blockquote><p>To create a custom layout manager,you must create a class that implements the<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/LayoutManager.html"><code>LayoutManager</code></a> interface.You can either implement it directly,or implement its subinterface, <a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/LayoutManager2.html"><code>LayoutManager2</code></a>.<p>Every layout manager must implement at leastthe following five methods, which are required by the <code>LayoutManager</code> interface:<p><dl><dt><strong><code>void addLayoutComponent(String, Component)</code></strong><dd>Called by the <code>Container</code> <code>add</code> methods.    Layout managers that don't associate strings with their components     generally do nothing in this method.    <p><dt><strong><code>void removeLayoutComponent(Component)</code></strong><dd>Called by the <code>Container</code> <code>remove</code> and    <code>removeAll</code> methods.    Many layout managers     do nothing in this method,    relying instead on querying the container for its components,    using the <code>Container</code> method<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/Container.html#getComponents()"><code>getComponents</code></a>.    <p><dt><strong><code>Dimension preferredLayoutSize(Container)</code></strong><dd>Called by the <code>Container</code> <code>getPreferredSize</code> method,    which is itself called under a variety of circumstances.    This method should calculate and return the ideal size of the container,    assuming that the components it contains will be at or above    their preferred sizes.    This method must take into account the container's internal borders,    which are returned by the<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/Container.html#getInsets()"><code>getInsets</code></a>    method.    <p><dt><strong><code>Dimension minimumLayoutSize(Container)</code></strong><dd>Called by the <code>Container</code> <code>getMinimumSize</code> method,    which is itself called under a variety of circumstances.    This method should calculate and return the minimum size of the container,    assuming that the components it contains     will be at or above their minimum sizes.    This method must take into account the container's internal borders,    which are returned by the     <code>getInsets</code> method.    <p><dt><strong><code>void layoutContainer(Container)</code></strong><dd>Called when the container is first displayed,    and each time its size changes.    A layout manager's <code>layoutContainer</code> method    doesn't actually draw components.    It simply invokes each component's     <code>setSize</code>,    <code>setLocation</code>,    and    <code>setBounds</code>    methods to set the component's size and position.<p>    This method must take into account the container's internal borders,    which are returned by the     <code>getInsets</code> method.    If appropriate,    it should also take the container's orientation     (returned by the <a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/Component.html#getComponentOrientation()"><code>getComponentOrientation</code></a>    method)    into account.    You can't assume that the <code>preferredLayoutSize</code>    or <code>minimumLayoutSize</code> method will be called    before <code>layoutContainer</code> is called.</dl><p>Besides implementing the preceding five methods,layout managers generally implement at least one public constructorand the <code>toString</code>method.<p>If you wish to support componentconstraints, maximum sizes, or alignment,then your layout manager should implement the<code>LayoutManager2</code> interface.That interface adds five methods to thoserequired by <code>LayoutManager</code>:<ul><li> <code>addLayoutComponent(Component, Object)</code><li> <code>getLayoutAlignmentX(Container)</code><li> <code>getLayoutAlignmentY(Container)</code><li> <code>invalidateLayout(Container)</code><li> <code>maximumLayoutSize(Container)</code></ul>For more information about these methods,see the <a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/LayoutManager2.html"><code>LayoutManager2</code> API documentation</a>.<p>When implementing a layout manager,you might want to use <a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/SizeRequirements.html"><code>SizeRequirements</code></a> objects to help you determinethe size and position of components.See the source code for <code>BoxLayout</code>for an example of using <code>SizeRequirements</code>.<p>The example CustomLayoutDemouses a custom layout manager called <code>DiagonalLayout</code>.You can find the layout manager's source code in<a class="SourceLink" target="_blank" href="examples/DiagonalLayout.java"><code>DiagonalLayout.java</code></a>.<code>DialogLayout</code> lays out components diagonally, from left to right, with one component per row.Here's a picture of CustomLayoutDemo using<code>DialogLayout</code> to lay out five buttons.<p><p><center><IMG SRC="../../figures/uiswing/layout/CustomLayoutDemo.png" WIDTH="243" HEIGHT="184" ALIGN="BOTTOM" ALT="A snapshot of CustomLayoutDemo"></center></p><p>You can<a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/layout/examples/CustomLayoutDemo.jnlp">run CustomLayoutDemo</a>using<a class="TutorialLink" target="_top" href="../../information/javawebstart.html">Java<sup><font size=-2>TM</font></sup> Web Start</a>.Its complete source code is in the<a href="examples/index.html#GraphPaperTest">example index</a>.<p>Another example of a custom layout manager is<code>GraphPaperLayout</code>,which implements <code>LayoutManager2</code>and lays out components in a grid.You can find its source code in<a class="SourceLink" target="_blank" href="examples/GraphPaperLayout.java"><code>GraphPaperLayout.java</code></a>.Here is a picture of a rough demo called GraphPaperTestthat uses <code>GraphPaperLayout</code>:<p><center><IMG SRC="../../figures/uiswing/layout/GraphPaperTest.png" WIDTH="213" HEIGHT="164" ALIGN="BOTTOM" ALT="A snapshot of GraphPaperTest"></center></p><p>When a container uses <code>GraphPaperLayout</code>,the size and location of its child components are specified(using grid units rather than absolute locations)as the components are addedto the container.You can set the relative grid size,horizontal space between components, andvertical space between componentswhen initializing the layout manager.You can also change component locations and the grid size dynamically.<p>You can <a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/layout/examples/GraphPaperTest.jnlp">run GraphPaperTest</a>using<a class="TutorialLink" target="_top" href="../../information/javawebstart.html">Java<sup><font size=-2>TM</font></sup> Web Start</a>.You can find its complete source code in the<a href="examples/index.html#GraphPaperTest">example index</a>.        </blockquote>        <div class=NavBit>            <a target=_top href=spring.html>&laquo; Previous</a>            &bull;            <a target=_top href=../TOC.html>Trail</a>            &bull;            <a target=_top href=none.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 SpringLayout        <br><b>Next page:</b> Doing Without a Layout Manager (Absolute Positioning)    </div>    </body></html> 

⌨️ 快捷键说明

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