以下代码中,按钮button单击事件使用匿名内部类,文本框中文字更改后,可以获得文本框中更改的文字。文本框双击事件使用了命名内部类,但无论文本框中值如何改变,双击文本框,只能得到初始值"hello world"。
请问该如何改动能使双击文本框得到文本框中更改后的文字(使用命名内部类)?****************************************************************
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.jface.dialogs.*;public class HelloWord {
/**
 * Launch the application
 * @param args
 */

public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setSize(500, 375);
shell.setText("SWT Application");
//界面代码开始
final Text text = new Text(shell, SWT.BORDER);
text.setText("hello world");
text.setBounds(186, 159, 120, 30);

text.addMouseListener(new MyMouseDoubleClick(text.getText())); final Button button = new Button(shell, SWT.NONE);
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
String str=text.getText();
MessageDialog.openInformation(null,"title", str);
}
});
button.setText("button");
button.setBounds(186, 231, 120, 30);
//界面代码结束
shell.layout();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}


private static final class MyMouseDoubleClick extends MouseAdapter {
private String str;
public MyMouseDoubleClick(String str1){
this.str=str1;
}
public void mouseDoubleClick(MouseEvent e) {
MessageDialog.openInformation(null,"title", str);
}
}}

解决方案 »

  1.   

    不会吧? 我也做过啊如果实在不行,就把title保存在其他的容器里。
      

  2.   

    使用匿名内部类就可以:
                      text.addMouseListener(new MouseAdapter() {
    public void mouseDoubleClick(final MouseEvent e) {
    String str=text.getText();
    MessageDialog.openInformation(null,"title", str);
    }
    });为什么呢?
      

  3.   

    class MyMouseDoubleClick  extends MouseAdapter { private String str;
    public MyMouseDoubleClick(String str1){
    this.str=str1;
    }
    public void mouseDoubleClick(MouseEvent e) {
    MessageDialog.openInformation(null,"title", str);
    }
      }
      

  4.   

    to kingdoom() :
    在text.addMouseListener(new MyMouseDoubleClick(text.getText()));此句上出现编译错误:No enclosing instance of type HelloWorld is accessible. Must qualify the allocation with an enclosing instance of type HelloWorld (e.g. x.new A() where x is an instance of HelloWorld).