Java 从匿名类检索数据
我想知道如何从以下代码中检索字符串textOperatorValue:Java 从匿名类检索数据,java,swing,class,Java,Swing,Class,我想知道如何从以下代码中检索字符串textOperatorValue: final JTextField textOperand = new JTextField(); textOperand.setBounds(200,100,75,25); //textOperand action Listener textOperand.addActionListener( new ActionListener () { public void actionPerformed(ActionEvent
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();
}
});
所以我可以接受它,然后将它解析成一个double,稍后在程序中使用。我试着将它设置为另一个字符串
字符串输入=”代码>但它说我必须将字符串初始化为最终字符串输入=”我学到的代码>就像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”技巧访问周围的范围,而不需要最终变量
或者,您需要从侦听器本身内部执行所有必要的工作(即,您不“检索”该值,只使用该值):
或者,您可以使用第二种技术调用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());
}
});
}
}
这取决于您想对文本字段中的值执行什么操作。您无法访问匿名类的成员。在这种情况下,它甚至是方法中的一个局部变量,这些变量永远无法访问
或者,您必须设置外部类成员的值,但要小心同步问题
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());
}
});
}
}
如果textOperationValue是全局变量(如果它在类中定义为全局变量),则它可以工作。
不在ActionListener内部,也不在您编写此代码的方法内部。我想知道如何从ActionListner获取字符串,并在代码的其他部分使用它(不包括在我的问题中)。您是否完成了编译器的要求并使用了最终变量?这确实是答案…@Byron是的,我试过了,它给我一个错误,说“这一行有多个标记-没有使用局部变量textOperatorValue的值-无法指定最终的局部变量textOperatorValue,因为它是在封闭类型“Oh”中定义的,是-希望现在已经修复。@DNA在您提供的第一段代码中,“Listen.this.(textOperator.getText());”是什么意思?我知道它使用的是文本类型的字符串/行,但我不知道Listen是什么意思。因此我想从JTextField中获取值,并将其用于公式中,以计算方程的答案,如果可能,将其显示在JLabel或JTextField中@DNAListen。这是引用父类实例(Listen)的一种方式。我们不能使用普通的这个
,因为它引用了内部的EventListener
实例。Swing教程中有很多关于使用JLabels和JTextfields的详细信息:
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());
}
});
}
}