Help With Button Pressed
Author |
Message |
gagster1233
|
Posted: Fri Jan 07, 2011 1:58 pm Post subject: Help With Button Pressed |
|
|
i am having trouble getting my number program to work for my grade 11 final project.
i am trying to get a program to start when a button is pressed and to restart whenever the button is pressed again.
this is my code so far any help would be greatly appreciated
Quote: import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class ButtonPressDemo
{
public static void main (String [] args)
{
//inpit
Button b;
ActionListener a = new MyActionListener ();
Frame f = new Frame ("Java Applet");
f.add (b = new Button ("Play Again"), BorderLayout.NORTH);
b.setActionCommand ("Play Again");
b.addActionListener (a);
f.add (b = new Button ("Good Day"), BorderLayout.CENTER);
b.addActionListener (a);
f.add (b = new Button ("Good Bye"), BorderLayout.SOUTH);
b.setActionCommand ("Exit");
b.addActionListener (a);
f.pack ();
f.show ();
}
}
//Actions//
class MyActionListener implements ActionListener
{
public void actionPerformed (ActionEvent ae)
{
String s = ae.getActionCommand ();
if (s.equals ("Exit"))
{
System.exit (0);
}
else if (s.equals ("Play Again"))
{
while (true)
{
class probability
{
public void main(String[]args)
{
random();
}
public void random()
{
Random dice = new Random();
int num,num2,num3,num4,score,count;
score=0;
count=0;
if(score>0)
{
num =1+dice.nextInt(6);
num2=1+dice.nextInt(6);
num3=1+dice.nextInt(6);
num4=1+dice.nextInt(6);
System.out.println("Your numbers are "+num+","+num2+","+num3+","+num4);
if (num==num2 && num2==num3 && num3==num4)
{
System.out.println("You got four of a kind.(Worth 100 points.) YOU WIN");
score=score+100;
}
else if(num==num2-1 && num2==num3-1 && num3==num4-1 || num==num2+1 && num2==num3+1 && num3==num4+1)
{
System.out.println("You got a royal flush. (Worth 75 points.");
score=score+75;
}
else if (num==num2 && num2==num3 || num==num2 && num2==num4 || num==num3 && num3==num4 || num2==num3 && num3==num4)
{
System.out.println("You got three of a kind.(Worth 50 points.)");
score=score+50;
}
else if(num==num2+1 && num2==num3+1 || num==num2+1 && num2==num4+1 || num==num3+1 && num3==num4+1 || num2==num3+1 && num3==num4+1)
{
System.out.println("You got a flush. (Worth 40 points.)");
score=score+40;
}
else if (num==num2 || num==num3 || num==num4 || num2==num3 || num2==num4 || num3==num4)
{
System.out.println("You got two of a kind.(Worth 25 points.)");
score=score+25;
}
count++;
}
System.out.println("It took you "+count+" rolls to get 100 points");
}
}
{
System.out.println (s + " clicked");
}
}
}
}
}
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Tony
![](http://wiki.compsci.ca/images/f/f4/OniTony.gif)
|
Posted: Fri Jan 07, 2011 2:30 pm Post subject: Re: Help With Button Pressed |
|
|
gagster1233 @ Fri Jan 07, 2011 1:58 pm wrote:
i am trying to get a program to start when a button is pressed and to restart whenever the button is pressed again.
And what does the your program do now? |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
![](images/spacer.gif) |
copthesaint
![](http://compsci.ca/v3/uploads/user_avatars/15853548854c9c056fda48d.jpg)
|
Posted: Mon Jan 10, 2011 3:12 am Post subject: Re: Help With Button Pressed |
|
|
So I asume you want to reset the program to a certain state if you press this button no mater what state the button is in. That should be easy enough.
Let me just CnP your code in to net beans and see what ur doin.
hmm.Now I see what your trying to be, however this just wont work.
Now to explain why this does not work. first of all the ActionListener is just not initialized this way.
The class you are calling "ActionListener" is a blueprint and a is the object that you name the object you are making. Now that you have your "ActionListener" blueprint, you cant just go and build a "MyActionListener" object with the ActionListener blueprint. Those are too different blueprints and the other GUI wont accept your object.
The ActionListener create a listener for a gui component to tell you the behaviour of a Gui Item, like if it is highlighted, pushed, ect. and the ActionListener maps this.
Read this tutorial here try it again and post things that you've tried and we'll help you best we can, provided we have the time to . |
|
|
|
|
![](images/spacer.gif) |
DemonWasp
|
Posted: Mon Jan 10, 2011 10:54 am Post subject: Re: Help With Button Pressed |
|
|
copthesaint @ Mon Jan 10, 2011 3:12 am wrote: The class you are calling "ActionListener" is a blueprint and a is the object that you name the object you are making. Now that you have your "ActionListener" blueprint, you cant just go and build a "MyActionListener" object with the ActionListener blueprint. Those are too different blueprints and the other GUI wont accept your object.
Err, what? Nowhere in that gibberish did you make much sense, but when you did, you were wrong.
He has implemented class MyActionListener, which implements the ActionListener interface. Having run his code, it compiles and works just fine. In fact, I'm hard-pressed to say what's going wrong, because it seems like his code does exactly what he wants it to do. |
|
|
|
|
![](images/spacer.gif) |
2goto1
![](http://compsci.ca/v3/uploads/user_avatars/787605484d25cfd31b548.jpg)
|
Posted: Mon Jan 10, 2011 11:31 am Post subject: Re: Help With Button Pressed |
|
|
DemonWasp @ Mon Jan 10, 2011 10:54 am wrote: copthesaint @ Mon Jan 10, 2011 3:12 am wrote: The class you are calling "ActionListener" is a blueprint and a is the object that you name the object you are making. Now that you have your "ActionListener" blueprint, you cant just go and build a "MyActionListener" object with the ActionListener blueprint. Those are too different blueprints and the other GUI wont accept your object.
Err, what? Nowhere in that gibberish did you make much sense, but when you did, you were wrong.
He has implemented class MyActionListener, which implements the ActionListener interface. Having run his code, it compiles and works just fine. In fact, I'm hard-pressed to say what's going wrong, because it seems like his code does exactly what he wants it to do.
Hard to say what issue gagster was having in the first place since gagster didn't really describe their roadblock or question. I'm not very familiar with Java Swing, but perhaps the fact that the same ActionListener instance is being assigned to both buttons may be causing the issue. Would it be wiser in Java to create separate ActionListener instances per button, rather than using the same ActionListener instance for both buttons? |
|
|
|
|
![](images/spacer.gif) |
DemonWasp
|
Posted: Mon Jan 10, 2011 11:56 am Post subject: RE:Help With Button Pressed |
|
|
In general, it's wisest to use separate ActionListeners for separate items (just for the sake of code separation). However, I think OP's problem lies in the fact that the definition of class "probability" occurs inside a while ( true ) loop that doesn't do anything else, other than produce output, and that while the event-handler thread is stuck in that loop, it will not respond to other mouse events, such as clicking on the "goodbye" button.
His problem should become obvious if he indents his code properly. It should be fixable pretty easily with some braces relocated. |
|
|
|
|
![](images/spacer.gif) |
copthesaint
![](http://compsci.ca/v3/uploads/user_avatars/15853548854c9c056fda48d.jpg)
|
Posted: Mon Jan 10, 2011 5:29 pm Post subject: RE:Help With Button Pressed |
|
|
ah ha, opps. Demonwasp, dont be too hard on me I was tired when I answered that! lol...
-_- no more 3am answers from me. |
|
|
|
|
![](images/spacer.gif) |
gagster1233
|
Posted: Thu Jan 13, 2011 1:56 am Post subject: Re: Help With Button Pressed |
|
|
i guess i wasnt specific enough, when i hit play again a window pops up that says "play again pressed" instead of running the program which is the probablility game |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
2goto1
![](http://compsci.ca/v3/uploads/user_avatars/787605484d25cfd31b548.jpg)
|
Posted: Fri Jan 14, 2011 8:08 am Post subject: Re: Help With Button Pressed |
|
|
That's strange. Where in your program does the text "play again pressed" get displayed? I don't see anything you've written in your code that would indicate that.
One thing in your program that looks unusual is your ActionListener implementation. See snippet below:
Code: | else if (s.equals ("Play Again"))
{
while (true)
{
class probability
{
public void main(String[]args)
{
random();
} |
As-is, it looks like you're trying to run the stuff where the code "class probability" starts. It won't get called as-is.
Is there any reason why you're declaring a class within your while block? That seems a little strange. You'd be better off writing your classes separate from one another, so that in your source file you have all 3 classes defined separately. There's no hard and fast rule that says that you have to do this, but for new programmers, it's easier and cleaner to work this way:
code: | public class ButtonPressDemo {
}
class MyActionListener {
}
class probability {
}
|
Then in your MyActionListener method, where Play Again is clicked, simply instantiate your probability class. I.e.:
code: | probability myProbability = new probability(); |
Once instantiated, you could call your random method:
code: | myProbability.random(); |
Ditch your probability.main() method, it will never get automatically called. Note that your Java program can only have public static void main(String[] args), which is your ButtonPressDemo.main(). |
|
|
|
|
![](images/spacer.gif) |
|
|