简体   繁体   中英

How do I fix my code to count the number of clicks on each button?

I have written a program with two buttons for a Java class that I am taking. I now need to count and display the number of clicks each button gets. I have some code for counting clicks but am fairly certain that it is wrong.

The error I have is "identifier expected", how can I fix this?

Here is my updated code:

import java.awt.*;
import java.awt.event.*;

public class FinalProj1 extends Frame implements ActionListener,WindowListener {
    FinalProj1() {
        setTitle("Click Counter");
        setSize(400,400);
        show();
    }
    public static void main(String args[]) {
        Frame objFrame;
        Button objButton1;
        Button objButton2;
        TextField count = new TextField(20);
        TextField count2 = new TextField(20);
        Label objLabel;
        Label objLabel2;

        objFrame= new FinalProj1();
        objButton1= new Button("Agree");
        objButton2= new Button("Dissagree");
        objLabel= new Label();
        objLabel2= new Label();
        objLabel2.setText("Mexican Food Is Better Than Chineese Food");

        objButton1.setBounds(110,175,75,75);
        objButton2.setBounds(190,175,75,75);
        objLabel2.setBounds(80,95, 250,25);

        objFrame.add(objButton2);
        objFrame.add(objButton1);
        objFrame.add(objLabel2);
        objFrame.add(objLabel);
    }

    private int numClicks = 0;
    private int numClicks2 = 0;
    objButton1.addActionListener(this)
    objButton2.addActionListener(this)

    public void actionPerformed(ActionEvent e) {
        numClicks++;
        numClicks2++;
        count.setText("There are " + numClicks + " who agree");
        count2.setText("There are " + numClicks2 + " who dissagree");
    }
}

The error he's having ("identifier expected") is specified in the previous question .

You're getting this error because these two lines of code are outside any method or initializer block:

objButton1.addActionListener(this)
objButton2.addActionListener(this)

Put them in your constructor after creating the two controls and you should be fine.

First you should add the action listeners to each button,(explained above). However,you are incrementing both counts whenever you press one of the two buttons,which is wrong. So you should modify your action performed method to smthg like this

public void actionPerformed(ActionEvent e)
{
    if(e.getSource().equals("Agree"))//if you push the button agree,increment only                          numClicks
    numClicks++;
    if(e.getSource().equal("Disagree"))//if you click on disagree,increment numClicks2
    numClicks2++;
    count.setText("There are " + numClicks + " who agree");
    count2.setText("There are " + numClicks2 + " who dissagree");
}

EDIT-Btw,are you implementing the windows listener methods?You have to.

One approach is to have one actionListener for every button. Try this:

objButton1.addActionListener(myFirstActionListener)
objButton2.addActionListener(mySecondActionListener)

ActionListener myFirstActionListener = new ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {

        numClicks++;

  }
}

ActionListener mySecondActionListener = new ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {

        numClicks2++;

  }
}

Jeroen's answer is correct for solving your compilation error, so definitely do that first before you proceed.

In regards to your actual counters, the problem you're having is that both your numClicks and numClicks2 variables are being incremented simultaneously whenever either objButton1 or objButton2 is clicked. This is because they are being handled by the same event handler method. You have two choices:

Option 1: let the single event handler method handle both clicks, but distinguish between the two, and only increment the relevant counter, like so:

public void actionPerformed(ActionEvent e){
    if(e.getSource() == objButton1){
        numClicks++;
    } else {
        numClicks++;
    }
    // the rest if your method
}

Option 2: specify separate event handlers for each button, something like this:

objButton1.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
        numClicks++;
        // display your message to user
    }
});
objButton2.addActionListener(new ActionListrner(){
    public void actionPerformed(ActionEvent e){
        numClicks2++;
        // display your message to user;
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM