简单的JFace ApplicationWindow例程

时间:2022-11-11 16:49:16
新公司新环境,现在这家公司有个系统的client是基于SWT/JFace的界面开发。第一眼看到一个Display的例子时,感觉真是麻烦,还要循环去等待消息传递,很简单的MessageBox都要while(true)。但是经过一两天的揣摩,对swt的运行机制有初步的了解后,感觉同awt虽然有一点区别,但共通性很强,所以还是很好上好的^_^。对于刚刚接触的我来说,纯手写swt界面,外观实在不怎么样-_-#。

贴一段simple example,自己留念一下 ^_^
简单的JFace ApplicationWindow例程package  TestJFace;
简单的JFace ApplicationWindow例程
简单的JFace ApplicationWindow例程
import  org.eclipse.jface.dialogs.MessageDialog;
简单的JFace ApplicationWindow例程
import  org.eclipse.jface.window.ApplicationWindow;
简单的JFace ApplicationWindow例程
import  org.eclipse.swt.SWT;
简单的JFace ApplicationWindow例程
import  org.eclipse.swt.events.SelectionAdapter;
简单的JFace ApplicationWindow例程
import  org.eclipse.swt.events.SelectionEvent;
简单的JFace ApplicationWindow例程
import  org.eclipse.swt.widgets.Button;
简单的JFace ApplicationWindow例程
import  org.eclipse.swt.widgets.Composite;
简单的JFace ApplicationWindow例程
import  org.eclipse.swt.widgets.Control;
简单的JFace ApplicationWindow例程
import  org.eclipse.swt.widgets.Display;
简单的JFace ApplicationWindow例程
import  org.eclipse.swt.widgets.Shell;
简单的JFace ApplicationWindow例程
简单的JFace ApplicationWindow例程
public   class  SimpleAppWindow  extends  ApplicationWindow  
简单的JFace ApplicationWindow例程简单的JFace ApplicationWindow例程
... {
简单的JFace ApplicationWindow例程    
public SimpleAppWindow()
简单的JFace ApplicationWindow例程简单的JFace ApplicationWindow例程    
...{
简单的JFace ApplicationWindow例程        
super(null);
简单的JFace ApplicationWindow例程    }

简单的JFace ApplicationWindow例程    
简单的JFace ApplicationWindow例程    
public void show()
简单的JFace ApplicationWindow例程简单的JFace ApplicationWindow例程    
...{
简单的JFace ApplicationWindow例程        setBlockOnOpen(
true);
简单的JFace ApplicationWindow例程        open();
简单的JFace ApplicationWindow例程    }

简单的JFace ApplicationWindow例程    
简单的JFace ApplicationWindow例程    
protected Control createContents(final Composite parent)
简单的JFace ApplicationWindow例程简单的JFace ApplicationWindow例程    
...{
简单的JFace ApplicationWindow例程        Button b 
= new Button(parent , SWT.PUSH);
简单的JFace ApplicationWindow例程        b.setText(
"button");
简单的JFace ApplicationWindow例程        b.addSelectionListener(
new SelectionAdapter()
简单的JFace ApplicationWindow例程简单的JFace ApplicationWindow例程        
...{
简单的JFace ApplicationWindow例程            
public void widgetSelected(SelectionEvent ex)
简单的JFace ApplicationWindow例程简单的JFace ApplicationWindow例程            
...{
简单的JFace ApplicationWindow例程                MessageDialog.openConfirm(parent.getShell(), 
"title""button 1 pressed");
简单的JFace ApplicationWindow例程            }

简单的JFace ApplicationWindow例程        }
);
简单的JFace ApplicationWindow例程        
return parent;
简单的JFace ApplicationWindow例程        
简单的JFace ApplicationWindow例程    }

简单的JFace ApplicationWindow例程    
简单的JFace ApplicationWindow例程    
protected void configureShell(Shell shell)
简单的JFace ApplicationWindow例程简单的JFace ApplicationWindow例程    
...{
           
super.configureShell(shell);
简单的JFace ApplicationWindow例程        shell.setText("练习");简单的JFace ApplicationWindow例程       
简单的JFace ApplicationWindow例程    }

简单的JFace ApplicationWindow例程}


简单的JFace ApplicationWindow例程package  TestJFace;
简单的JFace ApplicationWindow例程
简单的JFace ApplicationWindow例程
import  org.eclipse.swt.widgets.Display;
简单的JFace ApplicationWindow例程

简单的JFace ApplicationWindow例程
简单的JFace ApplicationWindow例程
public   class  AppMain 
简单的JFace ApplicationWindow例程简单的JFace ApplicationWindow例程
... {
简单的JFace ApplicationWindow例程    
public static void main(String[] args)
简单的JFace ApplicationWindow例程简单的JFace ApplicationWindow例程    
...{
简单的JFace ApplicationWindow例程        
new SimpleAppWindow().show();
简单的JFace ApplicationWindow例程        Display.getCurrent().dispose();
简单的JFace ApplicationWindow例程    }

简单的JFace ApplicationWindow例程}

上面的代码,extends ApplicationWindow 后,创建一个Button 再给button增加一个监听 ,就差不多实现一个最简单的例子。继续jface的ApplicationWindow后,最明显的一个改观就是。不用我再去
简单的JFace ApplicationWindow例程while ( ! shellTop.isDisposed())
简单的JFace ApplicationWindow例程简单的JFace ApplicationWindow例程
... {
简单的JFace ApplicationWindow例程    
if(!display.readAndDispatch())
简单的JFace ApplicationWindow例程        display.sleep();
简单的JFace ApplicationWindow例程}
这个过程,真的另人相当的不快。另外ApplicationWindow有现成的菜单栏、工具栏、状态栏。只需要做少量的代码,就可以实现。不想写了,下次继续。最后加一张用于applicationwindow的widget关系图。
简单的JFace ApplicationWindow例程