Java method call expectations

This is a java program with two buttons to change the integer value and display it

increase.addActionListener(incListener());
decrease.addActionListener(decListener());

Continue to display the error 'expected method call' I don't know what to do to solve the problem Any help would be appreciated. Thank you

Note: the complete code is attached below

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main extends JDialog {
public JPanel contentPane;
public JButton decrease;
public JButton increase;
public JLabel label;

public int number;

public Main() {
    setContentPane(contentPane);
    setModal(true);

    increase = new JButton();
    decrease = new JButton();
    increase.addActionListener(incListener());
    decrease.addActionListener(decListener());

    number = 50;
    label = new JLabel();
}

public class incListener implements ActionListener {
    public void actionPerformed (ActionEvent event) {
        number++;
        label.setText("" + number);
    }
}

public class decListener implements ActionListener {
    public void actionPerformed (ActionEvent event) {
        number--;
        label.setText("" + number);
    }
}

public static void main(String[] args) {
    Main dialog = new Main();
    dialog.pack();
    dialog.setVisible(true);
    System.exit(0);

}
}

Solution

Inclistener and decllistener are classes, not methods

attempt

increase.addActionListener(new incListener());

By the way, rename your class names so that they begin with uppercase letters

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>