【发布时间】:2016-08-11 22:17:25
【问题描述】:
我正在开发一个自定义组件,但遇到了一个问题。这是组件:
public class MyComponent extends JPanel {
private final JButton jButton;
private final JLabel jLabel;
public MyComponent(){
jButton = new JButton();
//etc..
}
public void addActionListener(ActionListener l){
//The problem with this is that ActionEvent has source attribute
//set to jButton which is not desirable. How can I set it to this?
jButton.addActionListener(l);
}
//other component-specific methods
}
问题是我试图隐藏MyComponent 的实现细节。但是以这种方式设置监听器并不好,因为调用者可能会观察到source attrubte 是jButton。如何设置为封闭的MyComponent 实例?
【问题讨论】:
-
jButton.add(l);你不是说jButton.addActionListener(l);吗? -
@11thdimension 是的,已修复。谢谢。
-
问题是我试图隐藏 MyComponent 的实现细节。 == EventHandler
标签: java swing inheritance actionlistener