gpt4 book ai didi

java - 父 JPanel - 如何监听子 JPanel 组件生成的事件?

转载 作者:行者123 更新时间:2023-12-02 04:43:07 24 4
gpt4 key购买 nike

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

此子级将用作另一个 JPanel 父级中的组件,该父级将监听来自子级的事件,而不是监听各个单选按钮。

我该怎么做?

到目前为止的代码 -

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);


}

}

最佳答案

我建议为组件提供充当其他感兴趣方注册兴趣的代理的功能。

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

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

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);

}
}
}

关于java - 父 JPanel - 如何监听子 JPanel 组件生成的事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15753536/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com