gpt4 book ai didi

java - 更改 void 方法内的变量值以便在 Java 外部使用?

转载 作者:行者123 更新时间:2023-12-01 20:56:41 25 4
gpt4 key购买 nike

我是 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);
}

enter image description here

所以这里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 放在内部类中,但您正在设置一个值,所以在你的情况下这是不可能的。

附注:

请注意,您正在隐藏 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,它将是一个完全不同的实例(即使有相同的名称) )。因此您也需要删除该声明,因为您将隐藏我刚刚声明的实例变量。

What are Shadow Variables in Java

但简短的描述是:

A variable is shadowed if there is another variable with the same name that is closer in scope

展示这种阴影的一个小例子是

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"
}
}

关于java - 更改 void 方法内的变量值以便在 Java 外部使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42242207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com