[英]Retrieving data from anonymous class
我想知道如何从此代码检索字符串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();
}
});
因此,我可以接受它,然后将其解析为一个双精度型,以供以后在程序中使用。 我尝试将其设置为等于另一个字符串String Input = " ";
但是它说我必须将字符串初始化为final String Input = " ";
我学到的就像是C ++中的常量。
您在ActionListener中声明的任何变量在其余代码中都不可见。 您需要设置一个范围更广的变量(从侦听器内部):
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();
}
});
}
}
在这里,我们使用“ OuterClass.this”技巧来访问周围的范围,而无需最终变量。
或者您需要从侦听器内部执行所有必要的工作(即,您不“获取”该值,而只是使用该值):
public void doSomethingUseful(String usefulValue) { /* add code here */ }
textOperand.addActionListener( new ActionListener ()
{
public void actionPerformed(ActionEvent e)
{
doSomethingUseful(textOperand.getText());
}
});
或者,您可以使用第二种技术来调用更改变量值的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());
}
});
}
}
这取决于您要对TextField中的值执行的操作。
您无法访问匿名类的成员。 在这种情况下,它甚至是方法中的局部变量,这些变量永远不可访问。
要么,要么必须设置外部类成员的值,但要注意同步问题。
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
}
}
或者,您必须在其他可以存储值的地方调用方法。
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);
}
}
或者,您创建一个(命名的)类,它是一个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
}
}
或者,您只需要在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());
}
});
}
}
如果textOperandValue是全局变量(如果在全局类中定义),则可以使用。 不在ActionListener内部,不在编写此代码的方法内部。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.