Swize
Swize

Reputation: 19

Java Button Problems

I have this code in which I am trying to open a class with the click of a button. When I run the program the buttons do nothing. I tested to see if I was referencing something wrong but without the buttons the class opened. Here is the code:

import java.awt.*;
import java.awt.event.*;    
import javax.swing.*; 

public class mainClass{
    public static void main(String args[]){

        JOptionPane frame = new JOptionPane(JOptionPane.INFORMATION_MESSAGE);

        Object[] options = {"Easy Mode","Medium Mode","Hard Mode"};

        int n = JOptionPane.showOptionDialog(frame,"Which difficulty do you want to play on?","Higher or Lower Game",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE, null, options,options[2]);

        String STRn = String.valueOf(n);

        if(STRn.equals(options[0])){

            EZMode ezmode = new EZMode();
            ezmode.easyMode();

        }else if(STRn.equals(options[1])){


        }else if(STRn.equals(options[2])){


        }           
    }

Upvotes: 0

Views: 44

Answers (2)

user9540189
user9540189

Reputation: 1

String STRn = String.valueOf(n);

Don't use this

Instead use respective values of n for conditional if statements' implementation

Upvotes: 0

Reimeus
Reimeus

Reputation: 159874

showOptionDialog returns the indices of the option selected not the value of the string itself. Try this

if (n == 0) {
   // do stuff for button 1
   ...

Upvotes: 3

Related Questions