更改子类中的 jbutton 操作

Changing jbutton action in subclass

我正在尝试更改子class 中的按钮操作,因为表单几乎与表单完全相同,只是其中一个要求提供 ID。我尝试做的是制作一个 ActionListener 对象并将其实例化为匿名 class 的对象,如下所示:

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);

}

我尝试使用此方法,但从未调用 buttonAction 的 actionPerformed。如何让父级 class 和子级 class 的按钮动作不同?

您可以让父级 class 实现 ActionListener,然后使用 button.addActionListener(this) 将动作添加到按钮。然后在subclass@OverrideactionPerformed方法中:

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.");
     }
}

另一种方法是添加 ActionListener 并在其中只调用一个方法。类似于 buttonPressed。然后在subclass@OverridebuttonPressed方法中.

一个完整的例子:

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));
    }
}

我发布的方法有效。问题是,如果您不删除按钮并将其添加到子类,它不会更改 运行

的操作

    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");
                }
            };
            button.removeActionListener(button.getActionListeners()[0]);
            button.addActionListener(buttonAction);
        }
    }

然而,我不知道为什么,但想知道为什么 buttonAction 必须重新注册。