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

📄 button.html

📁 jsf、swing的官方指南
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JCheckBoxMenuItem.html"><code>JCheckBoxMenuItem</code></a> class.Because <code>JCheckBox</code> and <code>JCheckBoxMenuItem</code> inherit from <code>AbstractButton</code>,Swing check boxes have all the usual button characteristics,as discussed earlier in this section.For example, you can specify imagesto be used in check boxes.<p>Check boxes are similar to <a href="#radiobutton">radio buttons</a>but their selection model is different, by convention.Any number of check boxes in a group&#151; none, some, or all &#151;can be selected.A group of radio buttons, on the other hand, can have only one button selected.<p>Here is a picture of an application that uses four check boxesto customize a cartoon:<p><center><IMG SRC="../../figures/uiswing/components/CheckBoxDemoMetal.png" WIDTH="203" HEIGHT="170" ALIGN="BOTTOM" ALT="NOT a tutorial reader!"></center></p><p><blockquote><hr><strong>Try this:</strong>&nbsp;<ol><li> <a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/components/examples/CheckBoxDemo.jnlp">Run CheckBoxDemo</a> (it requires release 6) using<a class="TutorialLink" target="_top" href="../../information/javawebstart.html">Java Web Start</a>.     Or, to compile and run the example yourself,     consult the      <a href="examples/index.html#CheckBoxDemo">example index</a>.<li> Click the <b>Chin</b> button or press Alt-c.     <br>     The <b>Chin</b> check box becomes unselected,     and the chin disappears from the picture.     The other check boxes remain selected.     This application has one item listener     that listens to all the check boxes.     Each time the item listener receives an event,     the application loads a new picture that reflects     the current state of the check boxes.</ol><hr></blockquote><p>A check box generates one item event and one action eventper click.Usually, you listen only for item events,since they let you determinewhether the click selected or deselected the check box.Below is the code from <a class="SourceLink" target="_blank" href="examples/CheckBoxDemo.java"><code>CheckBoxDemo.java</code></a>that creates the check boxes in the previous exampleand reacts to clicks.<blockquote><pre><em>//In initialization code:</em>    chinButton = new JCheckBox("Chin");    chinButton.setMnemonic(KeyEvent.VK_C);     chinButton.setSelected(true);    glassesButton = new JCheckBox("Glasses");    glassesButton.setMnemonic(KeyEvent.VK_G);     glassesButton.setSelected(true);    hairButton = new JCheckBox("Hair");    hairButton.setMnemonic(KeyEvent.VK_H);     hairButton.setSelected(true);    teethButton = new JCheckBox("Teeth");    teethButton.setMnemonic(KeyEvent.VK_T);     teethButton.setSelected(true);    //Register a listener for the check boxes.    chinButton.addItemListener(this);    glassesButton.addItemListener(this);    hairButton.addItemListener(this);    teethButton.addItemListener(this);...public void itemStateChanged(ItemEvent e) {    ...    Object source = e.getItemSelectable();    if (source == chinButton) {        <em>//...make a note of it...</em>    } else if (source == glassesButton) {        <em>//...make a note of it...</em>    } else if (source == hairButton) {        <em>//...make a note of it...</em>    } else if (source == teethButton) {        <em>//...make a note of it...</em>    }    if (e.getStateChange() == ItemEvent.DESELECTED)        <em>//...make a note of it...</em>    ...    updatePicture();}</pre></blockquote></blockquote><a name="radiobutton"><h3>How to Use Radio Buttons</h3></a><blockquote>Radio buttons are groups of buttonsin which,by convention,only one button at a time can be selected.The Swing release supports radio buttons with the<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JRadioButton.html"><code>JRadioButton</code></a> and<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/ButtonGroup.html"><code>ButtonGroup</code></a> classes.To put a radio button in a <a href="menu.html">menu</a>, use the<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JRadioButtonMenuItem.html"><code>JRadioButtonMenuItem</code></a> class.Other ways of displaying one-of-many choices are <a href="combobox.html">combo boxes</a> and<a href="list.html">lists</a>.Radio buttons look similar to<a href="#checkbox">check boxes</a>,but, by convention,check boxes place no limits on how many itemscan be selected at a time.<p>Because <code>JRadioButton</code> inherits from <code>AbstractButton</code>,Swing radio buttons have all the usual button characteristics,as discussed earlier in this section.For example, you can specify the image displayed in a radio button.<p>Here is a picture of an application that uses five radio buttonsto let you choose which kind of pet is displayed:<p><center><IMG SRC="../../figures/uiswing/components/RadioButtonDemoMetal.png" WIDTH="286" HEIGHT="196" ALIGN="BOTTOM" ALT="A snapshot of RadioButtonDemo"></center></p><p><blockquote><hr><strong>Try this:</strong>&nbsp;<ol><li> <a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/components/examples/RadioButtonDemo.jnlp">Run RadioButtonDemo</a> (it requires release 6) using<a class="TutorialLink" target="_top" href="../../information/javawebstart.html">Java Web Start</a>.     Or, to compile and run the example yourself,     consult the      <a href="examples/index.html#RadioButtonDemo">example index</a>.<li> Click the <b>Dog</b> button or press Alt-d.     <br>     The <b>Dog</b> button becomes selected,     which makes the <b>Bird</b> button become unselected.     The picture switches from a bird to a dog.     This application has one action listener      that listens to all the radio buttons.     Each time the action listener receives an event,     the application displays the picture     for the radio button     that was just clicked.     </ol><hr></blockquote><p>Each time the user clicks a radio button(even if it was already selected),the button fires an<a class="TutorialLink" target="_top" href="../events/actionlistener.html">action event</a>.One or two <a class="TutorialLink" target="_top" href="../events/itemlistener.html">item events</a> also occur &#151;one from the button that was just selected,and another from the button that lost the selection (if any).Usually, you handle radio button clicks using an action listener.<p>Below is the code from <a class="SourceLink" target="_blank" href="examples/RadioButtonDemo.java"><code>RadioButtonDemo.java</code></a>that creates the radio buttons in the previous exampleand reacts to clicks.<blockquote><pre><em>//In initialization code:</em>    //Create the radio buttons.    JRadioButton birdButton = new JRadioButton(birdString);    birdButton.setMnemonic(KeyEvent.VK_B);    birdButton.setActionCommand(birdString);    birdButton.setSelected(true);    JRadioButton catButton = new JRadioButton(catString);    catButton.setMnemonic(KeyEvent.VK_C);    catButton.setActionCommand(catString);    JRadioButton dogButton = new JRadioButton(dogString);    dogButton.setMnemonic(KeyEvent.VK_D);    dogButton.setActionCommand(dogString);    JRadioButton rabbitButton = new JRadioButton(rabbitString);    rabbitButton.setMnemonic(KeyEvent.VK_R);    rabbitButton.setActionCommand(rabbitString);    JRadioButton pigButton = new JRadioButton(pigString);    pigButton.setMnemonic(KeyEvent.VK_P);    pigButton.setActionCommand(pigString);    //Group the radio buttons.    ButtonGroup group = new ButtonGroup();    group.add(birdButton);    group.add(catButton);    group.add(dogButton);    group.add(rabbitButton);    group.add(pigButton);    //Register a listener for the radio buttons.    birdButton.addActionListener(this);    catButton.addActionListener(this);    dogButton.addActionListener(this);    rabbitButton.addActionListener(this);    pigButton.addActionListener(this);...public void actionPerformed(ActionEvent e) {    picture.setIcon(new ImageIcon("images/"                                   + e.getActionCommand()                                   + ".gif"));}</pre></blockquote><p>For each group of radio buttons,you need to create a <code>ButtonGroup</code> instanceand add each radio button to it.The <code>ButtonGroup</code>takes care of unselecting the previously selected buttonwhen the user selects another button in the group.<p>You should generally initialize a group of radio buttonsso that one is selected.However, the API doesn't enforce this rule &#151;a group of radio buttons can have no initial selection.Once the user has made a selection,exactly one button is selected from then on.There's no supported API for unselecting all the buttons.</blockquote><h3><a name="api">The Button API</a></h3><blockquote>The following tables list the commonly usedbutton-related API.Other methods you might call,such as <code>setFont</code> and <code>setForeground</code>,are listed in the API tables in<a href="jcomponent.html#api">The JComponent Class</a>.<p>The API for using buttons falls into these categories:<ul><li><a href="#contents">Setting or Getting the Button's Contents</a><li><a href="#looks">Fine Tuning the Button's Appearance</a><li><a href="#acts">Implementing the Button's Functionality</a><li><a href="#checkboxapi">Check Box Constructors</a><li><a href="#radiobuttonapi">Radio Button Constructors</a><li><a href="#togglebuttonapi">Toggle Button Constructors</a><li><a href="#buttongroup">Commonly Used Button Group Constructors/Methods</a></ul><p><table border=1><caption><a name="contents">Setting or Getting the Button's Contents</a></caption><tr><th>Method or Constructor</th><th>Purpose</th></tr>  <tr>    <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JButton.html#JButton(javax.swing.Action)">JButton(Action)</a>    <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JButton.html#JButton(java.lang.String, javax.swing.Icon)">JButton(String, Icon)</a>    <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JButton.html#JButton(java.lang.String)">JButton(String)</a>    <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JButton.html#JButton(javax.swing.Icon)">JButton(Icon)</a>    <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JButton.html#JButton()">JButton()</a></td><td>Create a <code>JButton</code> instance,    initializing it to have the specified text/image/action.</td></tr>  <tr>    <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/AbstractButton.html#setAction(javax.swing.Action)">void setAction(Action)</a>    <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/AbstractButton.html#getAction()">Action getAction()</a>    <br></td><td>Set or get the button's properties according to values    from the <code>Action</code> instance.</td></tr>  <tr>    <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/AbstractButton.html#setText(java.lang.String)">void setText(String)</a>    <br>

⌨️ 快捷键说明

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