简体   繁体   English

如何在Java中使用JRadio Buttons制作switch语句

[英]How to make a switch statement with JRadio Buttons in Java

Beginner at Java here, kinda of confused here, so I know how to make switch statements and I know how to use JRadio buttons. Java的初学者,这里有点困惑,所以我知道如何制作switch语句,也知道如何使用JRadio按钮。 Just having trouble putting it all together to do a switch statement using radio buttons. 只是很难将所有内容放在一起使用单选按钮执行switch语句。 I do have the JRadio buttons that I want to use in a JRadio button group called payFrequency. 我确实有要在名为payFrequency的JRadio按钮组中使用的JRadio按钮。

Update: example of code I am trying to use, so I know this below is incorrect, I would advise to provide and example of what I was trying to do.( payFrequency is the button group that the other radio button are in dont know if that information is relevant.) 更新:我正在尝试使用的代码示例,所以我知道以下内容是错误的,我建议提供和我正在尝试执行的示例。(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+ supports switch with all primitives and String objects . JDK7 +支持all primitives and String objects切换。 So no you cannot use a radio button in a switch statement. 因此,不,您不能在switch语句中使用单选按钮。 But you can use myRadioButton.getText() in your switch which will return the text label of the radio button. 但是您可以在开关中使用myRadioButton.getText() ,这将返回单选按钮的文本标签。 Then you can take appropriate actions for each case inside your switch. 然后,您可以针对交换机内部的每种情况采取适当的措施。

It's kinda tricky but you can set an "id" to JRadioButton with setActionCommand(String s) and then use them with switch case. 这有点棘手,但是您可以使用setActionCommand(String s)为JRadioButton设置一个“ id”,然后将其与switch case一起使用。

Check this code i modified from one random example ( 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);
            }
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM