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

📄 separator.html

📁 jsf、swing的官方指南
💻 HTML
📖 第 1 页 / 共 2 页
字号:
is extremely simple, boiling down to something like this:<blockquote><pre>menu.add(menuItem1);menu.add(menuItem2);menu.add(menuItem3);<b>menu.addSeparator();</b>menu.add(rbMenuItem1);menu.add(rbMenuItem2);<b>menu.addSeparator();</b>menu.add(cbMenuItem1);menu.add(cbMenuItem2);<b>menu.addSeparator();</b>menu.add(submenu);</pre></blockquote><p>Adding separators to a tool bar is similar.You can find the full code explained in thehow-to sections for<a href="menu.html">menus</a>and <a href="toolbar.html">tool bars</a>.If you want more control over separators in menus and tool bars,you can directly use the <code>JSeparator</code> subclasses that implement them:<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JPopupMenu.Separator.html">JPopupMenu.Separator</a> and<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JToolBar.Separator.html">JToolBar.Separator</a>.In particular, <code>JToolBar.Separator</code>has API for specifying the separator's size.</blockquote><h3><a name="elsewhere">Using JSeparator</a></h3><blockquote><p>You can use the <code>JSeparator</code> class directlyto provide a dividing line in any container.The following picture shows a GUIthat has a separator to the right of the buttonlabeled Fire.<p><center><IMG SRC="../../figures/uiswing/components/ListDemoMetal.png" WIDTH="256" HEIGHT="163" ALIGN="BOTTOM" ALT="A snapshot of ListDemo"></center></p><p>Separators have almost no API and are extremely easy to useas long as you keep one thing in mind:In most implementations,a vertical separator has a preferred height of 0,and a horizontal separator has a preferred width of 0.This means a separator <b>is not visible</b>unless you either set its preferred sizeor put it in under the control of a layout managersuch as <code>BorderLayout</code> or <code>BoxLayout</code>that stretches it to fill its available display area.<p>The vertical separator does have a bit of width(and the horizontal a bit of height),so you should see some space where the separator is.However, the actual dividing line isn't drawnunless the width and height are both non-zero.<p>The following code snippetshows how ListDemo puts together the panel that contains the vertical separator.You can find the full source code for ListDemo in<a class="SourceLink" target="_blank" href="examples/ListDemo.java"><code>ListDemo.java</code></a>.<blockquote><pre>JPanel buttonPane = new JPanel();buttonPane.setLayout(new BoxLayout(buttonPane,                                   BoxLayout.LINE_AXIS));buttonPane.add(fireButton);buttonPane.add(Box.createHorizontalStrut(5));<b>buttonPane.add(new JSeparator(SwingConstants.VERTICAL));</b>buttonPane.add(Box.createHorizontalStrut(5));buttonPane.add(employeeName);buttonPane.add(hireButton);buttonPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));</pre></blockquote>As the code shows,the buttons, separator, and text field all share a single container &#151;a <code>JPanel</code> instancethat uses a left-to-right <a class="TutorialLink" target="_top" href="../layout/box.html">box layout</a>.Thanks to the layout manager(and to the fact that separators have unlimited maximum sizes),the separator is automatically made as tallas its available display area.<p>In the preceding code,the horizontal struts are invisible components used to provide spacearound the separator.A 5-pixel empty borderprovides a cushion around the panel, and also serves to prevent the separator from extendingall the way to the component above itand the window's edge below it.<p>Here's a picture of another GUIthat uses a separator,this time to put a dividing linebetween a group of controls and a display area.<p><center><IMG SRC="../../figures/uiswing/components/TextInputDemo.png" WIDTH="549" HEIGHT="185" ALIGN="BOTTOM" ALT="A snapshot of TextInputDemo"></center></p><p>You can find the code in the<a href="examples/index.html#TextInputDemo">example index</a>.Here is the code that sets up the separator's container:<blockquote><pre>JPanel panel = new JPanel(new BorderLayout());...panel.setBorder(BorderFactory.createEmptyBorder(                        GAP/2, //top                        0,     //left                        GAP/2, //bottom                        0));   //rightpanel.add(new JSeparator(JSeparator.VERTICAL),          BorderLayout.LINE_START);panel.add(addressDisplay,          BorderLayout.CENTER);</pre></blockquote>As in the last example, the panel uses an empty borderso that the separator doesn'textend all the way to the edges of its container.Placing the separator in the leftmost area of the <code>BorderLayout</code>-controlled containermakes the separator as tall as the address-display componentthat's in the center of the container.See <a class="TutorialLink" target="_top" href="../layout/border.html">How to Use BorderLayout</a> for details on how border layouts work.</blockquote><h3><a name="api">The Separator API</a></h3><blockquote>The API for using separators is minimal,since they have no contentsand don't respond to user input.<p><table border=1><caption><a name="creating">Creating and Initializing Separators</a></caption><tr><th align=left>Constructor or Method</th><th align=left>Purpose</th></tr>  <tr>    <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JToolBar.html#addSeparator()">void addSeparator()</a><br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JToolBar.html#addSeparator(java.awt.Dimension)">void addSeparator(Dimension)</a>    <br><em>(in <code>JToolBar</code>)</em>    </td>    <td>Append a tool bar separator        (which is invisible in most,	if not all, look and feels)	to the current end of the tool bar.	The optional argument 	specifies the size of the separator.	The no-argument version of this method	uses a separator with a default size,	as determined by the current look and feel.    </td>  </tr>  <tr>    <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JMenu.html#addSeparator()">void addSeparator()</a><br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JMenu.html#insertSeparator(int)">void insertSeparator(int)</a>    <br><em>(in <code>JMenu</code>)</em>    </td>    <td>Put a separator in the menu.	The <code>addSeparator</code> method	puts the separator at the current end of the menu.	The <code>insertSeparator</code> method	inserts the separator into the menu	at the specified position.    </td>  </tr>  <tr>    <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JPopupMenu.html#addSeparator()">void addSeparator()</a><br><em>(in <code>JPopupMenu</code>)</em>    </td>    <td>Put a separator at the current end of the popup menu.    </td>  </tr>  <tr>    <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JSeparator.html#JSeparator()">JSeparator()</a>    <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JSeparator.html#JSeparator(int)">JSeparator(int)</a>    </td>    <td>Create a separator.	If you don't specify an argument,	the separator is horizontal.	The argument can be either	<code>SwingConstants.HORIZONTAL</code> or	<code>SwingConstants.VERTICAL</code>.    </td>  </tr>  <tr>    <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JSeparator.html#setOrientation(int)">void setOrientation(int)</a>    <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JSeparator.html#getOrientation()">int getOrientation()</a><br><em>(in <code>JSeparator</code>)</em>    </td>    <td>Get or set the separator's orientation,	which can be either	<code>SwingConstants.HORIZONTAL</code> or	<code>SwingConstants.VERTICAL</code>.    </td>  </tr>  <tr>    <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JToolBar.Separator.html#JToolBar.Separator()">JToolBar.Separator()</a><br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JToolBar.Separator.html#JToolBar.Separator(java.awt.Dimension)">JToolBar.Separator(Dimension)</a>    </td>    <td>    Create a separator for use in a tool bar.    The optional argument specifies    the separator's size.    </td>  </tr>  <tr>    <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JToolBar.Separator.html#setSeparatorSize(java.awt.Dimension)">setSeparatorSize(Dimension)</a><br><em>(in <code>JToolBar.Separator</code>)</em>    </td>    <td>    Specify the separator's size.    More specifically,    the specified <code>Dimension</code>    is used as the separator's minimum, preferred, and maximum sizes.    </td>  </tr>  <tr>    <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JPopupMenu.Separator.html#JPopupMenu.Separator()">JPopupMenu.Separator()</a>    </td>    <td>    Create a separator for use in a menu.    </td>  </tr></table></blockquote><h3><a name="eg">Examples that Use Separators</a></h3><blockquote>Several of this lesson's examples use separators,usually in menus.Here is a list of some of the more interesting examples.<p><table><tr><th align=left> Example</th><th align=left> Where Described</th><th align=left> Notes</th></tr><tr><td> <a href="examples/index.html#ListDemo"><code>ListDemo</code></a></td><td> This section and<a href="list.html">How to Use Lists</a></td><td> Uses a vertical separator in a panelcontrolled by a horizontal box layout.</td></tr><tr><td> <a href="examples/index.html#TextInputDemo"><code>TextInputDemo</code></a></td><td> This section and<a href="formattedtextfield.html">How to Use Formatted Text Fields</a></td><td> Uses a vertical separator at the left of a panelcontrolled by a border layout.</td></tr><tr><td> <a href="examples/index.html#MenuDemo"><code>MenuDemo</code></a></td><td> This section and<a href="menu.html">How to Use Menus</a></td><td> Uses the <code>JMenu</code> method <code>addSeparator</code>     to put separators in a menu.</td></tr><tr><td> <a href="examples/index.html#ToolBarDemo2"><code>ToolBarDemo2</code></a></td><td> <a href="toolbar.html">How to Use Tool Bars</a></td><td> Uses the <code>JToolBar</code> method <code>addSeparator</code>     to put space     between two kinds of buttons.</td></tr></table>        </blockquote>        <div class=NavBit>            <a target=_top href=scrollpane.html>&laquo; Previous</a>            &bull;            <a target=_top href=../TOC.html>Trail</a>            &bull;            <a target=_top href=slider.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 Scroll Panes        <br><b>Next page:</b> How to Use Sliders    </div>    </body></html> 

⌨️ 快捷键说明

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