当前位置: 首页 > 面试题库 >

SWT ExpandListener在Linux崩溃之前执行

爱乐邦
2023-03-14
问题内容

这是显示问题的简单可执行代码段。

使用ExpandBar时,期望的结果是在折叠或展开时调整窗口大小。它可以在Mac上正常运行,但不能在Linux上运行。

看起来在实际发生折叠/展开之前已调用 ExpandListener ,因此 pack()的 大小调整不正确。

异步执行仅仅是使它在Mac上运行的绷带,但这在Linux上不起作用。

import org.eclipse.swt.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.events.ExpandEvent;
import org.eclipse.swt.events.ExpandListener;

public class ExpandBarExample {
    public static void main(String[] args) {
        Shell shell = new Shell(SWT.DIALOG_TRIM | SWT.MIN
                | SWT.APPLICATION_MODAL);
        shell.setLayout(new FormLayout());
        shell.setText("Expand Bar");
        final ExpandBar bar = new ExpandBar(shell, SWT.NONE);
        FormData fd = new FormData();
        fd.top = new FormAttachment(0);
        fd.left = new FormAttachment(0);
        fd.right = new FormAttachment(100);
        fd.bottom = new FormAttachment(100);
        bar.setLayoutData(fd);

        bar.addExpandListener(new ExpandListener() {

            public void itemCollapsed(ExpandEvent arg0) {
                Display.getCurrent().asyncExec(new Runnable() {
                    public void run() {
                        bar.getShell().pack();
                    }
                });
            }

            public void itemExpanded(ExpandEvent arg0) {
                bar.getShell().pack();

                Display.getCurrent().asyncExec(new Runnable() {
                    public void run() {
                        bar.getShell().pack();
                    }
                });
            }

        });

        Composite composite = new Composite(bar, SWT.NONE);
        fd = new FormData();
        fd.left = new FormAttachment(0);
        fd.right = new FormAttachment(100);
        composite.setLayoutData(fd);

        FormLayout layout = new FormLayout();
        layout.marginLeft = layout.marginTop = layout.marginRight = layout.marginBottom = 8;

        composite.setLayout(layout);
        Label label = new Label(composite, SWT.NONE);
        label.setText("This is Bar 1");
        ExpandItem item1 = new ExpandItem(bar, SWT.NONE, 0);
        item1.setText("Bar 1");
        item1.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
        item1.setControl(composite);
        item1.setExpanded(true);

        composite = new Composite(bar, SWT.NONE);
        fd = new FormData();
        fd.left = new FormAttachment(0);
        fd.right = new FormAttachment(100);
        composite.setLayoutData(fd);

        layout = new FormLayout();
        layout.marginLeft = layout.marginTop = layout.marginRight = layout.marginBottom = 8;
        composite.setLayout(layout);
        label = new Label(composite, SWT.NONE);
        label.setText("This is Bar2");
        ExpandItem item2 = new ExpandItem(bar, SWT.NONE, 1);
        item2.setText("Bar 2");
        item2.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
        item2.setControl(composite);
        item2.setExpanded(true);

        composite = new Composite(bar, SWT.NONE);
        fd = new FormData();
        fd.left = new FormAttachment(0);
        fd.right = new FormAttachment(100);
        composite.setLayoutData(fd);

        layout = new FormLayout();
        layout.marginLeft = layout.marginTop = layout.marginRight = layout.marginBottom = 8;
        composite.setLayout(layout);
        label = new Label(composite, SWT.NONE);
        label.setText("This is Bar3");
        ExpandItem item3 = new ExpandItem(bar, SWT.NONE, 2);
        item3.setText("Bar 3");
        item3.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
        item3.setControl(composite);
        item3.setExpanded(true);

        bar.setSpacing(6);
        shell.pack();
        shell.open();
        Display display = shell.getDisplay();

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

}

问题答案:

我对这种解决方案不满意,但是它可以工作。

绝对是
XXX

解决方案

在评论中使用
XXX

标记虚假但有效的内容。使用FIXME标记虚假和损坏的内容。 -java.sun.com

无需再费周折

import org.eclipse.swt.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.events.ExpandEvent;
import org.eclipse.swt.events.ExpandListener;

public class ExpandBarExample {
    public static void main(String[] args) {
        Shell shell = new Shell(SWT.DIALOG_TRIM | SWT.MIN
                | SWT.APPLICATION_MODAL);
        shell.setLayout(new FormLayout());
        shell.setText("Expand Bar");
        final ExpandBar bar = new ExpandBar(shell, SWT.NONE);
        FormData fd = new FormData();
        fd.top = new FormAttachment(0);
        fd.left = new FormAttachment(0);
        fd.right = new FormAttachment(100);
        fd.bottom = new FormAttachment(100);
        bar.setLayoutData(fd);

        bar.addExpandListener(new ExpandListener() {

            private void resize(final ExpandEvent event, final boolean expand){

                final Display display = Display.getCurrent();

                new Thread(new Runnable() {
                    public void run() {

                        final int[] orgSize = new int[1];
                        final int[] currentSize = new int[1];

                        final Object lock = new Object();

                        if (display.isDisposed() || bar.isDisposed()){
                            return;
                        }

                        display.syncExec(new Runnable() {
                            public void run() {
                                if (bar.isDisposed() || bar.getShell().isDisposed()){
                                    return;
                                }

                                synchronized(lock){
                                    bar.getShell().pack(true);
                                    orgSize[0] = bar.getShell().getSize().y;
                                    currentSize[0] = orgSize[0];
                                }
                            }
                        });

                        while (currentSize[0] == orgSize[0]){
                            if (display.isDisposed() || bar.isDisposed()){
                                return;
                            }

                            display.syncExec(new Runnable() {
                                public void run() {

                                    synchronized(lock){
                                        if (bar.isDisposed() || bar.getShell().isDisposed()){
                                            return;
                                        }

                                        currentSize[0] = bar.getShell().getSize().y;

                                        if (currentSize[0] != orgSize[0]){
                                            return;
                                        }
                                        else{
                                            bar.getShell().layout(true);
                                            bar.getShell().pack(true);
                                        }
                                    }
                                }
                            });                             
                        }
                    }
                }).start();
        }

        public void itemCollapsed(ExpandEvent event) {
            resize(event, false);
        }

        public void itemExpanded(ExpandEvent event) {        
            resize(event, true);
        }

        });

        Composite composite = new Composite(bar, SWT.NONE);
        fd = new FormData();
        fd.left = new FormAttachment(0);
        fd.right = new FormAttachment(100);
        composite.setLayoutData(fd);

        FormLayout layout = new FormLayout();
        layout.marginLeft = layout.marginTop = layout.marginRight = layout.marginBottom = 8;

        composite.setLayout(layout);
        Label label = new Label(composite, SWT.NONE);
        label.setText("This is Bar 1");
        ExpandItem item1 = new ExpandItem(bar, SWT.NONE, 0);
        item1.setText("Bar 1");
        item1.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
        item1.setControl(composite);
        item1.setExpanded(true);

        composite = new Composite(bar, SWT.NONE);
        fd = new FormData();
        fd.left = new FormAttachment(0);
        fd.right = new FormAttachment(100);
        composite.setLayoutData(fd);

        layout = new FormLayout();
        layout.marginLeft = layout.marginTop = layout.marginRight = layout.marginBottom = 8;
        composite.setLayout(layout);
        label = new Label(composite, SWT.NONE);
        label.setText("This is Bar2");
        ExpandItem item2 = new ExpandItem(bar, SWT.NONE, 1);
        item2.setText("Bar 2");
        item2.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
        item2.setControl(composite);
        item2.setExpanded(true);

        composite = new Composite(bar, SWT.NONE);
        fd = new FormData();
        fd.left = new FormAttachment(0);
        fd.right = new FormAttachment(100);
        composite.setLayoutData(fd);

        layout = new FormLayout();
        layout.marginLeft = layout.marginTop = layout.marginRight = layout.marginBottom = 8;
        composite.setLayout(layout);
        label = new Label(composite, SWT.NONE);
        label.setText("This is Bar3");
        ExpandItem item3 = new ExpandItem(bar, SWT.NONE, 2);
        item3.setText("Bar 3");
        item3.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
        item3.setControl(composite);
        item3.setExpanded(true);

        bar.setSpacing(6);
        shell.pack();
        shell.open();
        Display display = shell.getDisplay();

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

}

如果有人想知道它在做什么并且不想看代码。

基本上, ExpandListener会 查看外壳的原始高度,并发出syncExec来 打包()
外壳,直到外壳实际更改大小为止。一个非常繁忙的等待方法。



 类似资料:
  • 每次我打开Aptana它都会崩溃。 Java运行时环境检测到一个致命错误: 如果您想提交错误报告,请访问:http://bugreport.sun.com/bugreport/crash.jsp崩溃发生在Java虚拟机之外的本机代码中。有关报告错误的位置,请参见问题框。

  • 我更新了我的SDK,播放服务等等,最近。当我在带有Lollipop前Android的设备/模拟器上运行我的应用程序时,它会立即出现以下错误: 我连火力点都不用!以下是我的分级:

  • 我试图接收通过Mediastore拍摄的图像的URI。ACTION_IMAGE_CAPTURE意图,但由于某种原因,默认相机应用程序(谷歌)甚至在返回我的活动之前和我的代码到达onActivityResult()之前就崩溃了。我怀疑这与我制作意图的方式有关,但我不确定。这是我的代码: 如果有人要求,我也可以添加我的onActivityResult代码,不过我不确定它在这里是否相关。 以下是logc

  • 我正在尝试使用firefox headless在headless redhat linux构建机器上运行selenium测试。我创建驱动程序的方法如下所示: 我在日志文件中看到的错误如下所示: 我已经检查了机器上是否安装了gtk、glib、pango、xorg和libstdc的正确版本。以前有人在使用无头制造机时遇到过这个问题吗? Firefox版本:|Selenium版本:|GeckDrive版

  • 我在一个片段中遇到了一个问题,该片段从新的Google Maps API中嵌入了SuportMapFragment。当创建我的片段时,它从方法开始,在中获取一些数据。当这种情况发生时,MapFragment保持在屏幕外,而是显示进度条。 完成后,我向MapView的事件注册。然后我显示片段,这导致地图视图被显示和布局。被触发,动画开始。 多谢!

  • 问题内容: 我的JVM一直在libzip.so上持续崩溃,并且出乎意料。我已经将该错误提交给了Oracle,但决定看看这里是否有人遇到过该问题,如果是,您如何处理它?这是一个正在运行的网络应用 Linux 2.6.34-gentoo-r6#1 SMP Fri Sep 24 00:15:06 EDT 2010 i686 Intel(R)Xeon(R)CPU X5460 @ 3.16GHz 真正的带j