Java API 分類導覽 - javax.swing.ButtonGroup clearSelection()

ButtonGroup 類別 (class) 的 clearSelection() 方法 (method) 用來清除已選取的選項。



修飾子與參數
public void clearSelection()


clearSelection() 沒有回傳值 (return value) ,也不需要參數 (paramenter) 。


舉例如下
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class ButtonGroupDemo implements ActionListener {
    JFrame frame;
    ButtonGroup group;
    JRadioButton button1, button2, button3, button4;
    JButton button5, button6, button7, button8;
    
    public static void main(String[] args) {
        new ButtonGroupDemo();
    }
    
    public ButtonGroupDemo() {
        frame = new JFrame("SwingDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        button1 = new JRadioButton("1");
        button2 = new JRadioButton("2");
        button3 = new JRadioButton("3");
        button4 = new JRadioButton("4");
        group = new ButtonGroup();
        group.add(button1);
        group.add(button2);
        group.add(button3);
        group.add(button4);
        
        button5 = new JButton("clear");
        button5.setActionCommand("clear");
        button5.addActionListener(this);
        button6 = new JButton("count");
        button6.setActionCommand("count");
        button6.addActionListener(this);
        button7 = new JButton("selected");
        button7.setActionCommand("selected");
        button7.addActionListener(this);
        button8 = new JButton("remove");
        button8.setActionCommand("remove");
        button8.addActionListener(this);
        
        frame.setLayout(new GridLayout(2, 4));
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.add(button4);
        frame.add(button5);
        frame.add(button6);
        frame.add(button7);
        frame.add(button8);
        
        frame.pack();
        frame.setVisible(true);
    }
    
    public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();
        
        if (cmd == "clear") {
            group.clearSelection();
            System.out.println("clear");
        }
        else if (cmd == "count") {
            System.out.println("count: " + group.getButtonCount());
        }
        else if (cmd == "selected") {
            System.out.println("selected: " + group.getSelection());
        }
        else if (cmd == "remove") {
            group.remove(button1);
            group.remove(button2);
            group.remove(button3);
            group.remove(button4);
            frame.remove(button1);
            frame.remove(button2);
            frame.remove(button3);
            frame.remove(button4);
            frame.update(frame.getGraphics());
            System.out.println("done");
        }
        else {
            System.out.println("something wrong");
        }
    }
}

/* 《程式語言教學誌》的範例程式
    http://pydoing.blogspot.com/
    檔名:ButtonGroupDemo.java
    功能:示範 Java 程式 
    作者:張凱慶
    時間:西元 2012 年 9 月 */


此例的 ButtonGroupDemo 直接實作 ActionListener 介面
public class ButtonGroupDemo implements ActionListener {


接著建立四個 JRadioButton ,並建立一個 ButtonGroup ,然後呼叫 ButtonGroup 的 add() 把四個 JRadioButton 加入該 ButtonGroup 內
button1 = new JRadioButton("1");
button2 = new JRadioButton("2");
button3 = new JRadioButton("3");
button4 = new JRadioButton("4");
group = new ButtonGroup();
group.add(button1);
group.add(button2);
group.add(button3);
group.add(button4);


下面建立四個不同功能的 JButton 按鈕
button5 = new JButton("clear");
button5.setActionCommand("clear");
button5.addActionListener(this);
button6 = new JButton("count");
button6.setActionCommand("count");
button6.addActionListener(this);
button7 = new JButton("selected");
button7.setActionCommand("selected");
button7.addActionListener(this);
button8 = new JButton("remove");
button8.setActionCommand("remove");
button8.addActionListener(this);


由於 ButtonGroupDemo 實作 ActionListener 介面,因此要寫出 actionPerformed()
public void actionPerformed(ActionEvent e) {
    String cmd = e.getActionCommand();
        
    if (cmd == "clear") {
        group.clearSelection();
        System.out.println("clear");
    }
    else if (cmd == "count") {
        System.out.println("count: " + group.getButtonCount());
    }
    else if (cmd == "selected") {
        System.out.println("selected: " + group.getSelection());
    }
    else if (cmd == "remove") {
        group.remove(button1);
        group.remove(button2);
        group.remove(button3);
        group.remove(button4);
        frame.remove(button1);
        frame.remove(button2);
        frame.remove(button3);
        frame.remove(button4);
        frame.update(frame.getGraphics());
        System.out.println("done");
    }
    else {
        System.out.println("something wrong");
    }
}


由按鈕指令執行不同的工作, clear 呼叫 ButtonGroup 的 clearSelection() , 清除所有選擇, count 呼叫 ButtonGroup 的 getButtonCount() ,印出目前的選項數目, selected 呼叫ButtonGroup 的 getSelection() ,印出目前選擇的選項物件, remove 呼叫 ButtonGroup 的 remove() ,移除所有選項物件。


編譯後執行,結果如下



中英文術語對照
類別class
方法method
回傳值return value
參數parameter


您可以繼續參考
Swing 元件
  • ButtonGroup
  • JComponent
    • AbstractButton
      • JButton
      • JToggleButton
        • JCheckBox
        • JRadioButton
      • JMenuItem
        • JCheckBoxMenuItem
        • JMenu
        • JRadioButtonMenuItem
    • JColorChooser
    • JComboBox
    • JFileChooser
    • JInternalFrame
    • JLabel
    • JLayeredPane
      • JDesktopPane
    • JList
    • JMenuBar
    • JOptionPane
    • JPanel
    • JPopupMenu
    • JProgressBar
    • JRootPane
    • JScrollBar
    • JScrollPane
    • JSeparator
    • JSlider
    • JSpinner
    • JSplitPane
    • JTabbedPane
    • JTable
    • text.JTextComponent
      • JEditorPane
        • JTextPane
      • JTextField
        • JFormattedTextField
        • JPasswordField
      • JTextArea
    • JToolBar
    • JToolTip
    • JTree
    • JViewport
  • JDialog
  • JFrame
  • JWindow


相關目錄
Java API 分類導覽
Java 教材
首頁


參考資料
http://docs.oracle.com/javase/6/docs/api/javax/swing/ButtonGroup.html

沒有留言: