簡體   English   中英

SWTException:Widget已被釋放

[英]SWTException: Widget is disposed

當我第二次創建打開的新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() {}
}

AboutController.java (代表新窗口):

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

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

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

我的觀點是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"));
    }
}

當我嘗試創建新的應用程序窗口時,使用new AboutController()然后出現Widget is disposed錯誤。

問題是您無法訪問已經放置的小部件。 在您的代碼中, AboutController.view是靜態的,因此在初始化類AboutController時只創建一次。 關閉Shell ,它會自動處理,因此所有子窗口小部件也會被處理掉 - 包括您的視圖對象。

當您再次打開窗口時,已經處理的視圖將被移交給超級構造函數而不是新創建的視圖。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM