Frage

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();
        }
    }
War es hilfreich?

Lösung

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! ");
    }
});
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top