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

📄 button.html

📁 jsf、swing的官方指南
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<div class="linkBHEAD"><a href="tabbedpane.html">How to Use Tabbed Panes</a></div><div class="linkBHEAD"><a href="table.html">How to Use Tables</a></div><div class="linkBHEAD"><a href="textarea.html">How to Use Text Areas</a></div><div class="linkBHEAD"><a href="textfield.html">How to Use Text Fields</a></div><div class="linkBHEAD"><a href="toolbar.html">How to Use Tool Bars</a></div><div class="linkBHEAD"><a href="tooltip.html">How to Use Tool Tips</a></div><div class="linkBHEAD"><a href="tree.html">How to Use Trees</a></div><div class="linkAHEAD"><a href="icon.html">How to Use Icons</a></div><div class="linkAHEAD"><a href="border.html">How to Use Borders</a></div><div class="linkAHEAD"><a href="problems.html">Solving Common Component 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>Using Swing Components</a>            </span>            <div class=NavBit>                <a target=_top href=applet.html>&laquo;&nbsp;Previous</a>&nbsp;&bull;&nbsp;<a target=_top href=../TOC.html>Trail</a>&nbsp;&bull;&nbsp;<a target=_top href=colorchooser.html>Next&nbsp;&raquo;</a>            </div>            <div id=PageTitle>How to Use Buttons, Check Boxes, and Radio Buttons</div>            <blockquote><p>To create a button,you can instantiate one of the many classes that descend from the<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/AbstractButton.html"><code>AbstractButton</code></a> class.The following table shows the Swing-defined<code>AbstractButton</code> subclassesthat you might want to use:<p><table border=0 cellspacing=5><tr><th align=left >Class<th align=left >Summary<th align=left >Where Described</th></tr><tr valign=top><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JButton.html"><code>JButton</code></a></td><td> A common button.</td><td> <a href="#abstractbutton">How to Use the Common Button API</a> and     <a href="#jbutton">How to Use JButton Features</a></td></tr><tr valign=top><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JCheckBox.html"><code>JCheckBox</code></a></td><td> A check box button.</td><td> <a href="#checkbox">How to Use Check Boxes</a></td></tr><tr valign=top><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JRadioButton.html"><code>JRadioButton</code></a></td><td> One of a group of radio buttons.</td><td> <a href="#radiobutton">How to Use Radio Buttons</a></td></tr><tr valign=top><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JMenuItem.html"><code>JMenuItem</code></a></td><td> An item in a menu.</td><td> <a href="menu.html">How to Use Menus</a></td></tr><tr valign=top><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JCheckBoxMenuItem.html"><code>JCheckBoxMenuItem</code></a></td><td> A menu item that has a check box.</td><td> <a href="menu.html">How to Use Menus</a>     and <a href="#checkbox">How to Use Check Boxes</a></td></tr><tr valign=top><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JRadioButtonMenuItem.html"><code>JRadioButtonMenuItem</code></a></td><td> A menu item that has a radio button.</td><td> <a href="menu.html">How to Use Menus</a>     and <a href="#radiobutton">How to Use Radio Buttons</a></td></tr><tr valign=top><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JToggleButton.html"><code>JToggleButton</code></a></td><td> Implements toggle functionality     inherited by      <code>JCheckBox</code> and      <code>JRadioButton</code>.     Can be instantiated or subclassed     to create two-state buttons.</td><td> Used in some     <a href="#tbeg">examples</a></td></tr></table><blockquote><hr><strong>Note:</strong>&nbsp;If you want to collect a group of buttons into a row or column,then you should check out<a href="toolbar.html">tool bars</a>.<hr></blockquote>First, this section explains the basic button APIthat <code>AbstractButton</code> defines &#151;and thus all Swing buttons have in common.Next, it describes the small amount of API that<code>JButton</code> adds to <code>AbstractButton</code>.After that, this section shows you how to use specialized APIto implement check boxes and radio buttons.</blockquote><h3><a name="abstractbutton">How to Use the Common Button API</a></h3><blockquote>Here is a picture of an application that displays three buttons:<p><center><IMG SRC="../../figures/uiswing/components/ButtonDemoMetal.png" WIDTH="489" HEIGHT="96" ALIGN="BOTTOM" ALT="A snapshot of ButtonDemo"></center></p><p><blockquote><hr><strong>Try this:</strong>&nbsp;<ol><li> <a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/components/examples/ButtonDemo.jnlp">Run ButtonDemo</a> (it requires release 6) using<a class="TutorialLink" target="_top" href="../../information/javawebstart.html">Java<sup><font size=-2>TM</font></sup> Web Start</a>.    Or, to compile and run the example yourself,     consult the      <a href="examples/index.html#ButtonDemo">example index</a>.<li> Click the left button.     <br>     It disables the middle button     (and itself, since it's no longer useful)     and enables the right button.<li> Click the right button.     <br>     It enables the middle button     and the left button,     and disables itself.</ol><hr></blockquote><p>As the <code>ButtonDemo</code> example shows,a Swing button can display both text and an image.In <code>ButtonDemo</code>,each button has its text in a different place,relative to its image.The underlined letter in each button's textshows the <em>mnemonic</em> &#151; the keyboard alternative &#151;for each button.In most look and feels,the user can click a button by pressing the Alt key and the mnemonic.For example, Alt-M would click the Middle button in ButtonDemo.<p>When a button is disabled,the look and feel automatically generatesthe button's disabled appearance.However, you could provide an image to be substituted for the normal image.For example, you could provide gray versions of the images used in the left and right buttons.<p>How you implement event handling depends on the type of button you use and how you use it.Generally, you implement an <a class="TutorialLink" target="_top" href="../events/actionlistener.html">action listener</a>,which is notified every time the user clicks the button.For <a href="#checkbox">check boxes</a>you usually use an<a class="TutorialLink" target="_top" href="../events/itemlistener.html">item listener</a>,which is notified when the check box is selected or deselected.<p>Below is the code from <a class="SourceLink" target="_blank" href="examples/ButtonDemo.java"><code>ButtonDemo.java</code></a>that creates the buttons in the previous exampleand reacts to button clicks.The bold code is the codethat would remain if the buttons had no images.<p><blockquote><pre><em>//In initialization code:</em>    ImageIcon leftButtonIcon = createImageIcon("images/right.gif");    ImageIcon middleButtonIcon = createImageIcon("images/middle.gif");    ImageIcon rightButtonIcon = createImageIcon("images/left.gif");    <b>b1 = new JButton("Disable middle button"</b>, leftButtonIcon<b>);</b>    b1.setVerticalTextPosition(AbstractButton.CENTER);    b1.setHorizontalTextPosition(AbstractButton.LEADING); //aka LEFT, for left-to-right locales    <b>b1.setMnemonic(KeyEvent.VK_D);</b>    <b>b1.setActionCommand("disable");</b>    <b>b2 = new JButton("Middle button"</b>, middleButtonIcon<b>);</b>    b2.setVerticalTextPosition(AbstractButton.BOTTOM);    b2.setHorizontalTextPosition(AbstractButton.CENTER);    <b>b2.setMnemonic(KeyEvent.VK_M);</b>    <b>b3 = new JButton("Enable middle button"</b>, rightButtonIcon<b>);</b>    //Use the default text position of CENTER, TRAILING (RIGHT).    <b>b3.setMnemonic(KeyEvent.VK_E);    b3.setActionCommand("enable");    b3.setEnabled(false);</b>    //Listen for actions on buttons 1 and 3.    <b>b1.addActionListener(this);    b3.addActionListener(this);    b1.setToolTipText("Click this button to disable "                      + "the middle button.");    b2.setToolTipText("This middle button does nothing "                      + "when you click it.");    b3.setToolTipText("Click this button to enable the "                      + "middle button.");</b>    ...}<b>public void actionPerformed(ActionEvent e) {    if ("disable".equals(e.getActionCommand())) {        b2.setEnabled(false);        b1.setEnabled(false);        b3.setEnabled(true);    } else {        b2.setEnabled(true);        b1.setEnabled(true);        b3.setEnabled(false);    }} </b>protected static ImageIcon createImageIcon(String path) {    java.net.URL imgURL = ButtonDemo.class.getResource(path);    <em>...//error handling omitted for clarity...</em>    return new ImageIcon(imgURL);}</pre></blockquote></blockquote><h3><a name="jbutton">How to Use JButton Features</a></h3><blockquote>Ordinary buttons &#151; <code>JButton</code> objects &#151;have just a bit more functionalitythan the <code>AbstractButton</code> class provides:You can make a <code>JButton</code>be the default button.<p>At most one button in a top-level containercan be the default button.The default button typically has a highlighted appearanceand acts clicked whenever the top-level container has the keyboard focusand the user presses the Return or Enter key.Here is a picture of a dialog, implemented in the<a href="examples/index.html#ListDialog">ListDialog</a> example,in which the <b>Set</b> buttonis the default button:<p><center><IMG SRC="../../figures/uiswing/components/NameChooserMetal.png" WIDTH="278" HEIGHT="191" ALIGN="BOTTOM" ALT="In the Java Look & Feel, the default button has a heavy border"></center></p><p>You set the default buttonby invoking the <code>setDefaultButton</code> methodon a top-level container's root pane.Here is the code that sets up the default buttonfor the <code>ListDialog</code> example:<blockquote><pre><em>//In the constructor for a JDialog subclass:</em>getRootPane().setDefaultButton(setButton);</pre></blockquote>The exact implementation of the default button featuredepends on the look and feel.For example, in the Windows look and feel,the default button changes to whichever button has the focus,so that pressing Enterclicks the focused button.When no button has the focus, the button you originally specifiedas the default button becomes the default button again.</blockquote><a name="checkbox"><h3>How to Use Check Boxes</h3></a><blockquote>The <a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JCheckBox.html"><code>JCheckBox</code></a> class provides support for check box buttons.You can also put check boxes in <a href="menu.html">menus</a>,using the 

⌨️ 快捷键说明

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