• Post Reply Bookmark Topic Watch Topic
  • New Topic

Magic8ball actionlister

 
Vincent Tyson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to write a magic 8 ball program as a starter to intro to java, and I have a code that is working just fine, but I am trying to change the code so instead of an information window popping out, I would have a JLabel that would cycle the 8ball responses.

I'm really lost, and non of my books have the code I'm searching for, just JOptionPage.showmessage......

Maybe someone can help?


I need to swap out the JOptionPane line for something that will cycle the lbl2=output.

any help is great thanks
 
Vincent Tyson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FYI

This is my original code..

 
Campbell Ritchie
Marshal
Posts: 52536
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Don't make a frame implement action listener. Create a new Listener class. You can pass the label to the constructor. But labels are not intended for changing text. Try a text area or similar. Look in the Java® Tutorials; you will find that text components have setText or append methods which probably do exactly what you want.
Hint: pass the text component to the constructor of your listener class.
 
Campbell Ritchie
Marshal
Posts: 52536
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way: that block of if‑elses is pretty dreadful. Maybe you can put your responses into an array and use the number as the index of the array.
 
Campbell Ritchie
Marshal
Posts: 52536
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You know that myRandom.nextInt(20) returns the next int between 1 and 20, don't you? Lots of people know that






… but they know it wrongly
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!