[英]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.