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

📄 toolbar.html

📁 jsf、swing的官方指南
💻 HTML
📖 第 1 页 / 共 2 页
字号:
        add(toolBar, BorderLayout.PAGE_START);        add(scrollPane, BorderLayout.CENTER);    }    ...}</pre></blockquote>The code positions the tool bar above the scroll paneby placing both componentsin a panel controlled by a border layout,with the tool bar in the <code>PAGE_START</code> position and the scroll pane in the <code>CENTER</code> position.Because the scroll pane is in the centerand no other components except the tool bar are in the container,the tool bar is automatically draggableto other edges of the container.The tool bar can also be dragged out into its own window,in which case the windowhas the title "Still draggable",as specified with the <code>JToolBar</code> constructor.<blockquote><hr><strong>Note:</strong>&nbsp;The tool bar's window is a <code>JDialog</code>.This demo uses <code>JDialog.setDefaultLookAndFeelDecorated(true)</code>to make the window's decorations be painted by the look and feel,which by default is the Java look and feel.Unfortunately,bug #<a class="OutsideLink" target="_blank" href="http://developer.java.sun.com/developer/bugParade/bugs/4820659.html">4820659</a> prevents the user from using the mouse to close dialogs decorated by the Java look and feel.You might consider not using dialog decorationsfrom the Java look and feel until the fix for this bug is released.<p>Another bug to keep an eye on is#<a class="OutsideLink" target="_blank" href="http://developer.java.sun.com/developer/bugParade/bugs/4793741.html">4793741</a>, which proposes that the tool bar's windowget the <em>defaultLookAndFeelDecorated</em>setting from <code>JFrame</code>.<hr></blockquote></blockquote><h3><a name="button">Creating Tool Bar Buttons</a></h3><blockquote>The buttons in the tool bar are ordinary <code>JButton</code>s that use imagesfrom the Java look and feel Graphics Repository.We encourage you to consider using images from the repository if your tool baruses the Java look and feel.<p>Each image in the repository comes in 16x16 and 24x24 versions,provided in a file named<code>jlfgr-1_0.jar</code>.You can download this JAR file from the<a class="OutsideLink" target="_blank" href="http://java.sun.com/developer/techDocs/hi/repository/">Java look and feel Graphics Repository page</a>.That page shows all the imagesand has links to pages that describe each image,including its intended useand location within the JAR file.<p>Here is the code that creates the buttons and adds them to the tool bar.<blockquote><pre>protected void addButtons(JToolBar toolBar) {    JButton button = null;    //first button    button = makeNavigationButton("Back24", PREVIOUS,                                  "Back to previous something-or-other",                                  "Previous");    toolBar.add(button);    //second button    button = makeNavigationButton("Up24", UP,                                  "Up to something-or-other",                                  "Up");    toolBar.add(button);    <em>...//similar code for creating and adding the third button...</em>}protected JButton makeNavigationButton(String imageName,                                       String actionCommand,                                       String toolTipText,                                       String altText) {    //Look for the image.    String imgLocation = "toolbarButtonGraphics/navigation/"                         + imageName                         + ".gif";    URL imageURL = ToolBarDemo.class.getResource(imgLocation);    //Create and initialize the button.    JButton button = new JButton();    button.setActionCommand(actionCommand);    button.setToolTipText(toolTipText);    button.addActionListener(this);    if (imageURL != null) {                      //image found        button.setIcon(new ImageIcon(imageURL, altText));    } else {                                     //no image found        button.setText(altText);        System.err.println("Resource not found: " + imgLocation);    }    return button;}</pre></blockquote>The first call to <code>makeNavigationButton</code>creates the image for the first button,using the 24x24 "Back" navigation imagein the graphics repository.We found the image by looking at the<a class="OutsideLink" target="_blank" href="http://java.sun.com/developer/techDocs/hi/repository/">Java look and feel Graphics Repository page</a>, which shows the navigation images (among others)and has a link to the <a class="OutsideLink" target="_blank" href="http://java.sun.com/developer/techDocs/hi/repository/TBG_Navigation.html">navigation graphics page</a>.That page describes the navigation imagesand informs us that the 24x24 Back image is located at <code>toolbarButtonGraphics/navigation/Back24.gif</code>.The <code>makeNavigationButton</code> method gets the image from the repository's JAR file(assuming the JAR file is in the example's code base)using the <code>getResource</code> method.<p>Besides finding the image for the button,the <code>makeNavigationButton</code> methodalso creates the button, sets the strings for its action command and tool tip text,and adds the action listener for the button.If the image is missing, the method prints an error messageand puts text on the button,so that the button is still usable.<blockquote><hr><strong>Note:</strong>&nbsp;If any buttons in your tool bar duplicate functionalityof other components, such as menu items,you should probably create and add the tool bar buttonsas described in <a class="TutorialLink" target="_top" href="../misc/action.html">How to Use Actions</a>.<hr></blockquote></blockquote><h3><a name="more">Customizing Tool Bars</a></h3><blockquote><p>By adding a few lines of code to the preceding example,we can demonstrate some more tool bar features:<ul><li> Using <code>setFloatable(false)</code>     to make a tool bar immovable.<li> Using <code>setRollover(true)</code>     to make the edges of the tool bar's buttons     invisible except     for the button (if any) that the mouse pointer is over.<li> Adding a separator to a tool bar.<li> Adding a non-button component to a tool bar.</ul>Here is a picture of the new UI.You can <a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/components/examples/ToolBarDemo2.jnlp">run ToolBarDemo2</a> (it requires release 6) using<a class="TutorialLink" target="_top" href="../../information/javawebstart.html">Java Web Start</a>.Or to compile and run it yourself, consult the<a href="examples/index.html#ToolBarDemo2">example index</a>.You can find the code in<a class="SourceLink" target="_blank" href="examples/ToolBarDemo2.java"><code>ToolBarDemo2.java</code></a>.<p><center><IMG SRC="../../figures/uiswing/components/ToolBarDemo2.png" WIDTH="458" HEIGHT="164" ALIGN="BOTTOM" ALT="ToolBarDemo2 shows a tool bar with a variety of components"></center></p>[PENDING: This figure will be updated.The cursor should be over the fourth button,showing that the button under the cursor has an outline.]<p>Because the tool bar can no longer be dragged,it no longer has bumps at its left edge.Here's the code that turns off dragging:<blockquote><pre>toolBar.setFloatable(false);</pre></blockquote><p>The tool bar is in rollover mode,so only the button under the cursor has a border.Here's the code that sets rollover mode:<blockquote><pre>toolBar.setRollover(true);</pre></blockquote><p>Another visible differenceis that the tool bar contains two new components,which are preceded by a blank space &#151; a <a href="separator.html">separator</a>.Here is the code that adds the separator:<blockquote><pre>toolBar.addSeparator();</pre></blockquote>Here is the code that adds the new components:<blockquote><pre>//fourth buttonbutton = new JButton("Another button");...toolBar.add(button);//fifth component is NOT a button!JTextField textField = new JTextField("A text field");...toolBar.add(textField);</pre></blockquote>You can easily make the components in a tool bar be aligned along their tops or bottoms,instead of centered,by invoking the <code>setAlignmentY</code> method.For example, to align the tops of all the components in a tool bar,invoke <code>setAlignmentY(TOP_ALIGNMENT)</code> on each component.Similarly, you can use the <code>setAlignmentX</code> methodto specify the alignment of components when the tool bar is vertical.This flexibility of layout is possiblebecause tool bars use <code>BoxLayout</code>to position their components.For more information, see<a class="TutorialLink" target="_top" href="../layout/box.html">How to Use BoxLayout</a>.</blockquote><h3><a name="api">The Tool Bar API</a></h3><blockquote>The following table lists the commonly used<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JToolBar.html"><code>JToolBar</code></a> constructors and methods.Other methods you might callare listed in the API tables in<a href="jcomponent.html">The JComponent Class</a>.<p><table border=1> <tr><th align=left>Method or Constructor</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#JToolBar()">JToolBar()</a>    <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JToolBar.html#JToolBar(int)">JToolBar(int)</a>    <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JToolBar.html#JToolBar(java.lang.String)">JToolBar(String)</a>    <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JToolBar.html#JToolBar(java.lang.String, int)">JToolBar(String, int)</a>    </td>    <td>Create a tool bar. The optional int parameter lets you        specify the orientation; the default is <code>HORIZONTAL</code>.        The optional <code>String</code> parameter,        allows you to specify the title displayed for the        undocked tool bar's window.    </td>  </tr>  <tr>    <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/awt/Container.html#add(java.awt.Component)">Component add(Component)</a>    </td>    <td>Add a component to the tool bar.    <p><strong>Version note:</strong>&nbsp;    Before 1.3, the only way to associate an <code>Action</code>    with a tool bar button was to use <code>JToolBar</code>'s    <code>add(Action)</code> method     to create the button and add it to the tool bar.    As of 1.3, that method is no longer recommended.    You can instead associate a button with an <code>Action</code>    using the <code>setAction(Action)</code> method    defined by <code>AbstractButton</code>.    </td>  </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>    </td>    <td>Add a separator to the end of the tool bar.    </td>  </tr>  <tr>    <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JToolBar.html#setFloatable(boolean)">void setFloatable(boolean)</a>    <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JToolBar.html#isFloatable()">boolean isFloatable()</a>    </td>    <td>The floatable property is true by default,        to indicate that the user can drag the tool bar        out into a separate window.        To turn off tool bar dragging,        use <code>toolBar.setFloatable(false)</code>.	Some look and feels might ignore this property.    </td>  </tr>  <tr>    <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JToolBar.html#setRollover(boolean)">void setRollover(boolean)</a>    <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JToolBar.html#isRollover()">boolean isRollover()</a>    </td>    <td>The rollover property is false by default.	Set it to true        to request that every button in the tool bar 	have no borders until the user 	passes the cursor over the button.	Some look and feels might ignore this property.    </td>  </tr></table></blockquote><h3><a name="eg">Examples that Use Tool Bars</a></h3><blockquote>This table lists examples that use <code>JToolBar</code>and where those examples are described.<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#ToolBarDemo"><code>ToolBarDemo</code></a></td><td> This page</td><td> A basic tool bar with icon-only buttons.</td></tr><tr><td> <a href="examples/index.html#ToolBarDemo2"><code>ToolBarDemo2</code></a></td><td> This page</td><td> Demonstrates a non-floatable tool bar in rollover mode     that contains a separator     and a non-button component.</td></tr><tr><td> <a href="../misc/examples/index.html#ActionDemo"><code>ActionDemo</code></a></td><td><a class="TutorialLink" target="_top" href="../misc/action.html">How to Use Actions</a></td><td> Implements a tool bar using <code>Action</code> objects.</td></tr></table>        </blockquote>        <div class=NavBit>            <a target=_top href=textfield.html>&laquo; Previous</a>            &bull;            <a target=_top href=../TOC.html>Trail</a>            &bull;            <a target=_top href=tooltip.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 Text Fields        <br><b>Next page:</b> How to Use Tool Tips    </div>    </body></html> 

⌨️ 快捷键说明

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