Java Method Call Expected

后端 未结 7 1214
隐瞒了意图╮
隐瞒了意图╮ 2020-12-17 18:49

This is a java program with two buttons used to change an integer value and display it. However in IntelliJIDEA the two lines with

increase.addActionListene         


        
相关标签:
7条回答
  • 2020-12-17 19:15

    incListener and declListener are classes, not methods.

    Try

    increase.addActionListener(new incListener());
    

    btw, rename your classes names to make them start with an uppercase

    0 讨论(0)
  • 2020-12-17 19:26

    It's sad but I had to Google this same error... I was staring at a method that returned a class. I left off the new operator.

    return <class>(<parameters>) vs return new <class>(<parameters>)

    0 讨论(0)
  • 2020-12-17 19:28

    Whenever a string object is created using new operator a new object is created which is what your program is looking for. The following link is useful in learning about the difference between a string and a new string. What is the difference between "text" and new String("text")?

    0 讨论(0)
  • 2020-12-17 19:29

    substitute the lines with

    increase.addActionListener( new incListener());
    decrease.addActionListener( new decListener());
    
    0 讨论(0)
  • 2020-12-17 19:30

    It's simple: use new incListener() instead of incListener(). The later is trying to call a method named incListener, the former creates an object from the class incListener, which is what we want.

    0 讨论(0)
  • 2020-12-17 19:33

    Make these changes:

     public Main() {
        contentPane = new JPanel();
        setContentPane(contentPane);
        setModal(true);
    
        increase = new JButton("inc");
        decrease = new JButton("dec");
        contentPane.add(increase);
        contentPane.add(decrease);
        increase.addActionListener(new incListener());
        decrease.addActionListener(new decListener());
    
        number = 50;
        label = new JLabel(number+"");
        contentPane.add(label);
    }
    
    0 讨论(0)
提交回复
热议问题