Java单选按钮

时间:2015-02-26 07:07:46

标签: java

我是新的Java用户,我正在制作一个表单,我已经在我的表单中添加了3个单选按钮,我想显示一个错误,如果用户选择三个单选按钮,则会显示一条消息:& #34;请只选择一个按钮"。

我的代码就像

}
    else
    {
        Check1="";

    }
    if(jRadioButton2.isSelected()==true)
    {
        Check2="C++";

    }
    else
    {
        Check2="";
    }

  if(jRadioButton3.isSelected()==true)
  {
      Check3="C Sharp";

  }
  else
  {
      Check3="";
  }

  {
    if(jRadioButton1+jRadioButton2+jRadioButton3.isSelected()==false) 
    {
    jLabel6.show("please Select one option");

5 个答案:

答案 0 :(得分:2)

通过阅读您要打印的错误消息,您可能只想一次从3个中选择1个RadioButton(请只选择一个还包括选择2应该生成错误)。

因此,适当的解决方案可能是使用ButtonGroup

答案 1 :(得分:1)

Go for Good coding statndard Use ButtonGroup类:用于为一组按钮创建多重排除范围。

在组中添加单选按钮不允许用户选择多个单选按钮。因此,您将无法检查用户选择了多少个单选按钮。

您的示例代码: -

package demo;

import javax.swing.ButtonGroup;


public class radio extends javax.swing.JFrame {

    /**
     * Creates new form radio
     */
    public radio() {
        initComponents();

        ButtonGroup bg = new ButtonGroup();
        bg.add(jRadioButton1);
        bg.add(jRadioButton2);
        bg.add(jRadioButton3);

    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jRadioButton3 = new javax.swing.JRadioButton();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jRadioButton1.setText("One");
        jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton1ActionPerformed(evt);
            }
        });

        jRadioButton2.setText("Two");

        jRadioButton3.setText("three");

        jButton1.setText("Ok");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(215, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton1)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jRadioButton3)
                        .addComponent(jRadioButton2)
                        .addComponent(jRadioButton1)))
                .addGap(134, 134, 134))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addComponent(jRadioButton1)
                .addGap(18, 18, 18)
                .addComponent(jRadioButton2)
                .addGap(18, 18, 18)
                .addComponent(jRadioButton3)
                .addGap(55, 55, 55)
                .addComponent(jButton1)
                .addContainerGap(89, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                              
        // TODO add your handling code here:
    }                                             

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

// TODO add your handling code here:
    }                                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(radio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(radio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(radio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(radio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new radio().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    // End of variables declaration                   
}

希望这会有所帮助。enter image description here

答案 2 :(得分:0)

您可以尝试加入以下所有三个条件:

if (jRadioButton1.isSelected() && jRadioButton2.isSelected() && jRadioButton3.isSelected()) {
    //user has selected all three buttons
}

答案 3 :(得分:0)

您可以根据需要使用以下代码段

if(jRadioButton1.isSelected()){  
JOptionPane.showMessageDialog(this,"First radio Button selected");  
}  
elseif(jRadioButton2.isSelected()){  
JOptionPane.showMessageDialog(this,"Second radio Button selected");  
}  
elseif(jRadioButton3.isSelected()){  
JOptionPane.showMessageDialog(this,"third radio Button selected");  
}
elseif(jRadioButton1.isSelected()&& jRadioButton2.isSelected() && jRadioButton3.isSelected()){  
JOptionPane.showMessageDialog(this,"All 3 radio Buttons selected");  
}
else
{
JOptionPane.showMessageDialog(this,"No radio Button selected");  
} 

答案 4 :(得分:0)

一个简单的答案就是

if(jRadioButton1.isSelected() && jRadioButton2.isSelected() && jRadioButton3.isSelected)
  /*display message*/

但是,您应该使用复选框。单选按钮用于互斥项目,这意味着您只能选择一个。复选框适用于有选项列表的情况,用户可以在其中选择任意数量的选项。