1

ラジオボタンを含む動的に追加されたパネルと、ラジオボタンにバインドされたいくつかのラベルを持つパネルがたくさんあります。選択したラジオボタンにラベルがバインドされたコンテナを取得するボタンがあるとします。または、選択したラジオボタンにデータバインドする別の言葉を言いましょう。しかし、このコンテナを取得する方法は?

これを実行しようとするコードを次に示します (実際、これは UI (ビュー) 側で何が起こっているかを示すためのスタブです)。

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();
        }
    }
}
4

2 に答える 2

4

データ駆動型の Q&A プログラムに取り組んでいるようですね。示されているアプローチは、コンテナのメソッドから返された配列を再帰的にトラバースすることによって技術的に可能ですがgetComponents()、結果は包含と継承の階層を混乱させます。また、スケーリングも不十分です。

代わりに、ここで提案されているように、モデル (文字列、選択) とビュー (ラベル、ボタン) を分離します。表示されている通知方法のいずれかを使用して、ビューにそのモデルをリッスンさせます。このようにして、ビューの各インスタンスは、独自のボタンと、どのボタンが選択されているかを認識します。標準の Swing コンポーネントのほとんどは、この分離可能なモデル アーキテクチャの良い例です。

補遺: 私は暗黙の設計上の制限を支持するのは気が進まないが、ここに好都合なアプローチがある。

で、参照をクライアント プロパティとしてPanelWithRadioButton追加します。panelWithTwoLabels

rButton.putClientProperty("labels", panelWithTwoLabels);

ではactionPerformed()、プロパティを使用して以下を取得しますpanelWithTwoLabels

PanelWithTwoLabels panelWithLabels =
    (PanelWithTwoLabels) btn.getClientProperty("labels");
于 2013-08-19T16:30:40.680 に答える
2

ご質問とは関係ありませんが...

panel.updateUI();

そのように updateUI() メソッドを使用しないでください。updateUI() メソッドは、LAF が変更されたときに Swing によって内部的に使用されます。

可視 GUI にコンポーネントを追加するときは、次を使用する必要があります。

panel.revalidat();
panel.repaint(); // sometimes needed
于 2013-08-19T18:16:22.293 に答える