父级JPanel-如何侦听子级JPanel组件生成的事件? [英] Parent JPanel - How to listen to the events generated by components of a Child JPanel?

查看:138
本文介绍了父级JPanel-如何侦听子级JPanel组件生成的事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个JPanel Child,其中包含几个单选按钮.每当单击单选按钮时,我也希望从Child生成一个ActionEvent.此动作事件应包含"对实际生成事件的按钮的引用.

I made a JPanel Child which contains a couple of radio buttons in it. Whenever a radio button is clicked i want an ActionEvent to be generated from the Child also. This action event should "contain" a reference to the button which actually generated an event.

此子级将用作另一个JPanel父级内部的组件,该JPanel父级将侦听该子级的事件,而不是侦听单个单选按钮.

This Child will be used as a component inside another JPanel Parent which will listen to the events from the Child, instead of listening to the individual radio buttons.

我该怎么做?

到目前为止的代码-

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

public class RadioListener extends JPanel implements ActionListener{

public static final String id = "id";

public RadioListener(){

    for(int i = 1; i < 5; i++){
        JRadioButton jrb = new JRadioButton(i + "", false);
        jrb.putClientProperty(id, i);
        this.add(jrb);
        jrb.addActionListener(this);

    }

}


public void actionPerformed(ActionEvent e){

    JRadioButton jrb = (JRadioButton) e.getSource(); 
    Integer id = (Integer) jrb.getClientProperty(RadioListener.id);
    System.out.println("id " + id);

}


public static void main(String[]args){

    JFrame frame = new JFrame("Radio buttons");
    frame.getContentPane().setLayout(new FlowLayout());
    frame.setSize(400, 100);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(new RadioListener());
    frame.setVisible(true);


}

}

推荐答案

我建议为该组件提供代理功能,以使其他有兴趣的各方进行注册.

I'd recommend providing the ability for the component to act as a proxy for other interested parties to register interest.

这意味着您不需要公开其他组件不应该调用或无法访问的方法/组件.

This means you don't need to expose methods/components that other components shouldn't be calling or have access to.

您还应该为侦听器使用内部类,因为它们将防止暴露其他人不应该访问的其他方法

You should also make use of inner classes for listeners, as they will prevent the exposure of other methods that others shouldn't have access to

public class ProxyActionListener extends JPanel {

    public static final String id = "id";
    private List<JRadioButton> buttons;

    public ProxyActionListener() {

        buttons = new ArrayList<>(25);
        ActionHandler actionHandler = new ActionHandler();

        for (int i = 1; i < 5; i++) {
            JRadioButton jrb = new JRadioButton(i + "", false);
            jrb.putClientProperty(id, i);
            this.add(jrb);
            jrb.addActionListener(actionHandler);
            buttons.add(jrb);

        }

    }

    public void addActionListener(ActionListener listener) {
        for (JRadioButton btn : buttons) {
            btn.addActionListener(listener);
        }
    }

    public void removeActionListener(ActionListener listener) {
        for (JRadioButton btn : buttons) {
            btn.removeActionListener(listener);
        }
    }

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                ProxyActionListener pal = new ProxyActionListener();
                pal.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        JRadioButton jrb = (JRadioButton) e.getSource();
                        Integer id = (Integer) jrb.getClientProperty(ProxyActionListener.id);
                        System.out.println("Proxy- id " + id);
                    }
                });

                JFrame frame = new JFrame("Radio buttons");
                frame.getContentPane().setLayout(new FlowLayout());
                frame.setSize(400, 100);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.getContentPane().add(pal);
                frame.setVisible(true);
            }
        });
    }

    protected class ActionHandler implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {

            JRadioButton jrb = (JRadioButton) e.getSource();
            Integer id = (Integer) jrb.getClientProperty(ProxyActionListener.id);
            System.out.println("id " + id);

        }
    }
}

这篇关于父级JPanel-如何侦听子级JPanel组件生成的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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