我的JRadioButton不适用于我的动作监听器

时间:2015-12-12 01:41:07

标签: java swing radio-button listener jradiobutton

所以我正在创建一个程序来找出表面积和多面体的体积,所以我需要使用JRadioButton来让用户选择他们想要的形状,如果他们想要SurfaceArea或Volume,和那样的东西。

然而,我遇到了一个问题,需要我在每次点击一个新按钮时运行一些东西。

当我向actionListener()添加JRadioButton时,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());
}

2 个答案:

答案 0 :(得分:3)

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

  

项目事件由实现ItemSelectable接口的组件触发。通常,ItemSelectable组件维护一个或多个商品的开/关状态

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

希望这有帮助!

答案 1 :(得分:2)

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

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();
            }
        });
    }
}
相关问题