私はカスタムコンポーネントを開発中で、問題が発生しました。ActionEventのイベントソースをカスタマイズする方法は?
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
アタッチがjButton
であることを観察する可能性があるので、このようにリスナーを設定することは良くありません。どのようにして囲むMyComponent
インスタンスに設定できますか?
'jButton.add(L)で
;'は 'jButton.addActionListener(l)を意味するものではありませんか。'? – 11thdimension
@ 11thdensionはいはい、修正されました。ありがとうございました。 – user3663882
問題は、MyComponentの実装の詳細を隠そうとしていることです。 == [EventHandler](http://stackoverflow.com/a/9007348/714968) – mKorbel