[英]Changing jbutton action in subclass
I'm trying to change button action in a subclass because the form is pretty much exactly the except one asks for an ID.我正在尝试更改子类中的按钮操作,因为该表单几乎完全是除了一个要求提供 ID 的表单。 What I i tried doing was making a
ActionListener
object and instantiating it to an object of an anonymous class like so:我尝试做的是制作一个
ActionListener
object 并将其实例化为匿名 class 的 object ,如下所示:
class ParentClass extends JPanel{
JButton button;
ActionListener buttonAction;
ParentClass{
button = new JButton("Parent Action");
buttonAction = new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("The button was clicked by the parent class");
}
};
button.add(buttonAction);
add(button);
}
}
class ChildClass extends ParentClass{
JButton button;
ActionListener buttonAction;
ChildClass{
super();
buttonAction = new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("The button was clicked by the child class");
}
};
}
}
public static void main(String[] args){
JFrame frame = new JFrame;
frame.add(new ChildClass());
frame.setSize(600, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
I was trying to use this method but the actionPerformed of buttonAction is never called.我试图使用此方法,但从未调用过 buttonAction 的 actionPerformed。 How can I make the button action different for the parent class and the subclass?
如何使父类 class 和子类的按钮操作不同?
You can let parent class implement ActionListener
, then use button.addActionListener(this)
in order to add the action to button.您可以让父 class 实现
ActionListener
,然后使用button.addActionListener(this)
将动作添加到按钮。 Then in the subclass @Override
actionPerformed
method:然后在子类
@Override
actionPerformed
方法中:
class ParentClass extends JPanel implements ActionListener
{
ParentClass()
{
JButton button = new JButton("something");
button.addActionListener(this);
add(button);
}
@Override
public void actionPerformed(ActionEvent event)
{
System.out.println("I am the parent.");
}
}
class SubClass extends ParentClass
{
SubClass()
{
super();//initialize button
}
@Override
public void actionPerformed(ActionEvent event)
{
System.out.println("I am the child.");
}
}
Another way is to add the ActionListener
and inside it, only call a method.另一种方法是添加
ActionListener
并在其中只调用一个方法。 Something like buttonPressed
.类似
buttonPressed
的东西。 Then in subclass @Override
buttonPressed
method.然后在子类
@Override
buttonPressed
方法中。
A complete example:一个完整的例子:
public class Test extends JFrame {
public Test() {
super("test");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new GridLayout(2, 1));
add(new ParentPanel());
add(new ChildPanel());
pack();
setLocationByPlatform(true);
}
private class ParentPanel extends JPanel implements ActionListener {
public ParentPanel() {
super(new BorderLayout());
JButton button = new JButton("My Class:" + getClass());
button.addActionListener(this);
add(button);
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Parent");
}
}
private class ChildPanel extends ParentPanel {
public ChildPanel() {
super();
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Child");
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new Test().setVisible(true));
}
}
I the method I posted works.我发布的方法有效。 The issue is if you don't remove and add the button to the subclass it doesn't change the action that will run
问题是,如果您不删除按钮并将其添加到子类,它不会更改将运行的操作
class ParentClass extends JPanel{
JButton button;
ActionListener buttonAction;
ParentClass{
button = new JButton("Parent Action");
buttonAction = new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("The button was clicked by the parent class");
}
};
button.add(buttonAction);
add(button);
}
}
So in the subclass what you would do is this:所以在子类中你要做的是:
class ChildClass extends ParentClass{
JButton button;
ActionListener buttonAction;
ChildClass{
super();
buttonAction = new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("The button was clicked by the child class");
}
};
button.removeActionListener(button.getActionListeners()[0]);
button.addActionListener(buttonAction);
}
}
I, however, do not know why but would like an explanation as to why buttonAction
had to be re-registered.但是,我不知道为什么,但想解释一下为什么必须重新注册
buttonAction
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.