📄 gridbag.html
字号:
pane.add(button, c);button = new JButton("5");c.ipady = 0; //reset to defaultc.weighty = 1.0; //request any extra vertical spacec.anchor = GridBagConstraints.PAGE_END; //bottom of spacec.insets = new Insets(10,0,0,0); //top paddingc.gridx = 1; //aligned with button 2c.gridwidth = 2; //2 columns widec.gridy = 2; //third rowpane.add(button, c);</pre></blockquote><p>This example uses one <code>GridBagConstraints</code> instancefor all the components the <code>GridBagLayout</code> manages.Just before each component is added to the container,the code sets (or resets to default values)the appropriate instance variablesin the <code>GridBagConstraints</code> object.It then adds the component to its container,specifying the <code>GridBagConstraints</code> objectas the second argument to the <code>add</code> method.<p>For example,to make button 4 be extra tall,the example has this code:<blockquote><pre>c.ipady = 40;</pre></blockquote>And before setting the constraints of the next component,the code resets the value of <code>ipady</code>to the default:<blockquote><pre>c.ipady = 0;</pre></blockquote><blockquote><hr><strong>Note:</strong> The Tutorial's examplesused to specify the constraints objecta different way,which you might see in other programs as well.Rather than specifying the constraints with the <code>add</code> method,our examples invokedthe <code>setConstraints</code> methodon the <code>GridBagLayout</code> object. For example:<blockquote><pre>GridBagLayout gridbag = new GridBagLayout();pane.setLayout(gridbag);...gridbag.setConstraints(button, c);pane.add(button);</pre></blockquote><hr></blockquote>Here's a table that shows all the constraintsfor each component in GridBagLayoutDemo's content pane.Values that aren't the defaultare marked in <b>boldface</b>.Values that are different from those in the previous table entryare marked in <i>italics</i>.<p><table><tr><th align=left> Component</th><th align=left> Constraints</th></tr><tr><td valign=top>All components</td><td valign=top><pre>ipadx = 0<b>fill = GridBagConstraints.HORIZONTAL</b></td></tr><tr><td valign=top>Button 1</td><td valign=top><pre>ipady = 0<b>weightx = 0.5</b>weighty = 0.0gridwidth = 1anchor = GridBagConstraints.CENTERinsets = new Insets(0,0,0,0)<b>gridx = 0</b><b>gridy = 0</b></td></tr><tr><td valign=top>Button 2</td><td valign=top><pre><b>weightx = 0.5</b><i><b>gridx = 1</b></i><b>gridy = 0</b></pre></td></tr><tr><td valign=top>Button 3</td><td valign=top><pre><b>weightx = 0.5</b><i><b>gridx = 2</b></i><b>gridy = 0</b></pre></td></tr><tr><td valign=top>Button 4</td><td valign=top><pre><i><b>ipady = 40</b></i><i>weightx = 0.0</i><i><b>gridwidth = 3</b></i><i><b>gridx = 0</b></i><i><b>gridy = 1</b></i></pre></td></tr><tr><td valign=top>Button 5</td><td valign=top><pre><i>ipady = 0</i>weightx = 0.0<i><b>weighty = 1.0</b></i><i><b>anchor = GridBagConstraints.SOUTH</b></i><i><b>insets = new Insets(10,0,0,0)</b></i><i><b>gridwidth = 2</b></i><i><b>gridx = 1</b></i><i><b>gridy = 2</b></i></pre></td></tr></table><p>GridBagLayoutDemo has two componentsthat span multiple columns(buttons 4 and 5).To make button 4 tall,we added internal padding (<code>ipady</code>) to it.To put space between buttons 4 and 5,we used insets to add a minimum of 10 pixels above button 5,and we made button 5 hug the southedge of its cell.<p>All the components in the <code>pane</code> containerare as wide as possible,given the cells that they occupy.The program accomplishesthis by setting the <code>GridBagConstraints</code><code>fill</code> instance variableto <code>GridBagConstraints.HORIZONTAL</code>,leaving it at that setting for all the components.If the program didn't specify the fill,the buttons would be at their natural width, like this:<p><center><IMG SRC="../../figures/uiswing/layout/GridBagLayoutDemo-nofill.png" WIDTH="253" HEIGHT="161" ALIGN="BOTTOM" ALT="GridBagLayoutDemo with default fill values."></center></p><p>When you enlarge GridBagLayoutDemo's window,the columns grow proportionately.This is becauseeach component in the first row,where each component is one column wide,has <code>weightx = 1.0</code>.The actual value of these components' <code>weightx</code> is unimportant.What matters is that all the components,and consequently, all the columns,have an equal weightthat is greater than 0.If no component managed by the <code>GridBagLayout</code>had <code>weightx</code> set,then when the components' container was made wider,the components would stay clumped together in the center of the container,like this:<p><center><IMG SRC="../../figures/uiswing/layout/GridBagLayoutDemo-noweightx.png" WIDTH="320" HEIGHT="161" ALIGN="BOTTOM" ALT="GridBagLayoutDemo with default weightx values and enlarged by the user."></center></p><p>Note that if you enlarge the window,the last row is the only one that gets taller.This is because only button 5 has <code>weighty</code>greater than zero.</blockquote><h3><a name="api">The GridBagLayout API</a></h3><blockquote>[PENDING: This section will be changed to use standard API tables.]<p>The <code>GridBagLayout</code> and <code>GridBagConstraints</code>classes each have only one constructor,with no arguments.Instead of invoking methods on a <code>GridBagConstraints</code> object,you manipulate its instance variables,as described in <a href="#gridbagConstraints">Specifying Constraints</a>.Generally, the only method you invoke on a <code>GridBagLayout</code> objectis <code>setConstraints</code>,as demonstrated in <a href="#gridbagExample">The Example Explained</a>.</blockquote><h3><a name="eg">Examples that Use GridBagLayout</a></h3><blockquote>You can find examples of using <code>GridBagLayout</code>throughout this tutorial.The following table lists a few.<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#GridBagLayoutDemo"><code>GridBagLayoutDemo</code></a></td><td valign=top> This section</td><td valign=top> Uses many features — weights, insets, internal padding, horizontal fill, exact cell positioning, multi-column cells, and anchoring (component positioning within a cell).</td></tr><tr><td valign=top> <a class="TutorialLink" target="_top" href="../components/examples/index.html#TextSamplerDemo"><code>TextSamplerDemo</code></a></td><td valign=top> <a class="TutorialLink" target="_top" href="../components/text.html">Using Text Components</a></td><td valign=top> Aligns two pairs of labels and text fields, plus adds a label across the full width of the container.</td></tr><tr><td valign=top> <a class="TutorialLink" target="_top" href="../events/examples/index.html#ContainerEventDemo"><code>ContainerEventDemo</code></a></td><td valign=top><a class="TutorialLink" target="_top" href="../events/containerlistener.html">How to Write a Container Listener</a></td><td valign=top> Positions five components within a container, using weights, fill, and relative positioning.</td></tr></table> </blockquote> <div class=NavBit> <a target=_top href=flow.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=grid.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> How to Use FlowLayout <br><b>Next page:</b> How to Use GridLayout </div> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -