【问题标题】:Changing jbutton action in subclass更改子类中的 jbutton 操作
【发布时间】:2019-11-09 23:30:47
【问题描述】:

我正在尝试更改子类中的按钮操作,因为表单几乎完全是除了请求 ID 之外的表单。我尝试做的是创建一个ActionListener 对象并将其实例化为一个匿名类的对象,如下所示:

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。如何使父类和子类的按钮操作不同?

【问题讨论】:

标签: java swing inheritance actionlistener anonymous-class


【解决方案1】:

您可以让父类实现ActionListener,然后使用button.addActionListener(this) 为按钮添加动作。然后在子类@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 这样的东西。然后在子类@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));
    }
}

【讨论】:

  • 考虑到您没有在SubClass 中重新分配动作侦听器,这项工作是否可行?我想它只会从 ParentClass 运行 actionperformed
  • @Deeswoc 你试过看看会发生什么吗?
  • 是的,运行它。它像我预期的那样工作。它从父类调用actionPerformed
  • @Deeswoc 你跑错了。我添加了一个完整的示例,它演示了我所说的内容以及如何实现这一目标。
【解决方案2】:

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


    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 必须重新注册。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-17
    • 1970-01-01
    • 2020-09-29
    • 1970-01-01
    • 1970-01-01
    • 2019-01-05
    • 1970-01-01
    相关资源
    最近更新 更多