之前学习了SWT/JFace。现在准备研究一下XWT.
首先给出一些简介
What is XWT?
XWT stands for eclipse XML Window Toolkit. It is a powerful and lightweight declarative UI framework designed for Eclipse, based on XML as markup language.
XWT simplifies UI development by a physical separation of UI definition in XML from the run-time logic in programing language. It is very intuitive for creating interfaces directly or via tools such as Visual editor. especially for people with a background in web design and technologies.
参考网址:
http://wiki.eclipse.org/E4/XWT
如何进行一个XWT开发呢?
首先要准备开发环境。
所需要的eclipse3.6和eclipse-e4-repo-incubation-0.10
采用link方式把eclipse-e4..的bundle加载到eclipse里面
现在就开始开发一个简单的XWT程序
启动Eclipse,点击new Project
如图:
点击Next如图
继续点击Next
点击Finish
然后在选择“Dependencies”选项卡,点击“Add”添加依赖
加入这些依赖
org.eclipse.core.runtime,
org.eclipse.swt;bundle-version="3.6.0",
org.eclipse.jface;bundle-version="3.6.0",
org.eclipse.e4.xwt;bundle-version="0.9.1",
org.eclipse.core.databinding;bundle-version="1.3.100",
org.eclipse.jface.databinding;bundle-version="1.4.0",
org.eclipse.core.databinding.property;bundle-version="1.3.0",
com.ibm.icu;bundle-version="4.2.1",
org.pushingpixels.trident;bundle-version="1.2.0",
org.eclipse.e4.xwt.forms;bundle-version="0.9.1",
org.eclipse.ui.forms;bundle-version="3.5.0"
如图所示
我们在package上面点击右键选择new再选择other
如图
最后点击Finish。
就会自动生成两个文件,分别是HelloWorld.java,HelloWorld.xwt
在HelloWorld.java里面的代码如有修改一下,这样才比较合适
之前的是
/** * HelloWorld.java * 版权所有(C) 2010 [email protected] * 创建:崔冉 2010-11-19 下午03:42:13 */ package com.cayden.helloworld; import org.eclipse.swt.widgets.Composite; /** * @author 崔冉 * @version 1.0.0 * @desc */ public class HelloWorld extends Composite { /** * @param parent * @param style */ public HelloWorld(Composite parent, int style) { super(parent, style); } }
修改后的是
/** * HelloWorld.java * 版权所有(C) 2010 [email protected] * 创建:崔冉 2010-11-19 下午03:42:13 */ package com.cayden.helloworld; import java.net.URL; import java.util.HashMap; import java.util.Map; import org.eclipse.e4.xwt.IConstants; import org.eclipse.e4.xwt.IXWTLoader; import org.eclipse.e4.xwt.XWT; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Composite; /** * @author 崔冉 * @version 1.0.0 * @desc */ public class HelloWorld extends Composite { /** * @param parent * @param style */ public HelloWorld(Composite parent, int style) { super(parent, style); setLayout(new FillLayout()); // load XWT String name = HelloWorld.class.getSimpleName() + IConstants.XWT_EXTENSION_SUFFIX; try { URL url = HelloWorld.class.getResource(name); Map<String, Object> options = new HashMap<String, Object>(); options.put(IXWTLoader.CLASS_PROPERTY, this); options.put(IXWTLoader.CONTAINER_PROPERTY, this); XWT.loadWithOptions(url, options); } catch (Throwable e) { throw new Error("Unable to load " + name, e); } } }
在HelloWorld.xwt文件中加入一句话
<Label text="HelloWorld"/>
<Composite xmlns="http://www.eclipse.org/xwt/presentation" xmlns:x="http://www.eclipse.org/xwt" xmlns:c="clr-namespace:com.cayden.helloworld" xmlns:j="clr-namespace:java.lang" x:Class="com.cayden.helloworld.HelloWorld"> <Composite.layout> <GridLayout numColumns="4" /> </Composite.layout> <Label text="HelloWorld"/> </Composite>
最后写一个Test.java的测试类代码如下
/** * Test.java * 版权所有(C) 2010 [email protected] * 创建:崔冉 2010-11-19 下午03:47:30 */ package com.cayden.helloworld; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; /** * @author 崔冉 * @version 1.0.0 * @desc */ public class Test { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Display display = new Display(); Shell shell = new Shell(display); HelloWorld helloWorld = new HelloWorld(shell, SWT.NONE); helloWorld.setBounds(0, 0, 200, 100); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } }
运行结果如图
更多信息会在以后写出来。