如何在 Swing 中将选择限制为最多三个单选按钮中的两个? [英] How to limit selection to a maximum of two out of three radio buttons in Swing?

查看:46
本文介绍了如何在 Swing 中将选择限制为最多三个单选按钮中的两个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

向按钮组添加单选按钮会限制用户选择任何一个单选按钮(或不选择).

Adding radio buttons to a button group limits the user to selecting any one radio button (or none).

有没有办法限制用户最多选择 n 个单选按钮,其中 n 大于 1 但小于按钮总数?

Is there a way of limiting the user to selecting up to n radio buttons, where n is more than 1 but less than the total number of buttons?

在我目前的情况下,我正在将绘图上的 2D 点转换为 2D 绘图所描述的理论 3D 空间.任何一个 2D 点都可能代表 3D 空间中的多个点,因此用户可以选择通过指定最多 2 维 3D 点的值来约束 3D 点.显然所有 3 个维度都没有意义.使用单选按钮选择要约束的尺寸.

In my current case, I am converting a 2D point on a drawing into the theoretical 3D space that the 2D drawing describes. Any one 2D point might represent multiple points in 3D space, so the user has the option to constrain the 3D point by specifying the value of the 3D point in a maximum of 2 dimensions. Obviously all 3 dimensions does not make sense. The dimensions to be constrained are selected using radio buttons.

Swing 是否提供了一种简单的方法来实现这一点?

Does Swing allow for an easy way to implement this?

推荐答案

不要使用单选按钮.当您只想要一个组中的 1 个时使用单选按钮.

Don't use radio buttons. Radio buttons are used when you only want 1 from a group.

改为使用复选框.当您想要 0 个或多个时使用复选框.

Instead use check boxes. Check boxes are used when you want 0 or more.

这是一个允许您创建复选框组"的示例,以便您可以限制选择的最大数量:

Here is a example that allows you to create a "group of check boxes" so you can limit the maximum number of selections:

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class CheckBoxGroup
{
    private Set<GroupButtonModel> models = new HashSet<GroupButtonModel>();
    private int groupSize;

    public CheckBoxGroup(int groupSize)
    {
        this.groupSize = groupSize;
    }

    public void register(JCheckBox checkBox)
    {
        ButtonModel groupModel = new GroupButtonModel();
        groupModel.setSelected ( checkBox.getModel().isSelected() );
        checkBox.setModel( groupModel );
    }


    private class GroupButtonModel extends JToggleButton.ToggleButtonModel
    {
        @Override
        public void setSelected(boolean selected)
        {
            if (!selected)
            {
                models.remove( this );
                super.setSelected( selected );
                return;
            }

            //  Check number of currently selected check boxes

            if (models.size() == groupSize)
            {
                System.out.println("Only " + groupSize + " items can be selected");
            }
            else
            {
                models.add( this );
                super.setSelected( selected );
            }

        }
    }

    private static void createAndShowGUI()
    {
        JPanel panel = new JPanel();
        CheckBoxGroup group = new CheckBoxGroup(3);

        for (int i = 0; i < 10; i++)
        {
            JCheckBox checkBox = new JCheckBox( String.valueOf(i) );
            panel.add( checkBox );
            group.register( checkBox );
        }

        JFrame frame = new JFrame("Check Box Group");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( panel );
        frame.setLocationByPlatform( true );
        frame.pack();
        frame.setVisible( true );
    }

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

这篇关于如何在 Swing 中将选择限制为最多三个单选按钮中的两个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆