graphic context是在GCcM的,GC对象是附着于现存的Controls?br /> 要创Z个graphically oriented的应用程序,首先要创建graphic contextQƈ其与一个component相关联,q两步都可通过GC的constructor来实现。共?个构造函敎ͼ见下Q?br />1. GC(Drawable)--Creates a GC and configures it for the Drawable object 2. GC(Drawable, int)--Creates and configures a GC and sets the text-display styleQ第二个参数可以是RIGHT_TO_LEFT或LEFT_TO_RIGHTQ默认|; W一个参数需要实现Drawable接口的对象, 此接口包含了与graphic context.内部相联pȝҎ。SWT提供了三个实现Drawable接口的类QImage, Device, ?Control.
protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache) { Point maxDimensions = calculateMaxDimensions(composite.getChildren()); int stepsPerHemisphere = stepsPerHemisphere(composite.getChildren().length); int maxWidth = maxDimensions.x; int maxHeight = maxDimensions.y; int dimensionMultiplier = (stepsPerHemisphere + 1); int controlWidth = maxWidth * dimensionMultiplier; int controlHeight = maxHeight * dimensionMultiplier; int diameter = Math.max(controlWidth, controlHeight); Point preferredSize = new Point(diameter, diameter); ... // code to handle case when our calculations // are too large return preferredSize; }
参数Q?br />1.composite--The object we’re going to populate. At the time this method is called, it has children, but neither the composite nor the children have been sized or positioned on the screen.
2.wHint and hHint--layout所需的最大长宽。若带有参数SWT.DEFAULT,表示此layout可以随意使用use whatever sizes it decides it needs.
3.flushCache--作ؓflagQto tell the layout whether it’s safe to use any cached values that it may be maintaining.
]]>6.4 The form layouthttp://www.aygfsteel.com/joeyjong/archive/2006/04/12/40607.htmlJOOJOOWed, 12 Apr 2006 04:22:00 GMThttp://www.aygfsteel.com/joeyjong/archive/2006/04/12/40607.htmlhttp://www.aygfsteel.com/joeyjong/comments/40607.htmlhttp://www.aygfsteel.com/joeyjong/archive/2006/04/12/40607.html#Feedback0http://www.aygfsteel.com/joeyjong/comments/commentRss/40607.htmlhttp://www.aygfsteel.com/joeyjong/services/trackbacks/40607.html
与之前所q的layout不同Qform layout不是Z行和列的Q它是基于与其他control之间的相对位|的?br /> FormLayout十分单,你只要:1.讑֮边距(高,宽)属性?2.讑֮spacing属性,x有control间的距离(in pixels)
同样可以使用FormData来配|单个的control?br /> FormData 如果一个control没有一个FormData实例来描q它的话Q就会默认放在composite的右上角 width和height属性指定了control的尺寸,in pixels. top, bottom, right, 和left属性,每一个都有一个FormAttachment实例Q这些attachments描述了control与其他control之间的关pR?br /> FormAttachment ?个用途径Q?br />1.通过使用percentage of the parent composite.
2.通过讑֮一个control和另一个control之间的相对位|?br />《图?br /> package com.swtjface.Ch6; import org.eclipse.swt.*; import org.eclipse.swt.widgets.*; import org.eclipse.swt.layout.*; public class Ch6FormLayoutComposite extends Composite { public Ch6FormLayoutComposite(Composite parent) { super(parent, SWT.NONE); FormLayout layout = new FormLayout(); setLayout(layout); Text t = new Text(this, SWT.MULTI); FormData data = new FormData(); data.top = new FormAttachment(0, 0); data.left = new FormAttachment(0, 0); data.right = new FormAttachment(100); data.bottom = new FormAttachment(75);//定text的位|,因ؓ左上角是坐标原点Q所以right的百分数?00?br />t.setLayoutData(data); Button ok = new Button(this, SWT.NONE); ok.setText("Ok"); Button cancel = new Button(this, SWT.NONE); cancel.setText("Cancel"); data = new FormData(); data.top = new FormAttachment(t); data.right = new FormAttachment(cancel);//ok按钮在text下面Qcancel左边 ok.setLayoutData(data); data = new FormData(); data.top = new FormAttachment(t); data.right = new FormAttachment(100);//cancel按钮在text下面Q在最双 cancel.setLayoutData(data); } }
]]>6.3 The grid layouthttp://www.aygfsteel.com/joeyjong/archive/2006/04/11/40484.htmlJOOJOOTue, 11 Apr 2006 08:16:00 GMThttp://www.aygfsteel.com/joeyjong/archive/2006/04/11/40484.htmlhttp://www.aygfsteel.com/joeyjong/comments/40484.htmlhttp://www.aygfsteel.com/joeyjong/archive/2006/04/11/40484.html#Feedback0http://www.aygfsteel.com/joeyjong/comments/commentRss/40484.htmlhttp://www.aygfsteel.com/joeyjong/services/trackbacks/40484.html
最常用的一Ulayout.以row layout为基?br /> package com.swtjface.Ch6; import org.eclipse.swt.*; import org.eclipse.swt.widgets.*; import org.eclipse.swt.layout.*; public class Ch6GridLayoutComposite extends Composite { public Ch6GridLayoutComposite(Composite parent) { super(parent, SWT.NONE); GridLayout layout = new GridLayout(4,false);//每一行有4个controlQ后一个参数是a boolean to indicate whether the columns should take up an even amount of space. By passing false, you tell the layout to only use the minimum amount of space needed for each column. setLayout(layout); for (int i = 0; i < 16; ++i) { Button button = new Button(this, SWT.NONE); button.setText("Cell " + i); } } }
Using GridData styles 十分cM于RowData对象。可通过其构造函数来讑֮STYLEQ这些STYLE可分?c:FILL, HORIZONTAL_ALIGN, and VERTICAL_ALIGN. 1.FILL:此cell是否fill所有的availabe的空间。可用的D包括FILL_HORIZONTALQ水qx张),FILL_VERTICALQ垂直扩张),FILL_BOTH?br />2.ALIGNQ用来指定control在cell中的什么位|。值包括BEGINNING, END, CENTER和FILL?br />具体参见下表 Using GridData size attributes 与RowData不同QGridDataq有很多的public属性。其中有些是布尔值类型的Q一般会Ҏ所讄的不同styles而自动管理,所以无需对其直接操作。还有一些是integer|用来定单个cells的大。具体g下表Q?br />
]]>5.8 ProgressIndicatorhttp://www.aygfsteel.com/joeyjong/archive/2006/04/10/40291.htmlJOOJOOMon, 10 Apr 2006 10:07:00 GMThttp://www.aygfsteel.com/joeyjong/archive/2006/04/10/40291.htmlhttp://www.aygfsteel.com/joeyjong/comments/40291.htmlhttp://www.aygfsteel.com/joeyjong/archive/2006/04/10/40291.html#Feedback0http://www.aygfsteel.com/joeyjong/comments/commentRss/40291.htmlhttp://www.aygfsteel.com/joeyjong/services/trackbacks/40291.html同ProgressIndicator一P它支持工作的虚拟单位Qyou need only initialize the ProgressIndicator with the total amount of work you expect to do and notify it as work is completed:
ProgressIndicator indicator = new ProgressIndicator(parent); ... indicator.beginTask(10); ... Display.getCurrent()display.asyncExec(new Runnable() { public void run() { //Inform the indicator that some amount of work has been done indicator.worked(1); } });
正如上例所C,使用ProgressIndicator需?步: 1.让indicator知道d有多工作,通过使用beginTask().只有q个Ҏ被调用了之后Q这个control才会在屏q上昄?br />2.每当有一部分工作被完成了Q就调用worked()。ؓ了防止非ui的线E来update widgetsQ所以用asyncExec()来解册个问题?br /> ProgressIndicator也提供animated模式Q即d作量不知道的情况。在q种模式下,the bar continually fills and empties until done() is called. 要用这个模式,p用beginAnimatedTask()代替beginTask();q且不需要worked()Ҏ?/font>
]]>5.7 ProgressBarhttp://www.aygfsteel.com/joeyjong/archive/2006/04/10/40287.htmlJOOJOOMon, 10 Apr 2006 09:56:00 GMThttp://www.aygfsteel.com/joeyjong/archive/2006/04/10/40287.htmlhttp://www.aygfsteel.com/joeyjong/comments/40287.htmlhttp://www.aygfsteel.com/joeyjong/archive/2006/04/10/40287.html#Feedback0http://www.aygfsteel.com/joeyjong/comments/commentRss/40287.htmlhttp://www.aygfsteel.com/joeyjong/services/trackbacks/40287.htmlProgressBarQ进度条Q是ProgressIndicator的简化版本。大多数情况下推荐用ProgressIndicator。如果你军_直接使用ProgressBarQ需要手动改变此bar的外观。如?br /> //Style can be SMOOTH, HORIZONTAL, or VERTICAL ProgressBar bar = new ProgressBar(parent, SWT.SMOOTH); bar.setBounds(10, 10, 200, 32); bar.setMaximum(100); ... for(int i = 0; i < 10; i++) { //Take care to only update the display from its //own thread Display.getCurrent().asyncExec(new Runnable() { public void run() { //Update how much of the bar should be filled in bar.setSelection((int)(bar.getMaximum() * (i+1) / 10)); } }); }
setSelection()causes the widget to be updated every time.This behavior is unlike that of ProgressIndicator or ProgressMonitorDialog,which will update the display only if it has changed by an amount that will be visible to the end user.