タイトルが少し曖昧なので、コードで説明します。私が持っているとしましょう
Class A extends JFrame implements ActionListener{
B b;
Class B extends JPanel{
public JButton button;
public B(A a){
button = new JButton();
button.addActionListener(a);// I want to process all actionEvents in A
this.add(button);
}
}
public A(){
b = new B(this);
//irrelevant codes omitted for brevity
}
public void actionPerformed(ActionEvent e){
//Here's the question:
//Suppose I have a lot of Bs in A,
//how can I determine which B the button
//that triggers this callback belongs to?
}
}
それで、それにする方法はありますか?または私の考えは間違っていますか?どんな考えでも大歓迎です。
編集:私が最終的に行うことは、has(JComponent component)
クリック可能なすべてのBと比較する関数をBに追加することです。getParent()
JPanelが複数のレイヤーを持っていると、それが参照しているパネルのレイヤーを特定するのが難しく、カプセル化の考え方に反するため、が厄介になります。