SWTException:Widget已被释放

ovn*_*nia 3 java swt

当我第二次创建打开的新SWT应用程序窗口时,应用程序崩溃并SWTException: Widget is disposed出现错误.怎么了?

这是我的代码:

摘要Controller.java:

public abstract class Controller {
    protected View view;

    public Controller(View v) {
        view = v;
    }

    protected void render() {
        data();
        view.setData(data);
        view.render();
        listeners();
        if (display)
            view.open();
    }
    protected void data() {}

    protected void listeners() {}
}
Run Code Online (Sandbox Code Playgroud)

AboutController.java (代表新窗口):

public class AboutController extends Controller {
    static AboutView view = new AboutView();

    public AboutController() {
        super(view);
        super.render();
    }
}
Run Code Online (Sandbox Code Playgroud)

摘要View.java:

public abstract class View {
    protected Display display;
    protected Shell shell;
    protected int shellStyle = SWT.CLOSE | SWT.TITLE | SWT.MIN;

    private void init() {
        display = Display.getDefault();
        shell = new Shell(shellStyle);
    };

    protected abstract void createContents();

    public View() {
        init();
    }

    public void render() {
        createContents();
    }

    public void open() {
        shell.open();
        shell.layout();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

而我的观点 AboutView.java

public class AboutView extends View implements ApplicationConstants {

    protected void createContents() {
        shell.setSize(343, 131);
        shell.setText("About");

        Label authorImage = new Label(shell, SWT.NONE);
        authorImage.setBounds(10, 10, 84, 84);
        authorImage.setImage(SWTResourceManager.getImage(AboutView.class,
                "/resources/author.jpg"));
    }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试创建新的应用程序窗口中,new AboutController()然后Widget is disposed会出现误差.

isn*_*bad 10

问题是您无法访问已经放置的小部件.在您的代码中,它AboutController.view是静态的,因此在AboutController初始化类时只创建一次.当Shell关闭时,它会自动配置等所有子小部件得到安置太-包括您的视图对象.

当您再次打开窗口时,已经处理的视图将被移交给超级构造函数而不是新创建的视图.