Java SWTException: 小部件已释放

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20125142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:20:08  来源:igfitidea点击:

SWTException: Widget is disposed

javaswt

提问by ovnia

When I create open new SWT app window a second time, app crashes with SWTException: Widget is disposederror. What's wrong?

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

Here is my code:

这是我的代码:

ABSTRACT Controller.java:

摘要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() {}
}

AboutController.java(represends new window):

AboutController.java(代表新窗口):

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

    public AboutController() {
        super(view);
        super.render();
    }
}

ABSTRACT View.java:

摘要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();
            }
        }
    }
}

And my view AboutView.java

而我的观点 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"));
    }
}

When I try to create new app window, with new AboutController()then Widget is disposederror occurs.

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

采纳答案by isnot2bad

The problem is that you cannot access an already disposed widget. In your code, AboutController.viewis static, so it is created only once when the class AboutControlleris initialized. When the Shellis closed, it is automatically disposed and so all child-widgets get disposed too - including your view object.

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

When you then open the window a second time, the already disposed view is handed over to the super-constructor instead of a newly created view.

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