parent class 中的 ActionListener
ActionListener in parent class
我在 parent class 中与 Java 中的 ActionListener 作斗争,我尝试了很多可能的解决方案,但无法使其正常工作。这在这里也没有帮助:
Java actionlistener actionPerformed in different class
问题如下:
Class2 extends Class1,我在 Class2 中有一个按钮。一旦 Class2 中的按钮被按下,Class1 应该通过动作侦听器通知并执行事件。
我正在努力让 Class1 知道事件已经发生。这对我来说看起来很简单,但我还是很挣扎。
非常感谢您的帮助,谢谢!
Parent Class
package test;
//imports removed for better visibility
public class ParentClass extends JPanel implements ActionListener{
JFrame frame;
public void createParentGui() {
frame = new JFrame("Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel mainCard = new JPanel(new CardLayout(20, 20));
ChildClass card1 = new ChildClass();
mainCard.add(card1);
frame.add(mainCard, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e)
{
System.out.println("Button pressed, action!");
}
}
Child Class
package test;
//imports removed for better visibility
public class ChildClass extends ParentClass {
ActionListener listener = null; //this is probably not right, how to do
//with a local variable when passing it to the parent class?
public Child() {
createGui();
}
private void createGui() {
final JButton b = new JButton("press me");
b.addActionListener(listener);
add(b);
}
}
ChildClass
具有 ParentClass
的所有字段和方法(除了它自己独特的字段和方法)。这就是继承的工作原理。
因此,由于 ParentClass
是 ActionListener
,这意味着 ChildClass
也是。更具体地说,ChildClass
已经继承了ParentClass
的public void actionPerformed(ActionEvent e)
方法。
因此,把b.addActionListener(listener);
改成b.addActionListener(this)
。 (也可以把[=的listener
字段去掉10=])
新代码会将 "this" ChildClass
对象传递给 b
,然后只要按下按钮就会调用 actionPerformed(ActionEvent e)
。由于任何 ChildClass
对象都具有 ParentClass
的 actionPerformed(ActionEvent e)
,这意味着 ParentClass#actionPerformed(ActionEvent)
将被调用(如您所愿)。
我在 parent class 中与 Java 中的 ActionListener 作斗争,我尝试了很多可能的解决方案,但无法使其正常工作。这在这里也没有帮助: Java actionlistener actionPerformed in different class
问题如下:
Class2 extends Class1,我在 Class2 中有一个按钮。一旦 Class2 中的按钮被按下,Class1 应该通过动作侦听器通知并执行事件。
我正在努力让 Class1 知道事件已经发生。这对我来说看起来很简单,但我还是很挣扎。
非常感谢您的帮助,谢谢!
Parent Class
package test;
//imports removed for better visibility
public class ParentClass extends JPanel implements ActionListener{
JFrame frame;
public void createParentGui() {
frame = new JFrame("Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel mainCard = new JPanel(new CardLayout(20, 20));
ChildClass card1 = new ChildClass();
mainCard.add(card1);
frame.add(mainCard, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e)
{
System.out.println("Button pressed, action!");
}
}
Child Class
package test;
//imports removed for better visibility
public class ChildClass extends ParentClass {
ActionListener listener = null; //this is probably not right, how to do
//with a local variable when passing it to the parent class?
public Child() {
createGui();
}
private void createGui() {
final JButton b = new JButton("press me");
b.addActionListener(listener);
add(b);
}
}
ChildClass
具有 ParentClass
的所有字段和方法(除了它自己独特的字段和方法)。这就是继承的工作原理。
因此,由于 ParentClass
是 ActionListener
,这意味着 ChildClass
也是。更具体地说,ChildClass
已经继承了ParentClass
的public void actionPerformed(ActionEvent e)
方法。
因此,把b.addActionListener(listener);
改成b.addActionListener(this)
。 (也可以把[=的listener
字段去掉10=])
新代码会将 "this" ChildClass
对象传递给 b
,然后只要按下按钮就会调用 actionPerformed(ActionEvent e)
。由于任何 ChildClass
对象都具有 ParentClass
的 actionPerformed(ActionEvent e)
,这意味着 ParentClass#actionPerformed(ActionEvent)
将被调用(如您所愿)。