Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Magic8ball actionlister  RSS feed

 
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..

 
Marshal
Posts: 60130
188
  • 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: 60130
188
  • 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: 60130
188
  • 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
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!