سؤال

I've been trying to implement this action listener as stated above but keep receiving two errors:

-Cannot instantiate the type ActionListener
-void is an invalid type for the variable incrementAction

I've been looking up similar examples but they all seem to point to the same method of implementing it. This is where I've got to.

        increment.addActionListener(new ActionListener());{
        public void incrementAction(ActionEvent e){
            this.incrementCount();
            this.setTextField();
        }
    }
هل كانت مفيدة؟

المحلول

The signature of the ActionListener method is:

public void actionPerformed(ActionEvent e)

JButton increment  = new JButton();
increment.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("ActionEvent received! ");
    }
});
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top