如何在 Java 中使用单选按钮制作 switch 语句

How to make a switch statement with JRadio Buttons in Java

这里是 Java 的初学者,在这里有点困惑,所以我知道如何制作 switch 语句并且我知道如何使用 JRadio 按钮。只是无法将它们放在一起以使用单选按钮执行 switch 语句。我确实有 JRadio 按钮,我想在名为 payFrequency 的 JRadio 按钮组中使用它们。

更新:我正在尝试使用的代码示例,所以我知道下面这个是不正确的,我建议提供我正在尝试做的事情的示例。(payFrequency 是按钮组,另一个单选按钮不知道该信息是否相关。)

           switch(PayFrequency)
               case jRadioButton1.isSelected():
                   sal1= (sal1a + sal1b) * 2.15;
                   break;
               case jRadioButton2.isSelected():
                   sal1= (sal1a + sal1b) * 4.3;
                   break;
               case jRadioButton3.isSelected():
                   sal1= (sal1a + sal1b) * 4.3;
                   break;                       
               default
                     sal1= sal1a + sal1b;  

JDK7+支持switch all primitives and String objects。所以不,你不能在 switch 语句中使用单选按钮。但是您可以在您的开关中使用 myRadioButton.getText(),这将 return 单选按钮的文本标签。然后你可以在你的 switch 中对每个案例采取适当的行动。

这有点棘手,但您可以使用 setActionCommand(String s) 将 "id" 设置为 JRadioButton,然后将它们与 switch case 一起使用。

检查我根据一个随机示例修改的代码 (Original Example):

import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;


public class SwingJRadioButtonDemo extends JFrame {


private static final long serialVersionUID = -     8307105427074441939L;

    private JButton buttonOK = new JButton("OK");

    private JRadioButton optionLinux = new JRadioButton("Linux");
    private JRadioButton optionWin = new JRadioButton("Windows");
    private JRadioButton optionMac = new JRadioButton("Macintosh");



    public SwingJRadioButtonDemo() {
        super("Swing JRadioButton Demo");
        //Set ID and add to group
        ButtonGroup group = new ButtonGroup();
        optionLinux.setActionCommand ( "1" );
        group.add(optionLinux);
        optionWin.setActionCommand ( "2" );
        group.add(optionWin);
        optionMac.setActionCommand ( "3" );
        group.add(optionMac);



        optionWin.setSelected(true);


        setLayout(new GridBagLayout());
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.anchor = GridBagConstraints.CENTER;
        constraints.insets = new Insets(10, 10, 10, 10);

        add(optionLinux, constraints);
        constraints.gridx = 1;
        add(optionWin, constraints);
        constraints.gridx = 2;
        add(optionMac, constraints);

        constraints.gridx = 0;
        constraints.gridy = 1;
        constraints.gridwidth = 3;


        constraints.gridy = 2;
        add(buttonOK, constraints);

    RadioButtonActionListener actionListener = new     RadioButtonActionListener();
        optionLinux.addActionListener(actionListener);
        optionWin.addActionListener(actionListener);
        optionMac.addActionListener(actionListener);

        buttonOK.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent event) {
                //Get "ID"
            String selectedOption = group.getSelection (     ).getActionCommand ( );
                //Switch on "IDS"
                switch(selectedOption) {
                    case "1":
                    JOptionPane.showMessageDialog(    SwingJRadioButtonDemo.this,
                            "You selected: Linux with id: " +     selectedOption);
                        break;
                    case "2":
                    JOptionPane.showMessageDialog(    SwingJRadioButtonDemo.this,
                            "You selected: Windows with id: " +     selectedOption);
                        break;
                    case "3":
                    JOptionPane.showMessageDialog(    SwingJRadioButtonDemo.this,
                            "You selected Mac with id: " +     selectedOption);
                        break;
                }
            }

        });

        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }

    class RadioButtonActionListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent event) {
            JRadioButton button = (JRadioButton) event.getSource();
            if (button == optionLinux) {

                System.out.println ( "Linux" );

            } else if (button == optionWin) {

                System.out.println ( "Windows" );

            } else if (button == optionMac) {

                System.out.println ( "Mac" );
            }
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new SwingJRadioButtonDemo().setVisible(true);
            }
        });
    }
}