简体   繁体   中英

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

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)

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.

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

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