【问题标题】:Swing: Intercept click on a JRadiobuttonSwing:拦截点击 JRadiobutton
【发布时间】:2022-12-20 05:32:01
【问题描述】:

我想拦截按钮组中 JRadioButton 的点击。 More precise: When JRadioButton A is chosen and the user clicks on JRadioButton B, I want to show a Yes/No-option pane.只有当用户点击“是”时,单选按钮 B 才会被选中。如果用户单击“否”,则不应更改任何内容。 这是还是更确切地说如何这可能吗?

谢谢你的时间

【问题讨论】:

    标签: java swing user-interface jradiobutton


    【解决方案1】:

    添加一个ItemListener单选按钮。参考How to Use Radio Buttons。选择更改后调用侦听器。

    如果按钮被选中,显示JOptionPane要求用户确认。参考How to Make DialogsJOptionPane 也可以通过点击关闭ESC键键或按X右上角的按钮。

    如果用户通过单击关闭 JOptionPane是的按钮,我们什么都不做,因为按钮实际上已经被选中了。如果用户以任何其他方式关闭JOptionPane,那么我们需要将选择重置为按钮一个.

    import java.awt.EventQueue;
    import java.awt.event.ItemEvent;
    
    import javax.swing.ButtonGroup;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    
    public class Intercep {
        private JFrame frame;
        private JRadioButton aRadioButton;
        private JRadioButton bRadioButton;
    
        private void buildAndDisplayGui() {
            frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(createButtons());
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    
        private JPanel createButtons() {
            JPanel panel = new JPanel();
            ButtonGroup bg = new ButtonGroup();
            aRadioButton = new JRadioButton("A");
            bRadioButton = new JRadioButton("B");
            bRadioButton.addItemListener(this::handleItem);
            bg.add(aRadioButton);
            bg.add(bRadioButton);
            panel.add(aRadioButton);
            panel.add(bRadioButton);
            return panel;
        }
    
        private void handleItem(ItemEvent event) {
            if (event.getStateChange() == ItemEvent.SELECTED) {
                int result = JOptionPane.showConfirmDialog(frame,
                                                           "Are you sure?",
                                                           "Confirm",
                                                           JOptionPane.YES_NO_OPTION);
                switch (result) {
                    case 0:
                        // YES
                        break;
                    case -1:
                        // <ESC> or 'X'
                    case 1:
                        // NO
                    default:
                        aRadioButton.setSelected(true);
                }
            }
        }
    
        public static void main(String args[]) {
            EventQueue.invokeLater(() -> new Intercep().buildAndDisplayGui());
        }
    }
    

    请注意上面代码中的这一行:

    bRadioButton.addItemListener(this::handleItem);
    

    这称为 method reference

    【讨论】:

      【解决方案2】:

      您想监听 ItemEvents 以了解何时选择了第一个按钮(请参阅 this)。您可以使用AbstractButton#isSelected 来检查是否选择了另一个按钮。最后,您可以使用 JOptionPane#showConfirmDialog(Component, Object, String, int) 提示用户选择“是”或“否”,并使用类似 AbstractButton#setSelected(boolean)ButtonGroup#clearSelection 的内容来控制选择哪些按钮。

      这应该让你开始走上正确的轨道。

      【讨论】:

      • 非常感谢你的两个答案。对我的问题是,选择按钮B时,我还有其他代码列表。在建议的代码中,即使用户单击“否”,也会短时间选择按钮 B。是否有替代方法来实际拦截按钮 B 的选择?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-07
      • 2021-02-26
      相关资源
      最近更新 更多