This article shares with you how to use Swing radio buttons and check boxes for your reference. The specific content is as follows
JRadioButton constructor:
JRadioButton(): Create a new JRadioButton.
JRadioButton(Icon icon): Create a JRadioButton with images but no text.
JRadioButton(Icon icon,boolean selected): Create a JRadioButton with an image but no text, and set its initial state (whether it is selected).
JRadioButton(String text): Create a JRadioButton with text.
JRadioButton(String text,boolean selected): Create a JRadioButton with text and set its initial state (whether it is selected).
JRadioButton(String text,Icon icon): Create a JRadioButton with text and images, with the initial state of none being selected.
JRadioButton(String text,Icon icon,boolean selected): Create a JRadioButton with text and images, and set its initial state (whether it is selected)
To change RadioButton to a single choice, we must use the ButtonGroup class. This class is located under the javax.swing package. The main function of the ButtonGroup class is: at the same time, there will be only one component with a state of "on", and the others are "off", that is, only one component will be selected at the same time. ButtonGroup class can be used by subclasses under AbstractButton. The most commonly used components are JRadioButton, JradioButtonMenu, Item and JToggleButton.
The construction method of ButtonGroup class is as follows:
ButtonGroup() Creates a new ButtonGroup()
The commonly used methods of ButtonGroup() class are as follows:
public void add(AbstractButton b): Add button to group
public void clearSelection(): Clear the selected content, that is, no buttons in the button group are selected
public int getButtonCount(): Get the number of buttons in this group
public Enumeration<AbstractButton>getElement(): Get the button used in this group
public void remove (AbstractButton b): Remove button from button
JCheckBox constructor
JCheckBox(): Create a new JChcekBox.
JCheckBox(Icon icon): Create a JCheckBox with images but no text.
JCheckBox(Icon icon,boolean selected): Create a JCheckBox with an image but no text, and set its initial state (whether it is selected).
JCheckBox(String text): Create a JCheckBox with text.
JCheckBox(String text,boolean selected): Create a JCheckBox with text and set its initial state (whether it is selected).
JCheckBox(String text,Icon icon): Create a JCheckBox with text and images, with the initial state of none selected.
JCheckBox(String text,Icon icon,boolean selected): Create a JCheckBox with text and images, and set its initial state (whether it is selected).
When the option in JCheckBox is selected or canceled, it will trigger the event of the ItemEvent. The ItemEvent class provides 4 methods to use, namely getItem(), getItemSelectable(), getStateChange(), and paramString(). The getItem() and paramString() methods will return some of the status values of this JCheckBox. Generally, we rarely use these two methods.
getItemSelectable() is equivalent to getSource() method. It also returns the component that triggers the event and is used to determine which component produces the event. The getSource() method is provided by the EventObject class, and all event classes will inherit this class. Therefore, we can use the getSource() method to determine which component triggers the event for all events.
Finally, the getStateChange() method will return whether this component has been selected. This method returns an integer value. We can use the class variable provided by ItemEvent; if it is selected, it returns SELECTED, and if it is not selected, it returns DESELECTED.
Radio buttons and check boxes to register and cancel the ItemEvent event listener as follows:
public void addItemListener(ItemListener l): Register the specified ItemListener event listener
public void removeItemListener(ItemListener l): Log out the specified ItemListener event listener
package ch10; import java.awt.event.*; import javax.swing.*; public class Vote extends JFrame implements ActionListener { private JPanel jp = new JPanel(); JRadioButton jrb1 = new JRadioButton("This website is very good, very novel!",true); JRadioButton jrb2 = new JRadioButton("This website is very ordinary, too ordinary"); JRadioButton jrb3 = new JRadioButton("This website is very bad, look at it occasionally"); JRadioButton jrb4 = new JRadioButton("This website is too bad, no more"); private JRadioButton[] jrb = new JRadioButton[]{jrb1,jrb2,jrb3,jrb4}; private ButtonGroup bg = new ButtonGroup(); JCheckBox jcb1 = new JCheckBox("The interface is more beautiful"); JCheckBox jcb2 = new JCheckBox("The content is richer"); JCheckBox jcb3 = new JCheckBox("Value-added service is better"); JCheckBox jcb4 = new JCheckBox("Member service is better"); private JCheckBox[] jcb = new JCheckBox[]{jcb1,jcb2,jcb3,jcb4}; private JButton [] jb = {new JButton("I want to vote"),new JButton("I want to reselect")}; private JLabel[] jl = {new JLabel("This website gives you the impression: "),new JLabel("Where do you think this site is better"),new JLabel("What you vote is: ")}; private JTextArea jt = new JTextArea(); private JScrollPane js= new JScrollPane(jt); public Vote() { jp.setLayout(null); for(int i=0;i<4;i++) { jrb[i].setBounds(30+170*i,40,170,30); jcb[i].setBounds(30+120*i,100,120,30); jp.add(jrb[i]); jp.add(jcb[i]); jcb[i].addActionListener(this); jrb[i].addActionListener(this); bg.add(jrb[i]); if(i>1) continue; jl[i].setBounds(20,20+50*i,200,30); jb[i].setBounds(380+120*i,200,100,20); jp.add(jl[i]); jp.add(jb[i]); jb[i].addActionListener(this); } jl[2].setBounds(20,150,120,30); jp.add(jl[2]); js.setBounds(120,150,500,50); jp.add(js); jt.setLineWrap(true); jt.setEditable(false); this.add(jp); this.setTitle("Website Satisfaction Questionnaire"); this.setBounds(150,150,750,300); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent a) { if(a.getSource()==jb[1]) { bg.clearSelection(); for(int i=0;i<jcb.length;i++) jcb[i].setSelected(false); jt.setText(""); } else { StringBuffer temp1 = new StringBuffer("You think this website"); StringBuffer temp2 = new StringBuffer(""); for(int i=0;i<4;i++) { if(jrb[i].isSelected()) temp1.append(jrb[i].getText()); if(jcb[i].isSelected()) temp2.append(jcb[i].getText()+","); } if(temp2.length()==0) jt.setText("Please select both surveys"); else { temp1.append("You think this website"); temp1.append(temp2.substring(0,temp2.length()-1)); jt.setText(temp1.toString()); } } public static void main(String args[]) { new Vote(); } }Reproduction image:
The above is all about this article, I hope it will be helpful to everyone's learning.