更改子类中的jbutton动作

问题描述 投票:0回答:2

我正在尝试更改子类中的按钮动作,因为除了要求输入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
2个回答
0
投票

您可以让父类实现ActionListener,然后使用button.addActionListener(this)来将操作添加到按钮。然后在子类@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.");
     }
}

另一种方法是在其内部添加ActionListener,仅调用一个方法。类似于buttonPressed。然后在子类@Override buttonPressed方法中。

完整示例:

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

-1
投票

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


    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

© www.soinside.com 2019 - 2024. All rights reserved.