簡體   English   中英

更改void方法內的變量值以在Java外使用?

[英]Changing a variable value inside a void method to be used outside in Java?

我是Java新手。 我進行了搜索,但是沒有找到明確的答案。

有沒有辦法在void方法內部更改預定義變量的值,並通過另一種void方法使用新值?

我需要什么:在Eclipse WindowBuilder中,單擊按鈕應更改在此按鈕外部定義的變量的值。 因此,我可以在單擊另一個按鈕時使用新值。 但是,發生的是,當我單擊另一個按鈕時,將使用最初定義的值,而不是更改后的值。

更新:示例代碼:

private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        String x = "0";

        JButton btn1 = new JButton("Button 1");
        btn1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String x = "1";
                textField1.setText(x);              
            }
        });
        btn1.setBounds(102, 134, 89, 23);
        frame.getContentPane().add(btn1);

        JButton btn2 = new JButton("Button 2");
        btn2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                textField2.setText(x);
            }
        });
        btn2.setBounds(232, 134, 89, 23);
        frame.getContentPane().add(btn2);

        textField1 = new JTextField();
        textField1.setBounds(159, 85, 86, 20);
        frame.getContentPane().add(textField1);
        textField1.setColumns(10);

        textField2 = new JTextField();
        textField2.setColumns(10);
        textField2.setBounds(159, 179, 86, 20);
        frame.getContentPane().add(textField2);
    }

在此處輸入圖片說明

因此,這里x初始化為"0" 單擊按鈕1 ,將x更改為"1" 然后,單擊按鈕2 ,得到的初始值為"0"而不是"1"

在您的代碼中,您正在使用局部變量x

JButton btn1 = new JButton("Button 1");
    btn1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            final String x = "1";
            textField1.setText(x);              
        }
    });

在您聲明的內部類方法ActionListener.actionPerformed之外,此變量將不存在。

您需要在符合您需要的范圍內聲明變量。

在這種情況下,您需要使用一個變量實例(請參見下面的注釋),因此在方法initialize外部聲明String x作為實例的一部分。

String x; //instance variable to be shared
private void initialize() {
    ...
    JButton btn1 = new JButton("Button 1");
    btn1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            x = "1";
            textField1.setText(x);              
        }
    });

    JButton btn2 = new JButton("Button 2");
    btn2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            textField2.setText(x);
        }
    });

}

注意:您不能簡單地將其放在initialize方法中,因為您需要將其作為final放置在內部類中使用,但是您正在為其設置值,因此在您的情況下是不可能的。

PS:

請注意,您正在隱藏initialize方法的String x

String x = "0";

JButton btn1 = new JButton("Button 1");
btn1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        String x = "1";
        textField1.setText(x);              
    }
});

在click方法中,這將不使用當前為"0"x ,它將是完全不同的實例(即使名稱相同)。 因此, 您還將需要刪除該聲明,因為這將隱藏我剛剛聲明的實例變量。

什么是Java中的影子變量

但是簡短的描述是:

如果存在另一個名稱更接近作用域的變量,則該變量將被覆蓋

一個顯示此陰影的小例子是

public class Main {

    String x = "a";
    public static void main(String[] args) {
        new Main();
    }

    public Main(){
        System.out.println(x); //"a"
        String x = "b";
        System.out.println(x); //"b"
        new Thread(new Runnable() {
            public void run() {
                String x = "c"; 
                System.out.println(x); //"c"
            }
        }).start();
        System.out.println(x); //"b"
    }

    public void method(){
        System.out.println(x); //"a"
    }
}

該變量必須被定義為該類的實例變量,而不是方法中的局部變量。 這樣,所有按鈕都將能夠訪問變量,而不僅僅是封裝在方法中的那些按鈕。

編輯:

這是一些示例代碼,可以准確顯示我的意思。 當前,您正在像這樣定義變量x:

final void initialize(){
    String x = "0"; //x is defined within the scope of this method only.
} 

這將變量x限制為僅存儲在initialize方法中。 但是對於您的情況,您希望這樣定義x:

String x; //Instance variable which is available to the entire class
final void initialize(){
    x = "0"; //modifies the instance variable for the entire class 
}

您在聲明一個NEW變量x,這就是問題所在。 相反,您應該更改x並將其定義為成員變量(如IQV在此答案下方的評論中所述)。

public void actionPerformed(ActionEvent e) {
    final String x = "1"; // this should be x = "";
    textField1.setText(x);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM