加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java:JOptionPane Radio Buttons

发布时间:2020-12-15 05:12:51 所属栏目:Java 来源:网络整理
导读:我正在研究一个简单的程序来帮助我计算混合eLiquid的东西.我试图在JOptionPane.showInputDialog中添加单选按钮,但我无法将它们链接在一起.当我运行程序时,什么都没有出现.这就是我的全部: JRadioButton nicSelect = new JRadioButton("What is the target N
我正在研究一个简单的程序来帮助我计算混合eLiquid的东西.我试图在JOptionPane.showInputDialog中添加单选按钮,但我无法将它们链接在一起.当我运行程序时,什么都没有出现.这就是我的全部:

JRadioButton nicSelect = new JRadioButton("What is the target Nicotine level? ");
        JRadioButton b1 = new JRadioButton("0");
        JRadioButton b2 = new JRadioButton("3");
        JRadioButton b3 = new JRadioButton("6");
        JRadioButton b4 = new JRadioButton("12");
        JRadioButton b5 = new JRadioButton("18");
        JRadioButton b6 = new JRadioButton("24");

解决方法

作为使用多个JRadioButton的替代方法,您可以通过将String数组传递给JOptionPane.showInputDialog,通过JComboBox提供选择接口:

String[] values = {"0","3","6","12","18","24"};

Object selected = JOptionPane.showInputDialog(null,"What is the target Nicotine level?","Selection",JOptionPane.DEFAULT_OPTION,null,values,"0");
if ( selected != null ){//null if the user cancels. 
    String selectedString = selected.toString();
    //do something
}else{
    System.out.println("User cancelled");
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读