Hey guys

I recent wrote the deal or no deal game in Java for fun. It currently operates using a Command driven interface where users input numbers based on textual feedback.

My aim is to develop this into a full GUI interface, however, I have not met GUI's yet in my computer science course. Whilst I have some basic knowledge of how they work and the Syntax they use, I still need help.

What I want to do is create 21 buttons and assign each a value which is determined by a accessor. These numbers will range from 1 to 22.

I then also want for the button text to change once it has been clicked (through an event listener or similar). This will then start a new turn.

This is the basic functionality I require. I have looked on various different websites but it doesn't seem to help.

Figured I'd ask here.

Please don't ask for the GUI code I've already written as it's incomprehensive and not very good. Also assume the accessor produces an integer value between 1 and 22 as this is not important to the task in hand.

Thank you in advance

Are you sure you want to use an accessor, or do you just need to know the number associated with whatever button has just been pressed? In that case you can use an event handler. Here's a recent post that shows how (it stores the number in an array, but obviously you would do something else with it)
Changing the text is just a call to the button's setText method.

Basically I have the Game Logic in one class so that it is separate from the IO/GUI.

The accessor is located in the Game Logic Class. It's function is to locate the value of a 'Box'. e.g. box 1 has a value of £250,000 and Box 20 has a value of £0.01.

So when I use the accessor, it returns the box Value which is in the Box class.

What I really need help with is sorting the JButtons so that I can easily create them and store them in an array for easy manipulation. I'm very inexperienced with GUIs in Java at the moment though so Clear simple advice would be great.

Just create buttons and use a GridLayout to place them on the screen. Add listeners to each button as I explained before. When the use clicks a button the listener already has its number, which you can then pass to the Box class to get the value, which you can use to update the text of the button.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.