Java btn.getclicked?

so how can i write if(btnAnswer1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lblAnswer.setText("correct");
}
}
});

You're seriously misunderstanding what's going on here - I'd seriously advise you to have a look in detail about swing, the events framework and the observer design pattern at a minimum, otherwise you're going to be hugely struggling from here on through misunderstanding.

The piece of code I gave you essentially calls the actionPerformed() method (where you write your setText) when that button is clicked. So whatever code you put there will execute whenever a user clicks the button. The concept here isn't really one of "if the button is clicked, do this" it's more "register the following piece of code to execute whenever the button is clicked." As for why it needs to be done this way, think of the control flow of your program. If you had something like:

Code:
lblQuestion.setText("What is a jump on a skateboard called?");
btnAnswer1.setText("Ollie");
if(btnAnswer1.isClicked()) {
      //Do something here when button is clicked
}
btnAnswer2.setText("Face plant");
btnAnswer3.setText(" Jump");

...as you originally proposed, then the code would execute, probably not execute the if statement (unless the user happened to be clicking on the button at the exact time that if statement was evaluated) and then carry on. This is why we need the concept of events, that switch from the concept of "if the button is pressed down now then do this" to "when the button is pressed, whether that's now or some time in the future, then execute this block of code."

Of course, the above pattern isn't baked in at language level, it's actually achieved by using a combination of other language level features that allow it to happen - but for now, just try and grasp the concept.
 
Ok, I sorta get this better now Ill research some more. Sorry for the inconvenience. I'm a beginner at learning this and don't understand everything the best
 
Back
Top Bottom