Swing:如何将容器绑定到 JRadioButton? [英] Swing: how to get container bind to JRadioButton?

查看:56
本文介绍了Swing:如何将容器绑定到 JRadioButton?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆动态添加的面板,其中包含单选按钮和一个带有多个标签的面板绑定到单选按钮.假设我有一个按钮,它应该检索带有标签绑定到选定单选按钮的容器,或者换个说法 - 数据绑定到选定单选按钮.但是如何获得这个容器呢?

I have a bunch of dynamically added panels that contain radio button and a panel with several labels bind to radio button. Suppose I have a button that should retrieve container with labels bind to selected radio button or let's say another words - data bind to selected radio button. But how to get this container?

这是我尝试执行此操作的代码(实际上这是一个存根,用于显示 UI(视图)端发生的情况):

Here is my code where I try to do this (in fact this is a stub to show what is going on on UI (view) side):

public class Test extends JFrame {
    public static ButtonGroup radioButtons = new ButtonGroup();

    public Test() {
        super("Test");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setSize(200, 300);

        final JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        JButton addButton = new JButton("Add");
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                panel.add(new PanelWithRadioButton("text1", "text2"));
                panel.revalidate();
            }
        });
        panel.add(addButton);

        JButton infoButton = new JButton("Info");
        infoButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                for(Enumeration<AbstractButton> myRadioButtons = radioButtons.getElements();
                    myRadioButtons.hasMoreElements();) {
                    JRadioButton btn = (JRadioButton) myRadioButtons.nextElement();

                    if(btn.isSelected()) {
                        PanelWithTwoLabels panelWithLabels = (PanelWithTwoLabels) btn.getComponent(1); //Trying to get Component bind to selected JRadioButton
                        JOptionPane.showMessageDialog(null, "Text1: " + panelWithLabels.getLabel1Text() + ", Text2: " + panelWithLabels.getLabel2Text());
                    }
                }
            }
        });
        panel.add(infoButton);

        getContentPane().add(panel);
        setVisible(true);
    }

    public static void main(String[] args) {
        new Test();
    }

    //JRadioButton + Panel with two text fields
    private class PanelWithRadioButton extends JPanel {
        private JRadioButton rButton;
        private PanelWithTwoLabels panelWithTwoLabels;

        public PanelWithRadioButton(String text1, String text2) {
            setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
            panelWithTwoLabels = new PanelWithTwoLabels(text1, text2);
            rButton = new JRadioButton();
            rButton.add(panelWithTwoLabels); //Bind Component to JRadioButton
            radioButtons.add(rButton);

            add(rButton);
            add(panelWithTwoLabels);
        }
    }

    private class PanelWithTwoLabels extends JPanel {
        private JLabel label1;
        private JLabel label2;

        public PanelWithTwoLabels(String text1, String text2) {
            setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
            label1 = new JLabel(text1);
            label2 = new JLabel(text2);

            add(label1);
            add(label2);
        }

        private String getLabel1Text() {
            return label1.getText();
        }

        private String getLabel2Text() {
            return label2.getText();
        }
    }
}

推荐答案

听起来您正在开发一个数据驱动的问答程序.虽然通过递归遍历从容器的 getComponents() 方法返回的数组,所展示的方法在技术上是可行的,但结果却混淆了容器 &继承层次结构;它的扩展性也很差.

It sounds like you're working on a data driven Q&A program. While the approach shown is technically possible by recursively traversing the array returned from the container's getComponents() method, the result confuses the containment & inheritance hierarchies; it also scales poorly.

相反,按照此处的建议,将模型(字符串、选择)和视图(标签、按钮)分开.让视图使用所示的通知方法之一来监听它的模型.这样,视图的每个实例都知道自己的按钮以及选择了哪个按钮.几乎所有标准的 Swing 组件都是一个很好的例子 可分离的模型架构.

Instead, separate the model (strings, selection) and view (labels, buttons), as suggested here. Let the view listen to its model using one of the notification approaches shown. In this way, each instance of the view knows its own buttons and which button is selected. Almost any of the standard Swing components is a good example of this separable model architecture.

附录:虽然我不愿意认可隐含的设计限制,但这里有一个权宜之计.

Addendum: While I'm reluctant to endorse the implicit design limitation, here's an expedient approach.

PanelWithRadioButton 中,添加对 panelWithTwoLabels 的引用作为客户端属性:

In PanelWithRadioButton, add a reference to panelWithTwoLabels as a client property:

rButton.putClientProperty("labels", panelWithTwoLabels);

actionPerformed()中,使用属性检索panelWithTwoLabels:

PanelWithTwoLabels panelWithLabels =
    (PanelWithTwoLabels) btn.getClientProperty("labels");

这篇关于Swing:如何将容器绑定到 JRadioButton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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