[英]Retrieving data from anonymous class
I was wondering how to retrieve the string textOperandValue,from this code : 我想知道如何从此代码检索字符串textOperandValue:
final JTextField textOperand = new JTextField();
textOperand.setBounds(200,100,75,25);
//textOperand action Listener
textOperand.addActionListener( new ActionListener () {
public void actionPerformed(ActionEvent e) {
String textOperandValue = textOperand.getText();
}
});
So I can take it, and then parse it into a double to be used later in the program. 因此,我可以接受它,然后将其解析为一个双精度型,以供以后在程序中使用。 I tried setting it equal to a another string
String Input = " ";
我尝试将其设置为等于另一个字符串
String Input = " ";
but it said I had to initialize the string to a final String Input = " ";
但是它说我必须将字符串初始化为
final String Input = " ";
which I learned is something like a constant in C++. 我学到的就像是C ++中的常量。
Any variables you declare within an ActionListener won't be visible to the rest of your code. 您在ActionListener中声明的任何变量在其余代码中都不可见。 Either you need to set a variable (from within the listener) that has wider scope:
您需要设置一个范围更广的变量(从侦听器内部):
public class Listen
{
String usefulResult = null;
public Listen()
{
final JTextField textOperand = new JTextField();
textOperand.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Listen.this.usefulResult = textOperand.getText();
}
});
}
}
Here we use the "OuterClass.this" trick to access the surrounding scope without needing a final variable. 在这里,我们使用“ OuterClass.this”技巧来访问周围的范围,而无需最终变量。
or you need to perform all the necessary work from within the listener itself (ie you don't "retrieve" the value, you just use the value): 或者您需要从侦听器内部执行所有必要的工作(即,您不“获取”该值,而只是使用该值):
public void doSomethingUseful(String usefulValue) { /* add code here */ }
textOperand.addActionListener( new ActionListener ()
{
public void actionPerformed(ActionEvent e)
{
doSomethingUseful(textOperand.getText());
}
});
Or you could use this second technique to call a setter method that changes the value of a variable, avoiding the problems of accessing final variables within event listeners: 或者,您可以使用第二种技术来调用更改变量值的setter方法,从而避免在事件侦听器中访问最终变量的问题:
public class Listen
{
String usefulResult = null;
public void setUseful(String usefulValue){
usefulResult = usefulValue;
}
public Listen()
{
final JTextField textOperand = new JTextField();
textOperand.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
setUseful(textOperand.getText());
}
});
}
}
It depends what you want to do with the value from the TextField. 这取决于您要对TextField中的值执行的操作。
You can't access the members of anonymous classes. 您无法访问匿名类的成员。 In this case it's even a local variable in a method, those are never accessible.
在这种情况下,它甚至是方法中的局部变量,这些变量永远不可访问。
Either, you'll have to set the value of a member of the outer class, but beware of synchronization trouble. 要么,要么必须设置外部类成员的值,但要注意同步问题。
class MyClass {
final JTextField textOperand = new JTextField();
public String textOperandValue;
public MyClass() {
textOperand.setBounds(200,100,75,25);
//textOperand action Listener
textOperand.addActionListener( new ActionListener () {
public void actionPerformed(ActionEvent e) {
textOperandValue = textOperand.getText();
}
});
}
public someOtherMethod() {
// textOperandValue available here
if (textOperandValue != null)
//is set
}
}
Or, you'll have to call a method somewhere else that can store the value. 或者,您必须在其他可以存储值的地方调用方法。
class MyClass {
final JTextField textOperand = new JTextField();
public MyClass() {
textOperand.setBounds(200,100,75,25);
//textOperand action Listener
textOperand.addActionListener( new ActionListener () {
public void actionPerformed(ActionEvent e) {
someOtherMethod(textOperand.getText());
}
});
}
public someOtherMethod(String value) {
System.out.println(value);
}
}
Or, you create a (named) class that is an ActionListener and that can store the value in a retrievable form. 或者,您创建一个(命名的)类,它是一个ActionListener,并且可以以可检索的形式存储值。
class MyClass {
final JTextField textOperand = new JTextField();
public String textOperandValue;
private class MyActionListener implements ActionListener {
String value;
public void actionPerformed(ActionEvent e) {
value =textOperand.getText();
}
}
MyActionListener l = new MyActionListener();
public MyClass() {
textOperand.setBounds(200,100,75,25);
//textOperand action Listener
textOperand.addActionListener(l);
}
public someOtherMethod() {
if (l.value != null)
//is set
}
}
Or, you just do what you need to do in the action method: 或者,您只需要在action方法中做您需要做的事情:
class MyClass {
final JTextField textOperand = new JTextField();
public MyClass() {
textOperand.setBounds(200,100,75,25);
//textOperand action Listener
textOperand.addActionListener( new ActionListener () {
public void actionPerformed(ActionEvent e) {
System.out.println(textOperand.getText());
}
});
}
}
it can work if textOperandValue is global(if it defined in class globally) variable. 如果textOperandValue是全局变量(如果在全局类中定义),则可以使用。 not inside ActionListener, not inside method where you wrote this code.
不在ActionListener内部,不在编写此代码的方法内部。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.