简体   繁体   中英

Making use of custom JOptionPane Buttons? int deferenced error?

I'm sorry if this sounds like a stupid question, but I was searching everywhere on custom buttons in JOptionPane. I came across how to achieve special buttons but, I can't seem to use it in my program.

    int choice;
    Object[] doors = { "Door 1", "Door 2", "Door 3" };

    JFrame frame = new JFrame();

    input = "Which door do you choose?";
    choice = JOptionPane.showOptionDialog(frame, input, 
             "Doors",
             JOptionPane.DEFAULT_OPTION,
             JOptionPane.QUESTION_MESSAGE,
             null,
             doors,
             doors[2]);

    if (car == 1 && choice.equals(doors[0])) {
        open = 3; option = 2;
    } 
    if (car == 1 && choice.equals(doors[1])) {
        open = 3; option = 1;
    } 
    if (car == 1 && choice.equals(doors[2])) {
        open = 2; option = 1;
    } 
    if (car == 2 && choice.equals(doors[0])) {
        open = 3; option = 2;
    } 
    if (car == 2 && choice.equals(doors[1])) {
        open = 1; option = 3;
    } 
    if (car == 2 && choice.equals(doors[2])) {
        open = 1; option = 2;
    } 
    if (car == 3 && choice.equals(doors[0])) {
        open = 2; option = 3;
    } 
    if (car == 3 && choice.equals(doors[1])) {
        open = 1; option = 3;
    } 
    if (car == 3 && choice.equals(doors[2])) {
        open = 2; option = 1;
    }

Note: This isn't my entire program just the problematic aspect

The options in the dialog box show up perfectly, just there is and error that says "int cannot be deferenced". I think I used a faulty comparison but then how do I fix it?

You look to be trying to dereference an int, that you're trying to call a method on an int, choice, and you just can't do that with Java. Why not simply use choice in your doors array? doors[choice] ?

// first check that the JOptionPane wasn't closed by the user
if (choice != JOptionPane.CLOSED_OPTION) {
  String chosenDoor = doors[choice];
}

Or test choice as you're testing car using it as a number as an int.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM