简体   繁体   English

如何使用 JOptionPane 为整数赋值?

[英]how to assign value to in integer with JOptionPane?

i declared a class then in that class i declared an int x and array of strings option i created a constructor of class in the consructor i used JOptionPane to choose from 3 options i want to assign int value to x for each option chosen我声明了一个类然后在那个类中我声明了一个 int x 和字符串数组选项我在构造函数中创建了一个类的构造函数我使用 JOptionPane 从 3 个选项中进行选择我想为每个选择的选项分配 int 值给 x

class A extends JFrame implements ActionListner, TextListener {
    ..........
    int x;
    String[] option = {"AA", "BB", "CC"};

    A() {
        ..........
        int x = JOptionPane.showOptionDialog(null, "Choose from", "Choose", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, option, option[0]);
    }

    public void B() {
        if(x==2) {
            ......
        }
public static void main(String[] args) {
   A lol = new A(arg[0]);
}

sorry cant disclose full code now when i choose option 3 (to assign value 2 to x),the function in B does not execute but when i assign the value 2 to x while declaring it, B always executes(even when i choose any other option in dialog box)抱歉,当我选择选项 3(将值 2 赋给 x)时,现在不能公开完整代码,B 中的函数不会执行,但是当我在声明它时将值 2 赋给 x 时,B 总是执行(即使我选择任何其他对话框中的选项)

any ideas what am i doing wrong任何想法我做错了什么

You're declaring a local variable in the constructor with the same name with the class varibale x, remove the keyword int.您在构造函数中声明了一个与类 varibale x 同名的局部变量,删除关键字 int。

A() {
   x = JOptionPane.showOptionDialog(null, "Choose from", "Choose", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, option, option[0]);
}

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

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