JRadioButton有兩種狀態(tài):選中和未選中。當(dāng)我們需要用戶進(jìn)行單選時(shí)使用一組JRadioButtons。
我們可以使用一個(gè)Action對(duì)象,一個(gè)字符串標(biāo)簽,圖標(biāo)和布爾標(biāo)志,以指示其是否被選擇默認(rèn)創(chuàng)建JRadioButton。
創(chuàng)建沒(méi)有標(biāo)簽和沒(méi)有圖像的JRadioButton
JRadioButton rb1 = new JRadioButton();
創(chuàng)建JRadioButton,文本為“我的選擇"
JRadioButton rb2 = new JRadioButton("My Choice");
創(chuàng)建JRadioButton,文本為“我的選擇"并默認(rèn)選擇
JRadioButton rb3 = new JRadioButton("My Choice", true);
要選擇/取消選擇JRadioButton,請(qǐng)調(diào)用setSelected()方法。
要檢查是否被選中,請(qǐng)使用它們的isSelected()方法。
以下代碼顯示如何使用這些方法:
tb3.setSelected(true); // Select tb3 boolean b1 = tb3.isSelected(); // will store true in b1 tb3.setSelected(false); // Unselect tb3 boolean b2 = tb3.isSelected(); // will store false in b2
如果選擇是互斥的,我們必須在按鈕組中分組所有選擇。ButtonGroup類的一個(gè)實(shí)例表示一個(gè)按鈕組。
我們可以通過(guò)使用它的add()和remove()方法分別向按鈕組添加和刪除JRadioButton。
最初,按鈕組的所有成員都未選中。
要形成一個(gè)按鈕組,我們需要將所有互斥選擇組件添加到ButtonGroup類的一個(gè)對(duì)象。 我們不會(huì)向容器添加ButtonGroup對(duì)象。
我們必須向容器中添加所有選擇的組件。
下面的代碼顯示了一組三個(gè)相互排斥的JRadioButton。
import java.awt.BorderLayout; import java.awt.Container; /* w ww. j a va 2 s . c o m*/ import javax.swing.Box; import javax.swing.ButtonGroup; import javax.swing.JFrame; import javax.swing.JRadioButton; public class Main extends JFrame { ButtonGroup genderGroup = new ButtonGroup(); JRadioButton genderMale = new JRadioButton("Male"); JRadioButton genderFemale = new JRadioButton("Female"); JRadioButton genderUnknown = new JRadioButton("Unknown"); public Main() { this.setDefaultCloseOperation(EXIT_ON_CLOSE); genderGroup.add(genderMale); genderGroup.add(genderFemale); genderGroup.add(genderUnknown); Box b1 = Box.createVerticalBox(); b1.add(genderMale); b1.add(genderFemale); b1.add(genderUnknown); Container contentPane = this.getContentPane(); contentPane.add(b1, BorderLayout.CENTER); } public static void main(String[] args) { Main bf = new Main(); bf.pack(); bf.setVisible(true); } }
下面的代碼顯示了如何使用ActionListener監(jiān)聽(tīng)JRadioButton事件。
import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; // ww w .j av a 2s. co m import javax.swing.AbstractButton; import javax.swing.ButtonGroup; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JRadioButton; public class Main { public static void main(String args[]) { JFrame frame = new JFrame("Grouping Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(new GridLayout(0, 1)); ButtonGroup group = new ButtonGroup(); JRadioButton aRadioButton = new JRadioButton("A"); JRadioButton bRadioButton = new JRadioButton("B"); ActionListener sliceActionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { AbstractButton aButton = (AbstractButton) actionEvent.getSource(); System.out.println("Selected: " + aButton.getText()); } }; panel.add(aRadioButton); group.add(aRadioButton); panel.add(bRadioButton); group.add(bRadioButton); aRadioButton.addActionListener(sliceActionListener); bRadioButton.addActionListener(sliceActionListener); frame.add(panel); frame.setSize(300, 200); frame.setVisible(true); } }
下面的代碼顯示了如何使用ChangeListener監(jiān)聽(tīng)JRadioButton事件。
它告訴你當(dāng)所選單選按鈕被布防,按下,選擇或釋放時(shí)。
import java.awt.GridLayout; //w w w . ja v a 2s .c o m import javax.swing.AbstractButton; import javax.swing.ButtonGroup; import javax.swing.ButtonModel; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; public class Main { public static void main(String args[]) { JFrame frame = new JFrame("Grouping Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(new GridLayout(0, 1)); ButtonGroup group = new ButtonGroup(); JRadioButton aRadioButton = new JRadioButton("A"); JRadioButton bRadioButton = new JRadioButton("B"); ChangeListener changeListener = new ChangeListener() { public void stateChanged(ChangeEvent changEvent) { AbstractButton aButton = (AbstractButton)changEvent.getSource(); ButtonModel aModel = aButton.getModel(); boolean armed = aModel.isArmed(); boolean pressed = aModel.isPressed(); boolean selected = aModel.isSelected(); System.out.println("Changed: " + armed + "/" + pressed + "/" + selected); } }; panel.add(aRadioButton); group.add(aRadioButton); panel.add(bRadioButton); group.add(bRadioButton); aRadioButton.addChangeListener(changeListener); bRadioButton.addChangeListener(changeListener); frame.add(panel); frame.setSize(300, 200); frame.setVisible(true); } }
下面的代碼顯示了如何使用ItemListener監(jiān)聽(tīng)JRadioButton事件。
import java.awt.GridLayout; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; // w ww . j a v a2 s . co m import javax.swing.AbstractButton; import javax.swing.ButtonGroup; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JRadioButton; public class Main { public static void main(String args[]) { JFrame frame = new JFrame("Grouping Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(new GridLayout(0, 1)); ButtonGroup group = new ButtonGroup(); JRadioButton aRadioButton = new JRadioButton("A"); JRadioButton bRadioButton = new JRadioButton("B"); ItemListener itemListener = new ItemListener() { String lastSelected; public void itemStateChanged(ItemEvent itemEvent) { AbstractButton aButton = (AbstractButton)itemEvent.getSource(); int state = itemEvent.getStateChange(); String label = aButton.getText(); String msgStart; if (state == ItemEvent.SELECTED) { if (label.equals(lastSelected)) { msgStart = "Reselected -> "; } else { msgStart = "Selected -> "; } lastSelected = label; } else { msgStart = "Deselected -> "; } System.out.println(msgStart + label); } }; panel.add(aRadioButton); group.add(aRadioButton); panel.add(bRadioButton); group.add(bRadioButton); aRadioButton.addItemListener(itemListener); bRadioButton.addItemListener(itemListener); frame.add(panel); frame.setSize(300, 200); frame.setVisible(true); } }
更多建議: