This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • 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:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

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: 61753
193
  • 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: 61753
193
  • 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: 61753
193
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!