簡體   English   中英

小部件已棄置

[英]Widget is disposed

大家好

我有一個主屏幕,具有以下鍵綁定:

shell.getDisplay().addFilter(SWT.KeyUp, new Listener() {
    @Override
    public void handleEvent(Event event) {
        switch (event.keyCode) {
        case SWT.ESC:
            shell.close();
            break;
        case SWT.F12:
            display.syncExec(new Runnable() {
                @Override
                public void run() {
                new Venda(shell);
                }
            });                 
            break;                  
        default:
            break;
        }               
    }
});

按F12鍵時,將打開搜索屏幕。 銷售屏幕的構造:

public Venda(Shell parent) {
        super(parent);
        this.shell = new Shell(getParent(),SWT.APPLICATION_MODAL);
        this.display = getParent().getDisplay();
        this.shell.setMaximized(true);
        this.shell.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_DARK_GREEN));
        this.fontText = new Font(shell.getDisplay(), new FontData("Arial", 28, SWT.NORMAL));
        this.fontLabel = new Font(shell.getDisplay(), new FontData("Arial", 13, SWT.NORMAL));
        this.shell.setText("Cupom Fiscal - Venda Produto");
        this.criaCampos();
        this.configuraTeclaAtalho();
        this.shell.open();
        while (!display.isDisposed()) {
            if (!display.isDisposed() && !display.readAndDispatch()) {
                display.sleep();
            }
        }
    }

在銷售屏幕上按F3鍵時,將打開搜索屏幕。 我的問題是:第一次打開銷售屏幕時,搜索屏幕可以正常工作,但是如果關閉銷售屏幕並再次打開,則搜索屏幕無法正常工作,從而導致錯誤:放置了小部件。 錯誤發生在源代碼中的第02行。 變量“ abreFechaCaixa”驗證是否應打開銷售屏幕。

    if(!abreFechaCaixa){
        MessageBox msg = new MessageBox(shell, SWT.ICON_WARNING | SWT.YES | SWT.NO);
        msg.setMessage("Caixa Fechado, deseja abrir?");
    msg.setText(shell.getText());
    if(msg.open() == SWT.YES){
        abreCaixa();
    }
    }
if(abreFechaCaixa){
    display.syncExec(new Runnable() {
        @Override
    public void run() {
                new Consulta(shell,"Cupom Fiscal - Consulta Produto");
    }
    });
}

構造函數的搜索屏幕:

public Consulta(Shell parent) {
                super(parent);
        this.shell = new Shell(parent, SWT.APPLICATION_MODAL);
        this.display = getParent().getDisplay();
        this.shell.setText(tituloTela);
        this.shell.setLayout(new GridLayout(1, false));
        this.fontText = new Font(shell.getDisplay(), new FontData("Arial", 28, SWT.NORMAL));
        this.fontLabel = new Font(shell.getDisplay(), new FontData("Arial", 13, SWT.NORMAL));
        this.criaCampos();
        this.shell.pack();
        this.centralizaTela();
        this.shell.open();
        while (!shell.isDisposed()) {
            if (!display.isDisposed() && !display.readAndDispatch()) {
                display.sleep();
            }
        }
    }

您能幫我解決這個問題嗎? 還是顯示關閉SWT中的窗口的最佳方法? 謝謝!

為了解決我的問題,我將進程分成線程。

我認為您需要添加“ display.dispose();” 在while循環!shell.isDisposed()之后。 如下所示:

 while (!shell.isDisposed()) {
    if (!display.readAndDispatch()){
       display.sleep();
    }
  }
 display.dispose ();

您不需要在Consulta類中使用while循環,因為ShellVenda的子級。 這意味着子shell的顯示對象與其父對象相同。 因此,在您的構造中,在此顯示上運行readAndDispatch()會被處理兩次。

暫無
暫無

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

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