This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin 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
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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: 62893
203
  • 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: 62893
203
  • 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: 62893
203
  • 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
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!