我的 JRadioButton 不能与我的动作侦听器一起使用

My JRadioButton won't work with my action listener

所以我正在编写一个程序来找出多面体的表面积和体积,所以我需要使用 JRadioButtons 让用户 select 他们想要什么形状,如果他们想要 SurfaceArea 或 Volume 之类的东西。

但是,我 运行 遇到了一个问题,每次单击新按钮时都需要我做一些事情 运行。

当我向 JRadioButton 添加 actionListener() 时,actionPerformed() 方法甚至没有 运行。有什么我想念的吗?

我希望我的 actionPerformed() 方法 运行。

width.addActionListener(ral);
height.addActionListener(ral);
length.addActionListener(ral);
slantHeight.addActionListener(ral);
radius.addActionListener(ral);
displayAnswer.addActionListener(ral);


public void actionPerformed(ActionEvent a) {
    System.out.println("Changed Radio Button: " + a.getSource());
}

来自 How to Write an Item Listener(强调我的):

Item events are fired by components that implement the ItemSelectable interface. Generally, ItemSelectable components maintain on/off state for one or more items.

由于单选按钮符合此描述,ItemListener 将是更适合使用的侦听器;试试看。

希望对您有所帮助!

仅供参考,这就是我所说的小型 "ish" 可编译可运行程序,它演示了一个问题。在这里,我演示 不是 向 JRadioButtons 添加动作侦听器或任何侦听器,而是向 JButton 添加单个侦听器(实际上是一个 AbstractAction,就像类固醇上的 ActionListener)。这使用 ButtonGroup 对象允许每个组只选择一个 JRadioButton,并允许代码查询选择了哪个按钮。 ButtonGroup 将为选定的 JRadioButton return "model",然后我们从该模型中提取 actionCommand 字符串:

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;

public class MyMcve extends JPanel {
    private static final String[] SHAPES = {
        "Circle", "Square", "Triangle"
    };
    private static final String[] COLORS = {
        "Red", "Orange", "Yellow", "Green", "Blue" 
    };
    private ButtonGroup shapeButtonGroup = new ButtonGroup();    
    private ButtonGroup colorButtonGroup = new ButtonGroup();

    public MyMcve() {
        JPanel shapesBtnPanel = new JPanel(new GridLayout(0, 1));
        shapesBtnPanel.setBorder(BorderFactory.createTitledBorder("Shapes"));
        for (String shape : SHAPES) {
            JRadioButton radioButton = new JRadioButton(shape);
            radioButton.setActionCommand(shape);
            shapeButtonGroup.add(radioButton);
            shapesBtnPanel.add(radioButton);
        }
        JPanel colorsBtnPanel = new JPanel(new GridLayout(0, 1));
        colorsBtnPanel.setBorder(BorderFactory.createTitledBorder("Colors"));
        for (String color : COLORS) {
            JRadioButton radioButton = new JRadioButton(color);
            radioButton.setActionCommand(color);
            colorButtonGroup.add(radioButton);
            colorsBtnPanel.add(radioButton);
        }

        JPanel bothButtonPanel = new JPanel(new GridLayout(1, 2));
        bothButtonPanel.add(shapesBtnPanel);
        bothButtonPanel.add(colorsBtnPanel);


        JButton getSelectionBtn = new JButton(new GetSelectionAction("Get Selection"));
        JPanel btnPanel = new JPanel();
        btnPanel.add(getSelectionBtn);

        setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        setLayout(new BorderLayout());
        add(bothButtonPanel, BorderLayout.CENTER);
        add(btnPanel, BorderLayout.PAGE_END);
    }

    private class GetSelectionAction extends AbstractAction {
        public GetSelectionAction(String name) {
            super(name);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            String shapeSelection = "";
            String colorSelection = "";
            ButtonModel shapeModel = shapeButtonGroup.getSelection();
            if (shapeModel != null) {
                shapeSelection = shapeModel.getActionCommand();
            }

            ButtonModel colorModel = colorButtonGroup.getSelection();
            if (colorModel != null) {
                colorSelection = colorModel.getActionCommand();
            }

            System.out.println("Selected Shape: " + shapeSelection);
            System.out.println("Selected Color: " + colorSelection);
        }
    }

    private static void createAndShowGui() {
        MyMcve mainPanel = new MyMcve();

        JFrame frame = new JFrame("MCVE");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

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