从 ActionListener 发送变量,以便另一个 ActionListener 可以使用它
Sending a variable from ActionListener, so another ActionListener can use it
我看到已经有人问过关于这个主题的一些问题,但我还没有找到答案。我正在编写一段代码,用户在 JTextField
中键入内容,然后单击按钮后,他的单词将替换为星号数,其字符数与他的单词所具有的字符数相同,例如 "table" 将替换为“****”。我是这样做的:
ask.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String guess = "";
String given = textGive.getText();
for (int i=0; i<given.length(); i++){
String asterisk = "*";
guess += asterisk;
textGive.setText(guess);
}
}
});
我知道我做得不是很好,但我不知道如何做得更好。有什么建议吗?
现在,我想以某种方式将两个字符串、原始单词和星号保存在范围之外,以便我可以在另一个 ActionListener 中访问它并进一步修改它。
在写第一个 ActionListener
之前,我确实写了 String guess = ""
和 String given = ""
但它似乎什么也没做。
因此,在我的第二个 ActionListener
中,我想向他发送用户键入他的词时收到的字符串 given
。
guess.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String attempt = textGuess.getText();
char att = attempt.charAt(0);
for (int i=0; i<5; i++){
if (given.charAt(i)==att){
textGuess.setText("Ok!");
}
}
}
});
Eclipse 给我错误提示:
"Cannot refer to the non-final local variable given defined in an enclosing scope".
我知道我需要将 given
设置为 final 以便进一步访问它,但是如果变量取决于第一个 ActionListener
的文本输入,该怎么做?这个问题还有其他解决方案吗?我最近开始使用 java,所以我不太了解这种语言。
您希望 class 可见的任何内容都应放在实例字段中,而不是局部变量中。例如,给定的变量应该是在 class 中声明的私有非静态字段,而不是隐藏在侦听器的 actionPerformed 方法中的变量。
例如,
public class Foo extends JPanel {
private JButton ask = new JButton("Ask");
private JTextField textGive = new JTextField(10);
private String given = ""; // visible throughout the class
public Foo() {
add(textGive);
add(ask);
ActionListener listener = e -> {
String guess = "";
// String given = textGive.getText(); //visible only within this method
given = textGive.getText();
guess = given.replaceAll("\w", "*");
textGive.setText(guess);
};
ask.addActionListener(listener);
textGive.addActionListener(listener); // also give the same listener to the JTextField
}
我看到已经有人问过关于这个主题的一些问题,但我还没有找到答案。我正在编写一段代码,用户在 JTextField
中键入内容,然后单击按钮后,他的单词将替换为星号数,其字符数与他的单词所具有的字符数相同,例如 "table" 将替换为“****”。我是这样做的:
ask.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String guess = "";
String given = textGive.getText();
for (int i=0; i<given.length(); i++){
String asterisk = "*";
guess += asterisk;
textGive.setText(guess);
}
}
});
我知道我做得不是很好,但我不知道如何做得更好。有什么建议吗?
现在,我想以某种方式将两个字符串、原始单词和星号保存在范围之外,以便我可以在另一个 ActionListener 中访问它并进一步修改它。
在写第一个 ActionListener
之前,我确实写了 String guess = ""
和 String given = ""
但它似乎什么也没做。
因此,在我的第二个 ActionListener
中,我想向他发送用户键入他的词时收到的字符串 given
。
guess.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String attempt = textGuess.getText();
char att = attempt.charAt(0);
for (int i=0; i<5; i++){
if (given.charAt(i)==att){
textGuess.setText("Ok!");
}
}
}
});
Eclipse 给我错误提示:
"Cannot refer to the non-final local variable given defined in an enclosing scope".
我知道我需要将 given
设置为 final 以便进一步访问它,但是如果变量取决于第一个 ActionListener
的文本输入,该怎么做?这个问题还有其他解决方案吗?我最近开始使用 java,所以我不太了解这种语言。
您希望 class 可见的任何内容都应放在实例字段中,而不是局部变量中。例如,给定的变量应该是在 class 中声明的私有非静态字段,而不是隐藏在侦听器的 actionPerformed 方法中的变量。
例如,
public class Foo extends JPanel {
private JButton ask = new JButton("Ask");
private JTextField textGive = new JTextField(10);
private String given = ""; // visible throughout the class
public Foo() {
add(textGive);
add(ask);
ActionListener listener = e -> {
String guess = "";
// String given = textGive.getText(); //visible only within this method
given = textGive.getText();
guess = given.replaceAll("\w", "*");
textGive.setText(guess);
};
ask.addActionListener(listener);
textGive.addActionListener(listener); // also give the same listener to the JTextField
}