Forums Register Login

Implement showOptionDialog - create new game

+Pie Number of slices to send: Send
Hi

I am working on a game Minesweeper.
In there I have implemented a showMessageDialog and I would like to replace it with a showOptionDialog.
The problem is, to define the correct action after clicking Yes or No...

Thats how it is now with the showMessageDialog:


How do I have to implement the showOptionDialog to create a new game when the user clicks Yes and quit the application when the user clicks No?


I appreciate any advise and help...
I can post the whole section of the class if the code extract is not enough (was not allowed to add it as attachment).

Thank you
+Pie Number of slices to send: Send
If you look at the API JOptionPane#showConfirmDialog returns an int which indicates the user choice. The available choices are defined as static constants in the JOptionPane class itself. e.g. YES_OPTION, CANCEL_OPTION
Check the value of the choice and proceed accordingly.

Recommended reading http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html
+Pie Number of slices to send: Send
 

Maneesh Godbole wrote:Recommended reading http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html



Thank you Maneesh...
I already read the recommended oracle article. Because of that I was able to implement a little bit of the showOptionDialog.

When I say No or cancel the dialog the application is closing; so far so good.
I struggle to rebuild the application if I say Yes on the dialogue. The application quits as well when I say Yes (play again)...
+Pie Number of slices to send: Send
 

peter meyer wrote:I struggle to rebuild the application if I say Yes on the dialogue. The application quits as well when I say Yes (play again)...



Must be something to do with the changes you made, then. If you're still stuck would you care to post that part of the new code?
+Pie Number of slices to send: Send
Thank you Paul taking time for this. Here is the code:


(don't get confused with some German expressions or names in the code).

Thanks
+Pie Number of slices to send: Send
if (explodiert==1)
That looks as if you were writing C code where you use 0 for false and non‑zero for true. You don't in Java®. You either
  • 1: Make explodiert a boolean in which case you would write if (explodiert)...     or
  • 2: You create an enum to match explodiert so you might write if (explodiert == SEVERELY)... or if (explodiert == SLIGHTY)... or if (explodiert == NOT_AT_ALL)...
  • Note you should put spaces around the == operators.
    Another thing I can see is that your method is much much too long. Your if statements are difficult to follow and you will be unable to understand any of them in six months. That method should be divided into much smaller methods. Or you should think of a more object‑oriented solution. I am afraid I can't think of one at present, but you can do all sorts of things. You can cover the screen with multiple buttons, each representing a particular location. I presume you have Mine and Minesweeper classes. Think of how to match a Mine to its location. Do you have any method for recording location on your screen?

    More about ==.
    Never write == true and == false which are poor style and error‑prone because you might write = instead by mistake.
    Never
    if (b == true)...
    Always
    if (b)...
    Never
    if (b == false)...
    Always
    if (!b)...
    Notice the bang sign ! in the last statement.
    +Pie Number of slices to send: Send
    Thank you Richie...
    Well it seems I will go back to an earlier version I was working on...
    When evil is afoot and you don't have any arms you gotta be hip and do the legwork, but always kick some ... tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com


    reply
    reply
    This thread has been viewed 459 times.
    Similar Threads
    Minesweeper Game
    Too many ActionEvents after button press.
    implementing objects in a private class to an array constructor...
    Multiplication Table
    Trouble with firingTableData.
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Mar 19, 2024 02:58:02.