java怎么判断单选框是否选中

59次阅读
没有评论

共计 806 个字符,预计需要花费 3 分钟才能阅读完成。

在 Java 中,可以使用 isSelected() 方法来判断单选框是否被选中。

以下是一个示例代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class RadioButtonExample extends JFrame implements ActionListener {
    JRadioButton radioButton;
    JButton button;

    public RadioButtonExample() {setTitle(" 单选框示例 ");
        setSize(300, 200);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        radioButton = new JRadioButton(" 选项 ");
        button = new JButton(" 判断选中 ");
        button.addActionListener(this);

        JPanel panel = new JPanel();
        panel.add(radioButton);
        panel.add(button);
        add(panel);

        setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {if (radioButton.isSelected()) {System.out.println(" 单选框选中 ");
        } else {System.out.println(" 单选框未选中 ");
        }
    }

    public static void main(String[] args) {new RadioButtonExample();}
}

在上述代码中,使用 isSelected() 方法来判断单选框是否选中。在 actionPerformed(ActionEvent e) 方法中,根据 isSelected() 的返回值来输出相应的信息。

丸趣 TV 网 – 提供最优质的资源集合!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-12-09发表,共计806字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)