简体   繁体   English

利用自定义的JOptionPane按钮? 诠释错误?

[英]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. 很抱歉,这听起来像是一个愚蠢的问题,但是我在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". 对话框中的选项完美显示,只有错误提示“不能引用int”。 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. 您似乎试图取消对一个int的引用,即试图对一个int,choice进行调用的方法,而Java却无法做到这一点。 Why not simply use choice in your doors array? 为什么不在门阵列中简单地使用选择呢? doors[choice] ? 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. 或在测试汽车时使用它作为整数来测试选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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