Java 如何使用JOptionPane将值赋给整数?

Java 如何使用JOptionPane将值赋给整数?,java,swing,jframe,Java,Swing,Jframe,我宣布了一个班级 然后在那个类中,我声明了一个int x和字符串数组选项 我创建了一个类的构造函数 在Constructor中,我使用JOptionPane从3个选项中进行选择 我想为每个选择的选项将int值指定给x class A extends JFrame implements ActionListner, TextListener { .......... int x; String[] option = {"AA", "BB", "CC"}; A()

我宣布了一个班级 然后在那个类中,我声明了一个int x和字符串数组选项 我创建了一个类的构造函数 在Constructor中,我使用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]);
}
对不起,不能透露完整的代码 现在,当我选择选项3(将值2赋给x)时,B中的函数不会执行 但当我在声明时将值2赋给x时,B总是执行(即使我在对话框中选择了任何其他选项)


如果知道我做错了什么

如果您在构造函数中声明了一个与类varibale x同名的局部变量,请删除关键字int

A() {
   x = JOptionPane.showOptionDialog(null, "Choose from", "Choose", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, option, option[0]);
}
如果这有助于解决问题,请告诉我。