• Post Reply Bookmark Topic Watch Topic
  • New Topic

GUI exit button  RSS feed

 
Will Ferrars
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've spent ages trying to implement an exit button into this code. Its the start of a GUI for a slot machine (I apologise if you are tired of seeing slot machine code), the actual machine code is in a separate class and I haven't linked them yet. My question is where/how would the exit button be added? I've tried and failed a lot of times and its driving me crazy.
Thank you for any help

 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you need an exit button for? You already have the EXIT_ON_CLOSE thing - isn't that sufficient? If not, you could add a menubar with an exit menu, so that it works like any old desktop app.
 
Will Ferrars
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its a usability thing. I'll look into a menubar but I think its slightly too much as I only need the play/spin button and the exit one currently.
 
Campbell Ritchie
Marshal
Posts: 55781
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Good thing you are running the slot machine from a different class; I am sure that is the right way to do it. Some good code there.
I would suggest you avoid System.exit and EXIT. System.exit can be vicious, stopping all threads, even if they haven't completed their tasks. I suggest you need to create a Listener which calls frame.dispose(), and change the close operation to DISPOSE_ON_CLOSE. Use a separate class for the Listener, not addActionListener(this). Even though addActionListener(this) won't compile in your current code. You may have to declare the frame final.
You appear to be adding the panels to each other. I would have though you add them to the frame directly, and don't use set preferred size. Let the layout set the sizes. I couldn't get your layout to work, and eventually tried a grid layout (3, 1) on the frame. You might consider grid layout on the panels; it will divide them in to equally‑sized parts. More about layouts here#.

I would use a menu bar with a menu called file, which you can add a button called exit to, with that listener added.

I shall move this discussion to our GUIs forum
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, usability also means not surprising your users - and what users know is to end an app by closing the window or selecting the Exit menu.

But OK - it seems you have a JPanel for the buttons already at the bottom of the window. What prevents you from butting the buttons there?
 
Campbell Ritchie
Marshal
Posts: 55781
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It took me 14 lines to add a menu bar, file menu and exit button, including the listener.
You can consider a close() method, which calls saveFileOptionally() and frame.dispose(), if there is ever anything to save. You would have to write the save method yourself Use one big colourful button for SPIN and the boring menu for exit.
 
Rob Camick
Ranch Hand
Posts: 2801
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out Closing an Application for some ideas.
 
Will Ferrars
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So would the menubar be in a public class with the actionlistener in another class below it? And both of these be below the main class containing the panels
 
Paul Clapham
Sheriff
Posts: 22521
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'd have to explain what you mean when you say a class is "below" another class.
 
Will Ferrars
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I may be getting confused with methods.
 
Campbell Ritchie
Marshal
Posts: 55781
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:Check out Closing an Application for some ideas.
Nice link
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!