栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

SWT ExpandListener在Linux崩溃之前执行

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SWT ExpandListener在Linux崩溃之前执行

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

绝对是
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来 打包()
外壳,直到外壳实际更改大小为止。一个非常繁忙的等待方法。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/454954.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号