📄 spring.html
字号:
the spring layout produces a configurationthat gives the first column more or less room,as needed.And as the following figure shows,when the window is resized,the flexibly sized components —the text fields —take all the excess space,while the labels stick to what they need.</p><p><center><IMG SRC="../../figures/uiswing/layout/SpringForm-resized.png" WIDTH="292" HEIGHT="184" ALIGN="BOTTOM" ALT="SpringForm enlarged"></center></p><p>Our last example of the <code>makeCompactGrid</code> method,in<a class="SourceLink" target="_blank" href="examples/SpringBox.java"><code>SpringBox.java</code></a>, shows some buttons configured to be laid out in a single row.You can<b><a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/layout/examples/SpringBox.jnlp">run SpringBox</a></b> using<a class="TutorialLink" target="_top" href="../../information/javawebstart.html">Java Web Start</a>.</p><p><center><IMG SRC="../../figures/uiswing/layout/SpringBox.png" WIDTH="477" HEIGHT="72" ALIGN="BOTTOM" ALT="SpringBox"></center></p><p>Note that the behavioris almost identical to that of <code>BoxLayout</code>in the case of a single row.Not only are the components laid outas <code>BoxLayout</code> would arrange thembut the minimum, preferred, and maximum sizesof the container that uses the <code>SpringLayout</code>return the same results that <code>BoxLayout</code> would.Here is the call to <code>makeCompactGrid</code>that produces this layout:</p><blockquote><pre>//Lay out the buttons in one row and as many columns//as necessary, with 6 pixels of padding all around.SpringUtilities.makeCompactGrid(contentPane, 1, contentPane.getComponentCount(), 6, 6, 6, 6);</pre></blockquote><p>Let's look at what happenswhen we resize this window.This is an odd special casethat's worth taking note ofas you may run into it by accidentin your first layouts.</p><p><center><IMG SRC="../../figures/uiswing/layout/SpringBox-resized.png" WIDTH="517" HEIGHT="116" ALIGN="BOTTOM" ALT="SpringBox resized"></center></p><p>Nothing moved!That抯 because none of the components (buttons)or the spacing between them was defined to be stretchable.In this case the spring layout calculatesa maximum size for the parent containerthat is equal to its preferred size,meaning the parent container itself is not stretchable.It would perhaps be less confusing if the AWTrefused to resize a window that was not stretchable,but it doesn't.The layout manager cannot do anything sensible hereas none of the components will take up the required space.Instead of crashing, it just does nothing,leaving all the components as they were.</p></blockquote><h3><a name="api">The SpringLayout API</a></h3><blockquote>The API for using <code>SpringLayout</code>is spread across three classes:<ul><li> <a href="#SpringLayout"><code>SpringLayout</code><li> <a href="#SpringLayout.Constraints"><code>SpringLayout.Constraints</code><li> <a href="#Spring"><code>Spring</code></ul><p><table border=1><caption><a name="SpringLayout">SpringLayout</a></caption><tr><th>Constructor or Method</th><th>Purpose</th></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/SpringLayout.html#SpringLayout()">SpringLayout()</a></td><td>Create a <code>SpringLayout</code> instance.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/SpringLayout.html#getConstraints(java.awt.Component)">SpringLayout.Constraints getConstraints(Component)</a></td><td>Get the constraints (set of springs)associated with the specified component.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/SpringLayout.html#getConstraint(java.lang.String, java.awt.Component)">Spring getConstraint(String, Component)</a></td><td>Get the spring for an edge of a component.The first argument specifies the edgeand must be one of the following<code>SpringLayout</code> constants:<code>NORTH</code>,<code>SOUTH</code>,<code>EAST</code>, or<code>WEST</code>.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/SpringLayout.html#putConstraint(java.lang.String, java.awt.Component, int, java.lang.String, java.awt.Component)">void putConstraint(String, Component, int, String, Component)</a><br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/SpringLayout.html#putConstraint(java.lang.String, java.awt.Component, javax.swing.Spring, java.lang.String, java.awt.Component)">void putConstraint(String, Component, Spring, String, Component)</a></td><td>Convenience methodsfor defining relationships between the edges of two components.The first two arguments specify the first component and its affected edge.The last two arguments specify the second component and its affected edge.The third argument specifies the spring that determinesthe distance between the two.When the third argument is an integer,a constant spring is created to provide a fixed distancebetween the component edges.</td></tr></table><p><table border=1><caption><a name="SpringLayout.Constraints">SpringLayout.Constraints</a></caption><tr><th>Constructor or Method</th><th>Purpose</th></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/SpringLayout.Constraints.html#SpringLayout.Constraints()">SpringLayout.Constraints()</a><br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/SpringLayout.Constraints.html#SpringLayout.Constraints(javax.swing.Spring, javax.swing.Spring)">SpringLayout.Constraints(Spring, Spring)</a><br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/SpringLayout.Constraints.html#SpringLayout.Constraints(javax.swing.Spring, javax.swing.Spring, javax.swing.Spring, javax.swing.Spring)">SpringLayout.Constraints(Spring, Spring, Spring, Spring)</a></td><td>Create a <code>SpringLayout.Constraints</code> instance.The first two arguments, if present,specify the X and Y springs, respectively.The second two arguments, if present,specify the height and width springs, respectively.Omitting an argument causes the corresponding springto be <code>null</code>,which <code>SpringLayout</code>generally replaces with suitable defaults.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/SpringLayout.Constraints.html#getConstraint(java.lang.String)">Spring getConstraint(String)</a><br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/SpringLayout.Constraints.html#getHeight()">Spring getHeight()</a><br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/SpringLayout.Constraints.html#getWidth()">Spring getWidth()</a><br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/SpringLayout.Constraints.html#getX()">Spring getX()</a><br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/SpringLayout.Constraints.html#getY()">Spring getY()</a><br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/SpringLayout.Constraints.html#setConstraint(java.lang.String, javax.swing.Spring)">void setConstraint(String, Spring)</a><br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/SpringLayout.Constraints.html#setHeight(javax.swing.Spring)">void setHeight(Spring)</a><br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/SpringLayout.Constraints.html#setWidth(javax.swing.Spring)">void setWidth(Spring)</a><br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/SpringLayout.Constraints.html#setX(javax.swing.Spring)">void setX(Spring)</a><br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/SpringLayout.Constraints.html#setY(javax.swing.Spring)">void setY(Spring)</a></td><td>Get or set the specified spring.The string argument to the <code>getConstraint</code>and <code>setConstraint</code> methods specifies an edge name,and must be one of the <code>SpringLayout</code> constants<code>NORTH</code>,<code>SOUTH</code>,<code>EAST</code>, or<code>WEST</code>.</td></tr></table><p><table border=1><caption><a name="Spring">Spring</a></caption><tr><th>Method</th><th>Purpose</th></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/Spring.html#constant(int)">static Spring constant(int)</a><br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/Spring.html#constant(int, int, int)">static Spring constant(int, int, int)</a></td><td>Create a spring that does not track a component's sizes.The three-argument version creates a spring withits minimum, preferred, and maximum values setto the specified values, in that order.The one-argument version creates a spring withits minimum, preferred, and maximum values all setto the specified integer.<p>Despite the name, springs returned by <code>constant</code>are mutable.To make a layout work out, <code>SpringLayout</code> might be forced to adjust a"constant" spring.For this reason, you should avoid reusing constant springsunless (1) you truly want the springs to always be precisely alikeand (2) other springs providesome flexibility in the layout.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/Spring.html#sum(javax.swing.Spring, javax.swing.Spring)">static Spring sum(Spring, Spring)</a><br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/Spring.html#max(javax.swing.Spring, javax.swing.Spring)">static Spring max(Spring, Spring)</a><br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/Spring.html#minus(javax.swing.Spring)">static Spring minus(Spring)</a></td><td>Create a spring that is the result of some mathematical manipulation.The <code>sum</code> method adds two springs.The <code>max</code> methodreturns a spring whose value is alwaysgreater than or equal to the values of the two arguments.The <code>minus</code> methodreturns a spring running in the opposite direction of the argument.The <code>minus</code> method can be used to createan argument for the <code>sum</code> method,allowing you to get the difference between two springs.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/Spring.html#getMinimumValue()">int getMinimumValue()</a><br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/Spring.html#getPreferredValue()">int getPreferredValue()</a><br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/Spring.html#getMaximumValue()">int getMaximumValue()</a></td><td>Get the corresponding value from the spring.For a <code>SpringLayout</code>-created springthat automatically tracks a component,these methods result in calls to the component'scorresponding <code>get<em>Xxx</em>Size</code> method.</td><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/Spring.html#getValue()">int getValue()</a><br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/Spring.html#setValue(int)">setValue(int)</a></td><td>Get or set the spring's current value.</td></tr></table></blockquote><h3><a name="eg">Examples that Use SpringLayout</a></h3><blockquote>The following table lists someexamples that use spring layout.<p><table><tr><th align=left> Example</th><th align=left> Where Described</th><th align=left> Notes</th></tr><tr><td valign=top> <a href="examples/index.html#SpringDemo3"><code>SpringDemo3</code></a></td><td valign=top> This page</td><td valign=top> Uses <code>SpringLayout</code> to create a row of evenly spaced, natural-size components.</td></tr><tr><td valign=top> <a href="examples/index.html#SpringDemo4"><code>SpringDemo4</code></a></td><td valign=top> This page</td><td valign=top> Reimplements SpringDemo3 to use <code>SpringLayout.Constraints</code> and <code>Spring</code> directly.</td></tr><tr><td valign=top> <a href="examples/index.html#SpringGrid"><code>SpringGrid</code></a></td><td valign=top> This page</td><td valign=top> Uses <code>SpringLayout</code> and the <code>makeGrid</code> utility method to create a layout where all the components are the same size.</td></tr><tr><td valign=top> <a href="examples/index.html#SpringCompactGrid"><code>SpringCompactGrid</code></a></td><td valign=top> This page</td><td valign=top> Uses <code>SpringLayout</code> and the <code>makeCompactGrid</code> utility method to create a layout where all the components in a row have the same height, and all components in a column have the same width.</tr><tr><td valign=top> <a href="examples/index.html#SpringForm"><code>SpringForm</code></a></td><td valign=top> This page</td><td valign=top> Uses <code>SpringLayout</code> and <code>makeCompactGrid</code> to align label-text field pairs.</td></tr><tr><td valign=top> <a href="examples/index.html#SpringBox"><code>SpringBox</code></a></td><td valign=top> This page</td><td valign=top> Uses <code>SpringLayout</code> and <code>makeCompactGrid</code> to demonstrate laying out a single row of components, and what happens when no springs can grow.</td></tr><tr><td valign=top><a class="TutorialLink" target="_top" href="../components/examples/index.html#SpinnerDemo"><code>SpinnerDemo</code></a></td><td valign=top><a class="TutorialLink" target="_top" href="../components/spinner.html">How to Use Spinners</a></td><td valign=top> Uses <code>SpringLayout</code> and <code>makeCompactGrid</code> to lay out rows of label-spinner pairs.</td></tr><tr><td valign=top><a class="TutorialLink" target="_top" href="../components/examples/index.html#TextInputDemo"><code>TextInputDemo</code></a></td><td valign=top><a class="TutorialLink" target="_top" href="../components/formattedtextfield.html">How to Use Formatted Text Fields</a></td><td valign=top> Uses <code>SpringLayout</code> and <code>makeCompactGrid</code> to lay out rows of labeled components. The components are a mix of text fields, formatted text fields, and spinners.</td></tr></table> </blockquote> <div class=NavBit> <a target=_top href=groupExample.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=custom.html>Next »</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> A GroupLayout Example <br><b>Next page:</b> Creating a Custom Layout Manager </div> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -