繁体   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