Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 05-31-2013, 01:56 PM   #1
Baseband Member
 
Join Date: Jan 2013
Location: Canada
Posts: 26
Default Java btn.getclicked?

So im making a trivia game and im stuck on a part

Im trying to make it so if the btnAnswer1.getClicked
lblAnswer.setText("Incorrect")

The problem is the btnAnswer1.getClicked isn't functional Do I have the wrong code?

if so what is the correct code to make this function
__________________

jpmasta12666 is offline   Reply With Quote
Old 05-31-2013, 11:01 PM   #2
Site Team
 
celegorm's Avatar
 
Join Date: Sep 2006
Posts: 10,713
Send a message via AIM to celegorm
Default Re: Java btn.getclicked?

usually the syntax is some form of "onClick(Event e)" for the function. If I'm correct that'll varry a bit based on what GUI library you are using but that should be the jist of it.
__________________

__________________
"as a fanboy i refuse to admit it and will pull countless things out of my butt to disprove it"

Team Thelegorm! Total Kills: 21 (i iz in uor profile, editsing your sigz)
celegorm is offline   Reply With Quote
Old 05-31-2013, 11:55 PM   #3
Baseband Member
 
Join Date: Jan 2013
Location: Canada
Posts: 26
Default Re: Java btn.getclicked?

I tryed that and im running Netbeans. it didn't work
jpmasta12666 is offline   Reply With Quote
Old 06-01-2013, 06:04 PM   #4
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: Java btn.getclicked?

Nowhere near enough information here to answer this - you need to post the exact code you're using!
__________________
Save the whales, feed the hungry, free the mallocs.
berry120 is offline   Reply With Quote
Old 06-01-2013, 07:16 PM   #5
Baseband Member
 
Join Date: Jan 2013
Location: /home/Spain/Andalucia
Posts: 36
Default Re: Java btn.getclicked?

Quote:
Originally Posted by berry120 View Post
Nowhere near enough information here to answer this - you need to post the exact code you're using!
I agree with my buddy in this point and btw, if you are using Netbeans there is a graphic designer for making forms
JackPanzer is offline   Reply With Quote
Old 06-01-2013, 11:57 PM   #6
Baseband Member
 
Join Date: Jan 2013
Location: Canada
Posts: 26
Default Re: Java btn.getclicked?

I am using the graphic designer.
here's the code:
int roll;
roll = (int)((3 -1 +1)*Math.random() +1);
if(roll==1){
lblQuestion.setText("What is a jump on a skateboard called?");
btnAnswer1.setText("Ollie");
if(btnAnswer1.isClicked)

btnAnswer2.setText("Face plant");
btnAnswer3.setText(" Jump");
btnAnswer4.setText("A Ninja");
}
jpmasta12666 is offline   Reply With Quote
Old 06-02-2013, 11:18 AM   #7
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: Java btn.getclicked?

What class type is btnAnswer1? If it's JButton, then there's no isClicked method (or field) which means that code won't compile. You probably want to add an action listener to the button:

Code:
btnAnswer1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        //Whatever you want to do when the button is clicked
    }
});
If the above code doesn't ring any bells, then you need to take a deeper look at how events work in swing.
__________________
Save the whales, feed the hungry, free the mallocs.
berry120 is offline   Reply With Quote
Old 06-02-2013, 12:05 PM   #8
Baseband Member
 
Join Date: Jan 2013
Location: Canada
Posts: 26
Default Re: Java btn.getclicked?

What if i want to add an if statement to it where would the () go
jpmasta12666 is offline   Reply With Quote
Old 06-02-2013, 12:14 PM   #9
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: Java btn.getclicked?

Quote:
Originally Posted by jpmasta12666 View Post
What if i want to add an if statement to it where would the () go
An if statement can be added as part of the normal code flow anywhere in that example. If you want one when you click on the button for instance:

Code:
btnAnswer1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        if(x==1) { //Whatever
            System.out.println("x was 1!");
        }
    }
});
Sorry about the spacing,
__________________
Save the whales, feed the hungry, free the mallocs.
berry120 is offline   Reply With Quote
Old 06-02-2013, 08:14 PM   #10
Baseband Member
 
Join Date: Jan 2013
Location: Canada
Posts: 26
Default Re: Java btn.getclicked?

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

jpmasta12666 is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 06:30 PM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0