[英]how to assign value to in integer with JOptionPane?
我聲明了一個類然后在那個類中我聲明了一個 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]);
}
抱歉,當我選擇選項 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]);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.