ï»??xml version="1.0" encoding="utf-8" standalone="yes"?>久久91精品国产91久久久,国产日韩一区欧美,亚洲 国产 欧美一区http://www.aygfsteel.com/joeyjong/category/8174.htmlJoey的读书笔è®?/description>zh-cnWed, 28 Feb 2007 08:11:13 GMTWed, 28 Feb 2007 08:11:13 GMT607.1 graphic contexthttp://www.aygfsteel.com/joeyjong/archive/2006/04/14/41042.htmlž®ç¬¼é¦’头ž®ç¬¼é¦’头Fri, 14 Apr 2006 03:26:00 GMThttp://www.aygfsteel.com/joeyjong/archive/2006/04/14/41042.htmlhttp://www.aygfsteel.com/joeyjong/comments/41042.htmlhttp://www.aygfsteel.com/joeyjong/archive/2006/04/14/41042.html#Feedback0http://www.aygfsteel.com/joeyjong/comments/commentRss/41042.htmlhttp://www.aygfsteel.com/joeyjong/services/trackbacks/41042.htmlgraphic contextž®Þp±¡Control最™å¶å±‚的画板,它可以ä‹É你向GUI components加入客制化的囑ÖÅžåQŒå›¾ç‰‡ï¼Œå?qi¨¢ng)不同字体的文本。同样也提供事äšg处理

graphic context是在GC¾cÖM¸­çš„,GC对象是附着于现存的Controlsã€?br />
要创å»ÞZ¸€ä¸ªgraphically oriented的应用程序,首先要创建graphic contextåQŒåƈž®†å…¶ä¸Žä¸€ä¸ª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 styleåQŒç¬¬äºŒä¸ªå‚数可以是RIGHT_TO_LEFT或LEFT_TO_RIGHTåQˆé»˜è®¤å€û|¼‰;
½W¬ä¸€ä¸ªå‚数需要实现Drawable接口的对象, 此接口包含了与graphic context.内部相联¾pÈš„æ–ÒŽ(gu¨©)³•。SWT提供了三个实现Drawable接口的类åQšImage, Device, å’?Control.


Control子类虽然都能包含囑ÖÅžåQŒä½†åªæœ‰ä¸€ä¸ªç±»æ˜¯ç‰¹åˆ«é€‚合GC对象的:(x¨¬)Canvas。它不仅提供了一个Compositeçš„containment propertyåQŒè¿˜å¯ä»¥ç”¨ä¸€¾pÕdˆ—的风格来定义囑ÖŞ在此区域内如何显½C?br />
½CÞZ¾‹åQ?/u>

package com.swtjface.Ch7;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;
public class DrawExample
{
public static void main (String [] args)
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Drawing Example");
Canvas canvas = new Canvas(shell, SWT.NONE);
canvas.setSize(150, 150);
canvas.setLocation(20, 20);//在shell中创建canvas
shell.open ();
shell.setSize(200,220);
GC gc = new GC(canvas);//在canvas中创�/font>graphic context
gc.drawRectangle(10, 10, 40, 45);
gc.drawOval(65, 10, 30, 35);
gc.drawLine(130, 10, 90, 80);
gc.drawPolygon(new int[] {20, 70, 45, 90, 70, 70});
gc.drawPolyline(new int[] {10,120,70,100,100,130,130,75});
gc.dispose();//释放Color对象
while (!shell.isDisposed())
{
 if (!display.readAndDispatch())
 display.sleep();
 }
 display.dispose();
 }
 }
有两炚wœ€è¦æ³¨æ„ï¼š(x¨¬)1.在调用shell.open()之前构徏Canvas对象åQŒç„¶åŽåœ¨è°ƒç”¨shell.open()之后创徏和ä‹É用GC对象
                     2.在ä‹É用完之后一定要立即释放GC object
如上例所½CºGC提供了一¾pÕdˆ—在Drawable对象上画囑ÖŞ的方法,如下åQ?br />

但是上例中有个问题:(x¨¬)当shell被变灰过或者最ž®åŒ–˜q‡ä¹‹åŽï¼Œå›‘ÖÅžž®×ƒ¼š(x¨¬)被擦厅R€‚所以我们需要解决的事,无论window怎么变化åQŒå›¾å½¢éƒ½ä¿æŒå¯è§ã€‚å› æ­¤SWT在一ä¸?/span>Drawable对象被刷新后让你自行控制。这个更新的˜q‡ç¨‹ž®Þp¢«¿UîCØ“(f¨´)paintingã€?/span>


Painting and PaintEvents

当一个GCæ–ÒŽ(gu¨©)³•在一个Drawabel对象上画å‡ÞZ¸€ä¸ªå›¾æ¡ˆï¼Œå®ƒä»…执行˜q™ä¸ªpainting˜q‡ç¨‹ä¸€‹Æ¡ã€‚如果用æˆäh”¹å˜å¯¹è±¡å°ºå¯¸æˆ–是用另一个窗口去覆盖它,则图形会(x¨¬)被消除。因此,应用½E‹åºèƒ½å¦åœ¨å¤–界事件媄响下¾l´æŒå…¶å¤–观这一点相当重要ã€?br />
˜q™äº›å¤–部事äšg被称为PaintEventsåQŒæŽ¥æ”¶å®ƒä»¬çš„½E‹åºæŽ¥å£æ˜¯PaintListener。一个Control在ä“Q何时候当其外观被应用½E‹åºæˆ–是外界‹zÕdŠ¨æ”¹å˜éƒ½ä¼š(x¨¬)触发一个PaintEvent。这些类对于事äšg和监听器的ä‹É用方式都和我们在½W¬å››ç« å†…提到的类伹{€‚由于PaintListener只有一个事件处理方法,所以不需要ä‹É用adapter¾c?br />
Canvas canvas = new Canvas(shell, SWT.NONE);
canvas.setSize(150, 150);
canvas.setLocation(20, 20);
canvas.addPaintListener(new PaintListener()
{
public void paintControl(PaintEvent pe)
{
GC gc = pe.gc;//每一个PaintEvent对象都包含有其自å·Þqš„GC
gc.drawPolyline(new int[] {10,120,70,100,100,130,130,75});
}
});
shell.open();

每一个PaintEvent对象都包含有其自å·Þqš„GCåQŒä¸»è¦æœ‰2个原因:(x¨¬)1.因䨓(f¨´)˜q™ä¸ªGC instance是由事äšg产生的,所以PaintEventä¼?x¨¬)负责释放他ã€?.应用½E‹åºå¯ä»¥åœ¨shell open之前创徏GC,˜q™æ ·å¯ä»¥ä½¿å›¾å½¢åœ¨ä¸€ä¸ªç‹¬ç«‹çš„¾cÖM¸­è¢«åˆ›å»ºã€?/font>

SWTåœ?/span>PaintListener接口内优åŒ?/span>painting˜q‡ç¨‹,SWT的开发者强烈徏è®?/span>Controlçš?/span>painting仅对PaintEvent作出反应。如果一个应用程序因为其他原因必™åÀL›´æ–°å…¶å›‘ÖÅžåQŒåˆ™ä»–们推荐使用controlçš?/span>redraw()æ–ÒŽ(gu¨©)³•åQŒè¿™ä¼?x¨¬)在队列中加入一ä¸?/span>paintè¯äh±‚。之后,你可以调ç”?/span>update()æ–ÒŽ(gu¨©)³•来处理所有的¾l‘定于该对象çš?/span>paintè¯äh±‚ã€?br />
需要牢记的是,虽然对于Control对象推荐在一个PaintListener内paintingåQŒä½†æ˜¯ç”±äºŽDeviceå’ŒImage对象òq¶ä¸èƒ½åœ¨è¯¥æŽ¥å£å†…使用。如果你需要在一个image或device内生成图形,你必™åÕd•独地生成一个GC对象òq¶åœ¨ä½¿ç”¨¾l“束后将光™”€æ¯ã€?/span>



]]>
6.5 Custom layoutshttp://www.aygfsteel.com/joeyjong/archive/2006/04/12/40691.htmlž®ç¬¼é¦’头ž®ç¬¼é¦’头Wed, 12 Apr 2006 09:19:00 GMThttp://www.aygfsteel.com/joeyjong/archive/2006/04/12/40691.htmlhttp://www.aygfsteel.com/joeyjong/comments/40691.htmlhttp://www.aygfsteel.com/joeyjong/archive/2006/04/12/40691.html#Feedback0http://www.aygfsteel.com/joeyjong/comments/commentRss/40691.htmlhttp://www.aygfsteel.com/joeyjong/services/trackbacks/40691.html 要客制化layoutåQŒéœ€è¦ç‘ô承抽象类LayoutåQŒéœ€è¦å†™2个方法——computeSize() å’Œlayout().

computeSize()

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--ä½œäØ“(f¨´)flagåQŒto tell the layout whether it’s safe to use any cached values that it may be maintaining.

computeSize()的目的主要在于计½Ž—我们要layoutçš„composite有多å¤?br />layout()
…�br />



]]>
6.4 The form layouthttp://www.aygfsteel.com/joeyjong/archive/2006/04/12/40607.htmlž®ç¬¼é¦’头ž®ç¬¼é¦’头Wed, 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不同åQŒform layout不是åŸÞZºŽè¡Œå’Œåˆ—çš„åQŒå®ƒæ˜¯åŸºäºŽä¸Žå…¶ä»–control之间的相对位¾|®çš„ã€?br />
FormLayout十分½Ž€å•,你只要:(x¨¬)1.讑֮š™åµè¾¹è·ï¼ˆé«˜ï¼Œå®½ï¼‰å±žæ€§ã€?2.讑֮šspacing属性,åÏx‰€æœ‰control间的距离(in pixels)

同样可以使用FormData来配¾|®å•个的controlã€?br />
FormData
如果一个control没有一个FormData实例来描˜q°å®ƒçš„话åQŒå°±ä¼?x¨¬)默认放在composite的右上角
width和height属性指定了control的尺寸,in pixels.
top, bottom, right, å’Œleft属性,每一个都有一个FormAttachment实例åQŒè¿™äº›attachments描述了control与其他control之间的关¾p…R€?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的位¾|®ï¼Œå› äØ“(f¨´)左上角是坐标原点å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下面åQŒcancel左边
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.htmlž®ç¬¼é¦’头ž®ç¬¼é¦’头Tue, 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 最常用的一¿Ulayout.以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个controlåQŒåŽä¸€ä¸ªå‚数是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
十分¾cÖM¼¼äºŽRowData对象。可通过其构造函数来讑֮šSTYLEåQŒè¿™äº›STYLE可分ä¸?¾c»ï¼š(x¨¬)FILL, HORIZONTAL_ALIGN, and VERTICAL_ALIGN.
1.FILL:æ­¤cell是否fill所有的availabeçš„ç©ºé—´ã€‚å¯ç”¨çš„å€ÆD¿˜åŒ…括F(xi¨¤n)ILL_HORIZONTALåQˆæ°´òqÏx‰©å¼ ï¼‰,FILL_VERTICALåQˆåž‚直扩张),FILL_BOTHã€?br />2.ALIGNåQŒç”¨æ¥æŒ‡å®šcontrol在cell中的什么位¾|®ã€‚值包括BEGINNING, END, CENTERå’ŒFILLã€?br />具体参见下表

Using GridData size attributes
与RowData不同åQŒGridData˜q˜æœ‰å¾ˆå¤šçš„public属性。其中有些是布尔值类型的åQŒä¸€èˆ¬ä¼š(x¨¬)æ ÒŽ(gu¨©)®æ‰€è®„¡½®çš„不同styles而自动管理,所以无需对其直接操作。还有一些是integerå€û|¼Œç”¨æ¥¼‹®å®šå•个cells的大ž®ã€‚具体äšg下表åQ?br />



]]>
6.2 The row layouthttp://www.aygfsteel.com/joeyjong/archive/2006/04/11/40479.htmlž®ç¬¼é¦’头ž®ç¬¼é¦’头Tue, 11 Apr 2006 07:58:00 GMThttp://www.aygfsteel.com/joeyjong/archive/2006/04/11/40479.htmlhttp://www.aygfsteel.com/joeyjong/comments/40479.htmlhttp://www.aygfsteel.com/joeyjong/archive/2006/04/11/40479.html#Feedback0http://www.aygfsteel.com/joeyjong/comments/commentRss/40479.htmlhttp://www.aygfsteel.com/joeyjong/services/trackbacks/40479.html可以多行/列显½Cºã€?br />
package com.swtjface.Ch6;
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.*;
public class Ch6RowLayoutComposite extends Composite {
public Ch6RowLayoutComposite(Composite parent) {
super(parent, SWT.NONE);
RowLayout layout = new RowLayout(SWT.HORIZONTAL);
setLayout(layout);
for (int i = 0; i < 16; ++i) {
Button button = new Button(this, SWT.NONE);
button.setText("Sample Text");
}
}
}

wrapâ€”â€”é»˜è®¤äØ“(f¨´)true,若设为false,所有的controls都在同一行ã€?br />packâ€”â€”é»˜è®¤äØ“(f¨´)true.使所有的child controls都大ž®ä¸€æ —÷€?br />justifyâ€”â€”é»˜è®¤äØ“(f¨´)false. è‹¥äØ“(f¨´)true,每一行的control都会(x¨¬)以间隔相同的方式排列ã€?br />
RowData
可以通过setLayoutData()来设定每个control的大ž®ï¼Œå¦‚:(x¨¬)button.setLayoutData(new RowData(200 + 5 * i, 20 + i));


]]>
6.1 The fill layouthttp://www.aygfsteel.com/joeyjong/archive/2006/04/11/40472.htmlž®ç¬¼é¦’头ž®ç¬¼é¦’头Tue, 11 Apr 2006 07:23:00 GMThttp://www.aygfsteel.com/joeyjong/archive/2006/04/11/40472.htmlhttp://www.aygfsteel.com/joeyjong/comments/40472.htmlhttp://www.aygfsteel.com/joeyjong/archive/2006/04/11/40472.html#Feedback0http://www.aygfsteel.com/joeyjong/comments/commentRss/40472.htmlhttp://www.aygfsteel.com/joeyjong/services/trackbacks/40472.html 默认ä¸ÞZ»Žå·¦åˆ°åÏxŽ’æ”„¡š„åQŒæ ¹æ®æ¯ä¸ªcontrol实际所需的大ž®æ¥åˆ†é…½Iºé—´åQŒæ­¤composite中多于出来的½Iºé—´åQŒå†òqÏx‘Šåˆ°æ¯ä¸ªcontrol上。随着composite的大ž®è°ƒæ•ß_(d¨¢)¼Œcontrol的大ž®ä¹Ÿä¼?x¨¬)跟着调整ã€?br />
package com.swtjface.Ch6;
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.*;
public class Ch6FillLayoutComposite extends Composite {
public Ch6FillLayoutComposite(Composite parent) {
super(parent, SWT.NONE);
FillLayout layout = new FillLayout( SWT.VERTICAL); //默认是SWT.HORIZONTAL
setLayout(layout);//为此Composite讑֮šä¸€ä¸ªlayout.如果漏了此语句,ä¼?x¨¬)显½CÞZ¸å‡ºchild controlã€?br />for (int i = 0; i < 8; ++i) {
Button button = new Button(this, SWT.NONE);
button.setText("Sample Text");
}
}
}



]]>
5.8 ProgressIndicatorhttp://www.aygfsteel.com/joeyjong/archive/2006/04/10/40291.htmlž®ç¬¼é¦’头ž®ç¬¼é¦’头Mon, 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¼Œå®ƒæ”¯æŒå·¥ä½œçš„虚拟单位åQŒyou 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需è¦?步:(x¨¬)
1.让indicator知道æ€Õd…±æœ‰å¤šž®‘工作,通过使用beginTask().只有˜q™ä¸ªæ–ÒŽ(gu¨©)³•被调用了之后åQŒè¿™ä¸ªcontrol才会(x¨¬)在屏òq•上昄¡¤ºã€?br />2.每当有一部分工作被完成了åQŒå°±è°ƒç”¨worked()ã€‚äØ“(f¨´)了防止非ui的线½E‹æ¥update widgetsåQŒæ‰€ä»¥ä‹É用asyncExec()来解册™¿™ä¸ªé—®é¢˜ã€?br />
ProgressIndicator也提供animated模式åQŒå³æ€Õd·¥ä½œé‡ä¸çŸ¥é“的情况。在˜q™ç§æ¨¡å¼ä¸‹ï¼Œthe bar continually fills and empties
until done() is called. 要ä‹É用这个模式,ž®Þp¦ç”¨beginAnimatedTask()代替beginTask();òq¶ä¸”不需要worked()æ–ÒŽ(gu¨©)³•äº?/font>

]]>
5.7 ProgressBarhttp://www.aygfsteel.com/joeyjong/archive/2006/04/10/40287.htmlž®ç¬¼é¦’头ž®ç¬¼é¦’头Mon, 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.htmlProgressBaråQŒè¿›åº¦æ¡åQŒæ˜¯ProgressIndicator的简化版本。大多数情况下推荐ä‹É用ProgressIndicator。如果你军_®šç›´æŽ¥ä½¿ç”¨ProgressBaråQŒéœ€è¦æ‰‹åŠ¨æ”¹å˜æ­¤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.


ž®ç¬¼é¦’头 2006-04-10 17:56 发表评论
]]>5.6 Sliderhttp://www.aygfsteel.com/joeyjong/archive/2006/04/10/40286.htmlž®ç¬¼é¦’头ž®ç¬¼é¦’头Mon, 10 Apr 2006 09:45:00 GMThttp://www.aygfsteel.com/joeyjong/archive/2006/04/10/40286.htmlhttp://www.aygfsteel.com/joeyjong/comments/40286.htmlhttp://www.aygfsteel.com/joeyjong/archive/2006/04/10/40286.html#Feedback0http://www.aygfsteel.com/joeyjong/comments/commentRss/40286.htmlhttp://www.aygfsteel.com/joeyjong/services/trackbacks/40286.html¾cÖM¼¼äºŽscrollbars。scrollbars仅限于用在可滑动的item上,如textã€?br />
可通过setMinimum()å’ŒsetMaximum()来设定它的范围。可通过setThumb()来设定滑块的倹{€‚在有些OS上,thumb的大ž®æ˜¯å¸¸æ•°ã€‚每按一下箭å¤ß_(d¨¢)¼Œæ‰€¿UÕdŠ¨çš„å€¼ç§°ä¸ºincrement.可通过setIncrement()来设定,按滑块和½Ž­å¤´é—´çš„½Iºé—´æ‰€æ»‘动的å€égØ“(f¨´)page incrementåQŒå¯é€šè¿‡PageIncrement()来设定。以上这些数据可以通过void setValues( int selection, int minimum, int maximum, int thumb, int increment, int pageIncrement)来一‹Æ¡æ€§è®¾å®šï¼Œå…¶ä¸­selection是thumb的出发点ã€?br />
Slider有个属性用来设定其是水òqŒ™¿˜æ˜¯åž‚ç›´çš„åQŒé»˜è®¤äØ“(f¨´)æ°´åã^ã€?br />
package com.swtjface.Ch5;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Slider;
public class Ch5Slider extends Composite {
public Ch5Slider(Composite parent) {
super(parent, SWT.NONE);
setLayout(new FillLayout());
Slider slider = new Slider(this, SWT.HORIZONTAL);
slider.setValues(1000, 400, 1600, 200, 10, 100);
}
}


]]>
5.5 CoolBarhttp://www.aygfsteel.com/joeyjong/archive/2006/04/10/40279.htmlž®ç¬¼é¦’头ž®ç¬¼é¦’头Mon, 10 Apr 2006 09:28:00 GMThttp://www.aygfsteel.com/joeyjong/archive/2006/04/10/40279.htmlhttp://www.aygfsteel.com/joeyjong/comments/40279.htmlhttp://www.aygfsteel.com/joeyjong/archive/2006/04/10/40279.html#Feedback0http://www.aygfsteel.com/joeyjong/comments/commentRss/40279.htmlhttp://www.aygfsteel.com/joeyjong/services/trackbacks/40279.html¾cÖM¼¼äºŽToolBar的升¾U§ã€‚他们的区别在于CoolBar上的item可以被重新配¾|®ï¼Œé‡æ–°å®šå¤§ž®ã€‚CoolBar的一般用途就是包含toolbars或按钮ã€?br />
String[] coolItemTypes = {"File", "Formatting", "Search"};
CoolBar coolBar = new CoolBar(parent, SWT.NONE);
for(int i = 0; i < coolItemTypes.length; i++)
{
CoolItem item = new CoolItem(coolBar, SWT.NONE);
ToolBar tb = new ToolBar(coolBar, SWT.FLAT);
for(int j = 0; j < 3; j++)
{
ToolItem ti = new ToolItem(tb, SWT.NONE);
ti.setText(coolItemTypes[i] + " Item #" + j);
}
}

]]>
5.4 ToolBarManagerhttp://www.aygfsteel.com/joeyjong/archive/2006/04/07/39866.htmlž®ç¬¼é¦’头ž®ç¬¼é¦’头Fri, 07 Apr 2006 08:32:00 GMThttp://www.aygfsteel.com/joeyjong/archive/2006/04/07/39866.htmlhttp://www.aygfsteel.com/joeyjong/comments/39866.htmlhttp://www.aygfsteel.com/joeyjong/archive/2006/04/07/39866.html#Feedback0http://www.aygfsteel.com/joeyjong/comments/commentRss/39866.htmlhttp://www.aygfsteel.com/joeyjong/services/trackbacks/39866.html 是JFace的类åQŒç‘ô承自ContributionManageråQŒå‡¡æ˜¯ç‘ô承了IAction或IContribution接口的对象都可被加至ToolBarManager.你只要花旉™—´ä¸ºToolBarManageræ·ÕdŠ ActionåQŒToolbarå’ŒToolItem实例ä¼?x¨¬)自动äñ”生ã€?br />
你可通过调用ApplicationWindowçš„createToolBarManager()æ¥äØ“(f¨´)你的应用½E‹åºæ·ÕdŠ ä¸€ä¸ªtoolbar。与MenuManager不同的是åQŒcreateToolBarManager()需要一个style参数åQŒè¿™ä¸ªå‚数用来设定ToolBar所用的按钮的风æ û|¼š(x¨¬)flat或normalã€?/font>

  • ControlContribution

除了MenuManager所用的ContributionItems之外åQŒè¿˜æœ‰ä¸€ä¸ªæ–°çš„ContributionItemåQŒåªèƒ½è¢«ToolBarManager使用——ControlContribution。这个类可将ä»ÖM½•能被用于toolbarçš„Control打包˜q›åŽ»ã€?br />
要ä‹É用ControlContribution¾c»ï¼Œå¿…须要实现抽象方法createControl().

toolBarManager.add(new ControlContribution("Custom") {
protected Control createControl(Composite parent) {
SashForm sf = new SashForm(parent, SWT.NONE);
Button b1 = new Button(sf, SWT.PUSH);
b1.setText("Hello");
Button b2 = new Button(sf, SWT.PUSH);
b2.setText("World");
b2.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
System.out.println("Selected:" + e);
}
});
return sf;
}
});

如果你希望有ä»ÖM½•的事件发生,必须在你的controls上实现SelectionListeners

  • Creating toolbars by hand

如果你不想ToolBarManager来创建toolbar的话åQŒå¯ä»¥æ‰‹åŠ¨åˆ›å»ºï¼Œéœ€è¦ç”¨åˆ°ToolBarå’ŒToolItem¾c?

Toolbar
是一个composite controlåQŒåŒ…含多个ToolItems.Toolbar由多个小图标按钮¾l„成åQŒä¸€èˆ¬æ˜¯16-by-16bitmap囄¡‰‡ã€‚每个按钮都对应一个ToolItem。Toolbar可以是水òq³çš„也可以是垂直的,默认为水òq?br />
ToolItem
每一个ToolItem都有一个图片,如果没有åQŒé»˜è®¤äØ“(f¨´)¾U¢è‰²æ–¹å—。When the user selects a ToolItem from the menu, it broadcasts the event to any registered SelectionListeners.Your application should register a listener with each ToolItem and use that listener to perform whatever logic corresponds to the menu item.



]]>
5.3 The Combo widgethttp://www.aygfsteel.com/joeyjong/archive/2006/04/07/39864.htmlž®ç¬¼é¦’头ž®ç¬¼é¦’头Fri, 07 Apr 2006 08:30:00 GMThttp://www.aygfsteel.com/joeyjong/archive/2006/04/07/39864.htmlhttp://www.aygfsteel.com/joeyjong/comments/39864.htmlhttp://www.aygfsteel.com/joeyjong/archive/2006/04/07/39864.html#Feedback0http://www.aygfsteel.com/joeyjong/comments/commentRss/39864.htmlhttp://www.aygfsteel.com/joeyjong/services/trackbacks/39864.html 三种¾cÕdž‹çš„Combo control:
1.Simple:默认¾cÕdž‹åQŒä¸€ä¸ªå¯¾~–辑的text field和一个供选择的list
2.Drop-down:下拉列表åQŒæ–‡æœ¬æ¡†å¯ç¼–è¾?br />3.Read-onlyåQšæ–‡æœ¬æ¡†ä¸å¯¾~–辑的下拉列表,可用select( 0 )来将光™»˜è®¤é€‰ä¸­åˆ—表中的首项ã€?br />
以上三种¾cÕdž‹å¯åœ¨æž„造函æ•îC¸­é€šè¿‡STYLE.*来设¾|®ã€?br />



]]>
5.2 JFace text supporthttp://www.aygfsteel.com/joeyjong/archive/2006/04/04/39169.htmlž®ç¬¼é¦’头ž®ç¬¼é¦’头Tue, 04 Apr 2006 07:02:00 GMThttp://www.aygfsteel.com/joeyjong/archive/2006/04/04/39169.htmlhttp://www.aygfsteel.com/joeyjong/comments/39169.htmlhttp://www.aygfsteel.com/joeyjong/archive/2006/04/04/39169.html#Feedback0http://www.aygfsteel.com/joeyjong/comments/commentRss/39169.htmlhttp://www.aygfsteel.com/joeyjong/services/trackbacks/39169.html 先要ž®†org.eclipse.text_x.y.zå’Œorg.eclipse.jface.text_x.y.z加到classpath

  • TextViewer and Document

两个重要的接口:(x¨¬)IDocumentå’ŒITextViewer。JFace为其提供了默认的实现ã€?br />
一ä¸?span lang="en-US">IDocument的实例持有被¾~–辑的真实的文本信息。它的主要实现是Document¾c…R€‚AbstractDocument提供了部分实玎ͼŒä½ å¯é€šè¿‡¾l§æ‰¿å®ƒæ¥æ·ÕdŠ è‡ªå·±çš„å®žçŽ°ã€‚IDocument允许通过IDocumentListener接口来获取内容编辑的通知ã€?br />
IDocument˜q˜æä¾›äº†ä»¥ä¸‹åŠŸèƒ½
Positions
可以¾l™æ¯ä¸€ä¸ªtext区域分配一个记åäh¥ä½œäØ“(f¨´)它的Position。当被指定给某个ducument时一个Position对象有an offset and a length of text。如果documentçš„text被更新的话,Position也会(x¨¬)同步更新åQŒæ‰€ä»¥ä»–永远都是指向同一ŒD‰|–‡å­—。Position¾cÀLœ¬íw«æä¾›äº†ä¸€äº›åŸºæœ¬çš„功能åQŒå¯é€šè¿‡¾l§æ‰¿ä»–来完善更多有用的功能ã€?br />
Partition content types
每个documentç”׃¸€ä¸ªæˆ–多个partitions¾l„成åQŒé€šè¿‡ITypedRegion接口来表现。每一个partition可以有各自的内容¾cÕdž‹åQŒå¦‚plain text, rich text, or HTML。要使用它,你要创徏一个IDocumentPartitioner然后assign¾l™ä½ çš„document,然后documentçš„partitionerž®×ƒ¼š(x¨¬)负责响应å¯ÒŽ(gu¨©)Œ‡å®šä½¾|®å†…容类型的查询åQŒå®ƒå¿…须通过实现computePartitioning()来返回包含此document中所有ITypedRegions的一个数¾l„。不需要实çŽîC½ è‡ªå·±çš„document partitioner。如果没有创建,整个documentž®±æ˜¯ä¸€ä¸ªåŒºåŸŸï¼Œ¾cÕdž‹ä¸ºIDocument.DEFAULT_CONTENT_TYPEã€?br />
Searching
IDocument通过search()提供了搜索的功能。不支持regular expressions or other patternsåQŒä½†æä¾›äº†search start location,direction, and case sensitivity and whether to match whole words only.

ITextViewerž®†ä¸€ä¸ªæ ‡å‡†çš„text widget转换成一个基于documentçš„text widget

ITextViewer的默认实现是TextVieweråQŒå®ƒä½¿ç”¨StyledText来显½Cºæ•°æ®ã€‚ITextViewer支持text modificationsçš„listeneråQŒä¹Ÿæ”¯æŒvisual eventsåQˆå¦‚改变viewportåQŒå³text的当前可视区域)的监听器ã€?br />
è™½ç„¶ä½œäØ“(f¨´)ITextViewer的默认应用,如果你想要修æ”ÒŽ(gu¨©)˜¾½Cºï¼ŒTextViewer允许你直接accessStyledTextåQŒä½†å»ø™®®ä½ ä‹É用TextPresentationåQŒå› ä¸ºå®ƒå¯ä»¥æ”‰™›†è¯¥æ–‡æ¡£ä¸­å¸¦æœ‰çš„各个不同的StyleRangesã€?br />
ITextViewer˜q˜æ”¯æŒå¾ˆå¤šä¸åŒç±»åž‹çš„æ’äšgåQŒå¯ç”¨æ¥ä¿®æ”¹widget的行为。可以被customized的功能有åQ?br />1.通过IUndoManager来支持undo
2.通过ITextDoubleClickStrategy来支持对鼠标双击的处ç?br />3.通过IAutoIndentStrategy来支持文本的自动¾~©è¿›
4.通过ITextHover来实玎ͼŒå½“鼠标停留在document的一个section上时åQŒæ˜¾½Cºtext.
要ä‹É用上˜q°æ’ä»Óž¼Œä½ éœ€è¦åˆ†é…ä¸€ä¸ªé€‚当的接口实例给text vieweråQŒç„¶åŽè°ƒç”¨activatePlugins().

如下列出了org.eclipse.jface.text的子包及(qi¨¢ng)其作ç”?br />



]]>
5.1 Editing text with SWThttp://www.aygfsteel.com/joeyjong/archive/2006/03/29/38098.htmlž®ç¬¼é¦’头ž®ç¬¼é¦’头Wed, 29 Mar 2006 09:42:00 GMThttp://www.aygfsteel.com/joeyjong/archive/2006/03/29/38098.htmlhttp://www.aygfsteel.com/joeyjong/comments/38098.htmlhttp://www.aygfsteel.com/joeyjong/archive/2006/03/29/38098.html#Feedback0http://www.aygfsteel.com/joeyjong/comments/commentRss/38098.htmlhttp://www.aygfsteel.com/joeyjong/services/trackbacks/38098.html 用于文本¾~–辑的controlæœ?个:(x¨¬)Textå’ŒStyledText.åŽè€…å¯ä»¥äØ“(f¨´)文本和control本èín讑֮šé¢œè‰²åQŒæ ¼å¼ç­‰ã€‚这两个control之间毫无兌™”åQŒé™¤äº†éƒ½æ˜¯Composite的子¾cÖM¹‹å¤–ã€?/font>

  • The basic Text widget

package com.swtjface.Ch5;

import org.eclipse.swt.SWT;

import org.eclipse.swt.events.VerifyEvent;

import org.eclipse.swt.events.VerifyListener;

import org.eclipse.swt.layout.FillLayout;

import org.eclipse.swt.widgets.Composite;

import org.eclipse.swt.widgets.Text;

public class Ch5Capitalizer extends Composite {

public Ch5Capitalizer(Composite parent) {

super(parent, SWT.NONE);

buildControls();

}

private void buildControls() {

this.setLayout(new FillLayout());

Text text = new Text(this, SWT.MULTI | SWT.V_SCROLL);

text.addVerifyListener(new VerifyListener() { //每当text被改变,ä»ÖM½•以注册的VerifyListeners便会(x¨¬)被调用。此处每按一‹Æ¡é”®ç›˜ï¼Œæ­¤æ–¹æ³•就被调用。如果是同时输入多个字符åQŒä¹Ÿè°ƒç”¨ä¸€‹Æ?/font>

public void verifyText(VerifyEvent e) {

if( e.text.startsWith("1") ) {

e.doit = false;

} //如果文本ä»?å¼€å¤ß_(d¨¢)¼Œå³ä¸å…è®¸¾~–辑

else {

e.text = e.text.toUpperCase();

}

}

});

}

}

Text的重要方法,见下�br />
insert()--doesn’t allow you to insert text into the existing content.

  • The StyledText widget

StyledText包含了一¾pÕdˆ—的应用到该小部äšg的预定义的动作,˜q™äº›æ˜¯å¸¸è§„的东西如:(x¨¬)剪切、粘贴、移动至下一个词、移动至文末。代表这些动作的帔R‡åœ?/span>org.eclipse.swt.custom½E‹åºåŒ…中çš?/span>ST¾cÖM¸­æœ‰å®šä¹‰ã€‚这些常量在两种情况下发挥功效:(x¨¬)首先åQŒä½ å¯ä»¥ä½¿ç”¨å®ƒä»¬½E‹åºæ€§åœ°ä½¿ç”¨invokeAction()æ–ÒŽ(gu¨©)³•调用ä»ÖM¸€çš„这些方法;其次åQŒä½ ä¹Ÿå¯ä»¥ä‹Éç”?/span>setKeyBinding()æ–ÒŽ(gu¨©)³•来将它们¾l‘定于键击行为ã€?/span>setKeyBinding()选定一个键åQˆå¯ä»¥é€šè¿‡è¯¸å¦‚Shift或是Ctrl之类的编辑键来修æ”?/span>SWT帔R‡ä¹‹ä¸€åQ‰ç»‘定于指定的动作。如下的例子中组合键Ctrl-Q¾l‘定于粘贴动作。引èµäh³¨æ„çš„æ˜¯è¿™òq¶ä¸æ„å‘³ç€ä¼?x¨¬)将默认键的¾l‘定清除åQŒè¯¥ä¸¤ä¸ª¾l‘定都会(x¨¬)生效ã€?/span>

相对于Text而言åQŒè¿˜æ·ÕdŠ äº†drawing line backgrounds and line styles的事ä»Óž¼Œå¯ä»¥é€šè¿‡æ­¤äº‹ä»¶æ¥æ”¹å˜æ•´è¡Œçš„style或背景颜艌Ӏ‚注意:(x¨¬)如果使用了LineStyleListener,ž®×ƒ¸èƒ½åœ¨StyledText实例上调用get/setStyleRange()åQ?如果使用了LineBackgroundListeneråQŒé‚£ä½ å°±ä¸èƒ½è°ƒç”¨getLineBackground() or setLineBackground().

可以通过使用一个StyledTextçš„StyleRanges来改变显½Cºçš„风格

StyleRange
StyledText通过使用StyleRange¾cÀL¥½Ž¡ç†å½“前所昄¡¤ºçš„不同styles。其所有的栏位都是public的可随意修改åQŒä½†æ˜¯è¦ä¸€ç›´åˆ°å½“æ­¤StyledText实例的setStyleRange()被调用之后才ä¼?x¨¬)生效ã€?br />
StyleRanges通过开始偏¿U»é‡å’Œé•¿åº¦æ¥è®‘Ö®štext的区域范围ã€?/font> StyleRange可设定背景和前景è‰ÔŒ¼Œé»˜è®¤ä¸ºnullåQŒè¿˜å¯è®¾å®šå­—体,SWT.NORMAL 或者SWT.BOLD.

similarTo()可用来判断两个StyleRange实例是否有同æ ïL(f¨¥ng)š„前景、背景和字体ã€?br />
当我们保存text之后åQŒå¯é€šè¿‡styledText.getStyleRanges()来获取style信息åQŒæ­¤å‡½æ•°ä¼?x¨¬)返回an array of StyleRange


toggleBold()--ž®†å·²è¾“入的文本在boldå’Œnormal之间切换åQŒæ˜¯è¢«ä¸€ä¸ªKeyListener调用的,此KeyListenerä¼?x¨¬)监听F(xi¨¤n)1是否被按ä¸?br />
A StyledText example
复制、粘贴功能不需要通过代码便可使用åQŒæ˜¯å’Œplatform的标准键盘快æäh–¹å¼ç›¸å…Œ™”çš?br />
ExtendedModifyListenerå’ŒModifyListener不同åQŒå‰è€…提供了关于what was doneçš„event¾l†èŠ‚åQŒè€ŒåŽè€…只是当¾~–辑懂作产生时notify,不会(x¨¬)åŽÕd‡†¼‹®çš„辨别到底何种修改发生了ã€?br />

package com.swtjface.Ch5;

import java.util.LinkedList;

import java.util.List;

import org.eclipse.swt.SWT;

import org.eclipse.swt.custom.*;

import org.eclipse.swt.events.KeyAdapter;

import org.eclipse.swt.events.KeyEvent;

import org.eclipse.swt.layout.FillLayout;

import org.eclipse.swt.widgets.Composite;

public class Ch5Undoable extends Composite {

private static final int MAX_STACK_SIZE = 25;

private List undoStack;

private List redoStack;

private StyledText styledText;

public Ch5Undoable(Composite parent) {

super(parent, SWT.NONE);

undoStack = new LinkedList();

redoStack = new LinkedList();

buildControls();

}

private void buildControls() {

this.setLayout(new FillLayout());

styledText = new StyledText(this, SWT.MULTI | SWT.V_SCROLL);

styledText.addExtendedModifyListener(

new ExtendedModifyListener() { //每次text被编辑的时候,都会(x¨¬)调用此listener

public void modifyText(ExtendedModifyEvent event) {

String currText = styledText.getText();

String newText = currText.substring(event.start,

event.start + event.length); //获得新插入的文本

if( newText != null && newText.length() > 0 ) {

if( undoStack.size() == MAX_STACK_SIZE ) {

undoStack.remove( undoStack.size() - 1 );

}

undoStack.add(0, newText);//ž®†æ–°æ’入的文本保存到undoStackä¸?/font>

}

}

}); //关键部分

styledText.addKeyListener(new KeyAdapter() {

public void keyPressed(KeyEvent e) {

switch(e.keyCode) {

case SWT.F1:

undo(); break;

case SWT.F2:

redo(); break;

default: //ignore everything else

}

}

});

}

private void undo() {

if( undoStack.size() > 0 ) {

String lastEdit = (String)undoStack.remove(0);//得到要undo的字½W?/font>

int editLength = lastEdit.length();

String currText = styledText.getText();

int startReplaceIndex = currText.length() - editLength;

styledText.replaceTextRange(startReplaceIndex, editLength, ""); //ž®†æœ€åŽè¾“入的字符替换成空

redoStack.add(0, lastEdit);//把最后的˜q™ä¸ªundo的字½W¦åŠ åˆ°redoStackä¸?/font>

}

}

private void redo() {

if( redoStack.size() > 0 ) {

String text = (String)redoStack.remove(0);//得到要恢复的字符

moveCursorToEnd();

styledText.append(text);//ž®†è¦æ¢å¤çš„å­—½W¦åŠ è‡Ïx–‡æœ¬çš„æœ€å?/font>

moveCursorToEnd();

}

}

private void moveCursorToEnd() {

styledText.setCaretOffset(styledText.getText().length());

}

}



]]>
4.2 Event processing in JFacehttp://www.aygfsteel.com/joeyjong/archive/2006/03/24/37241.htmlž®ç¬¼é¦’头ž®ç¬¼é¦’头Fri, 24 Mar 2006 09:02:00 GMThttp://www.aygfsteel.com/joeyjong/archive/2006/03/24/37241.htmlhttp://www.aygfsteel.com/joeyjong/comments/37241.htmlhttp://www.aygfsteel.com/joeyjong/archive/2006/03/24/37241.html#Feedback1http://www.aygfsteel.com/joeyjong/comments/commentRss/37241.htmlhttp://www.aygfsteel.com/joeyjong/services/trackbacks/37241.html ActionContributionItem--combines the function of a GUI widget and its attached listener class.
Action--处理事äšg
与SWTçš„listener/event模式很类ä¼û|¼Œä½†æ˜¯å…¶class更抽象,更易于ä‹É用,scope更窄ã€?/font>

  • actions and contributions

Action--可以½Ž€å•的理解成一个命令,可以兌™”到菜单,工具条,以及(qi¨¢ng)按钮
Contribution--åœ?/span>JFace里面åQŒä¸€ä¸?/span>Action可以对应多个GUI对象åQŒè¿™äº›å¯¹è±¡å°±æ˜¯æ‰€è°“çš„Contribution Item. 有两个主要的Contribution¾c»ï¼š(x¨¬)ContributionItemå’ŒContributionManageråQŒå®ƒä»¬éƒ½æ˜¯æŠ½è±¡ç±»åQŒé å…¶å­¾cÀL¥å®žçŽ°äº‹äšg的处理。ç‘ô承关¾p»è§ä¸‹å›¾
ContributionItem--引发事äšg的单独GUI¾l„äšg
ContributionManager--产生包含ContributionItems的对�br />
ActionContributionItem--最重要åQŒåœ¨ApplicationWindow中创建和实施åQŒæ¥ž®†ä¸€ä¸ªaction˜qžæŽ¥è‡Ïx­¤GUIåQŒå®ƒè™½æ²¡æœ‰è®¾å®šå¥½çš„外观,但是依赖于你使用的fill()æ–ÒŽ(gu¨©)³•åQŒå´å¯ä»¥å¸®åŠ©ä¸€ä¸ªæŒ‰é’®ã€èœå•æ å’Œå·¥å…äh çš„æˆå½?br />
另一个与Contribution协作的方法是通过ContributionManager,它的子类¾cÖM¼¼äºŽContributionItemçš„container。其中MenuManagerž®†ContributionItems¾l„合在窗口最高层菜单åQ?ToolBarManager则将˜q™äº›å¯¹è±¡æ”‘Öœ¨ä»…在菜单之下的toolbar中ã€?/font>

  • 创徏Action¾c?/font>

Action是抽象类�/font>

package com.swtjface.Ch4;
import org.eclipse.jface.action.*;
import org.eclipse.jface.resource.*;
public class Ch4_StatusAction extends Action
{
StatusLineManager statman;
short triggercount = 0;
public Ch4_StatusAction(StatusLineManager sm)
{
super("&Trigger@Ctrl+T",
AS_PUSH_BUTTON);//在T字母之前çš?amp;½W¦å·æ„å‘³ç€˜q™ä¸ªå­—母ž®†ä½œä¸ø™¯¥åŠ¨ä½œçš„å¿«æ·é”®ã€‚è€Œåœ¨TEXT领域内的“Ctrl+T”确保了当用户在同时按下Ctrl键和T键时该动作就ä¼?x¨¬)被‹È€å‘ã€?/font>
statman = sm;
setToolTipText("Trigger the Action");
setImageDescriptor(ImageDescriptor.createFromFile
(this.getClass(),"eclipse.gif"));
}
public void run() //每次当Ch4_StatusAction被生成,run()æ–ÒŽ(gu¨©)³•ž®Þp¢«è°ƒç”¨
{
triggercount++;
statman.setMessage("The status action has fired. Count: " +
triggercount);
}

  • Implementing contributions in an ApplicationWindow

package com.swtjface.Ch4;

import org.eclipse.swt.*;

import org.eclipse.swt.widgets.*;

import org.eclipse.jface.window.*;

import org.eclipse.jface.action.*;

public class Ch4_Contributions extends ApplicationWindow {

StatusLineManager slm = new StatusLineManager();

Ch4_StatusAction status_action = new Ch4_StatusAction(slm); //ç”?font face="Verdana">StatusLineManager的对象作参数åQ?/font>创徏了一ä¸?font face="Verdana">Ch4_StatusAction的实ä¾?/font>

ActionContributionItem aci = new ActionContributionItem(status_action); //ç”?font face="Verdana" size="2">Ch4_StatusAction的对象作参数åQŒåˆ›å»ÞZº†ActionContributionItem对象

public Ch4_Contributions() {

super(null); // 创徏�/font> ApplicationWindow对象

addStatusLine();

addMenuBar();

addToolBar(SWT.FLAT | SWT.WRAP); //在窗口上æ·ÕdŠ äº†status line, menu, toolbar

}

protected Control createContents(Composite parent) {

getShell().setText("Action/Contribution Example");

parent.setSize(290,150); //讄¡½®äº†çª—口的titleå’Œsize

aci.fill(parent); // ž®†ActionContributionItem攑֜¨GUIä¸­ã€‚å› ä¸ø™¿™é‡Œçš„参数是Composite对象åQŒæ‰€ä»¥æ ¹æ®Actionçš„STYLE属性来¼‹®å®šã€‚此处是ButtonåQŒå› ä¸?font color="#0000ff">Ch4_StatusAction çš„STYLE属性是AS_PUSH_BUTTON;

return parent;

}

public static void main(String[] args) {

Ch4_Contributions swin = new Ch4_Contributions();

swin.setBlockOnOpen(true);

swin.open();

Display.getCurrent().dispose();

}

protected MenuManager createMenuManager() {

MenuManager main_menu = new MenuManager(null);

MenuManager action_menu = new MenuManager("Menu");

main_menu.add(action_menu);

action_menu.add(status_action); //å…Œ™”status_action.created and added to the menu in the form of a menu item

return main_menu;

}

protected ToolBarManager createToolBarManager(int style) {

ToolBarManager tool_bar_manager = new ToolBarManager(style);

tool_bar_manager.add(status_action); //å…Œ™”status_action。created and added to the toolbar as a toolbar item.

return tool_bar_manager;

}

protected StatusLineManager createStatusLineManager() {

return slm;

}

}

  • Interfacing with contributions

两个途径来将ActionContributionItemæ·ÕdŠ åˆ°GUIåQ?br />1. 通过ContributionManager子类的add()æ–ÒŽ(gu¨©)³•ã€?br />åQ?åQ‰å¯æŽ¥å—Action对象的参敎ͼŒä»Žè€Œé—´æŽ¥çš„ž®†ContributionItemå’ŒContributionManagerå…Œ™”。可多次执行
åQ?åQ‰å¯ç›´æŽ¥æŽ¥å—ActionContributionItem对象的参数。只可执行一‹Æ?br />2.通过ActionContributionItem¾cȝš„fill()æ–ÒŽ(gu¨©)³•。根据其参数的不同,所先是的组件也不同åQŒå…·ä½“见下表åQ?br />

  • Exploring the Action class

Important methods of the Action class


Property methods for the Action class

DESCRIPTION--written to a status line to provide additional help.

Style methods for the Action class

如果ENABLED是FALSEåQŒåˆ™å˜ç°ã€‚CHECKED主要用于radioå’Œcheckbox

Accelerator key / keyboard methods for the Action class

Accelerator keys--鼠标点击的键盘块æäh–¹å¼?/font>

Listener methods for the Action class

虽然JFace使用action代替了SWTçš„listener/event机制åQŒä½†æ˜¯Actiono¾cÖM»ç„¶å¯ä»¥å’Œlistener协作来处理特定需求的事äšgã€?br />IPropertyChangeListener接口å…Ïx³¨å®¢æˆ·è‡ªå®šä¹‰çš„PropertyChangeEvents,当所¾l™çš„对象按照你所¾l™çš„æ–¹å¼å˜æˆå¦ä¸€ä¸ªå¯¹è±¡æ—¶åQŒæ­¤äº‹äšg被触发ã€?br />
Miscellaneous methods of the Action class



]]>
4.1 Event processing in SWThttp://www.aygfsteel.com/joeyjong/archive/2006/03/23/37061.htmlž®ç¬¼é¦’头ž®ç¬¼é¦’头Thu, 23 Mar 2006 08:32:00 GMThttp://www.aygfsteel.com/joeyjong/archive/2006/03/23/37061.htmlhttp://www.aygfsteel.com/joeyjong/comments/37061.htmlhttp://www.aygfsteel.com/joeyjong/archive/2006/03/23/37061.html#Feedback0http://www.aygfsteel.com/joeyjong/comments/commentRss/37061.htmlhttp://www.aygfsteel.com/joeyjong/services/trackbacks/37061.html
  • The SWT event-processing cycle

  • 1. Once an SWT application begins running, its Display class sorts through this queue using its readAndDispatch() method and msg field, which acts as a handle to the underlying OS message queue.
    2. If it finds anything relevant, it sends the event to its top-level Shell object, which determines which widget should receive the event.
    3. The Shell then sends the event to the widget that the user acted on, which transfers this information to an associated interface called a listener.
    4. One of the listener’s methods performs the necessary processing or invokes another method to handle the user’s action, called an event handler.


    所在包åQšorg.eclipse.swt.events   

    • typed listeners and events

    typed listeners--只对某一¾cȝš„用户事äšg起作用,¾l§æ‰¿TypedListener¾c?br />typed events--与此¾cȝ‰¹å®šåŠ¨ä½œç›¸å…³çš„äº‹äšgåQŒç‘ô承TypedEvent¾c?br />
    可通过add...Listener()method with the typed listener as the argument来将listener附加到widget�br />
    完整的typed eventså’Œlisteners列表åQŒå¦‚下:(x¨¬)
     

    TypedEvent¾cÕdŒ…含了一些member field,他们提供与事件发生相关的一些信息,˜q™äº›ä¿¡æ¯å¯ä»¥åœ¨event handler中ä‹Éç”¨æ¥èŽ·å¾—ä¸ŽçŽ¯å¢ƒç›¸å…³çš„ä¿¡æ¯ã€‚ä¸‹å›¾äØ“(f¨´)¾l§æ‰¿è‡ªTypedEventå?qi¨¢ng)EventObject¾cȝš„fields:

    除此之外åQŒå¾ˆå¤šçš„event¾c»è¿˜æœ‰å…¶ä»–用来提供更多用户动作信息的fields,如MouseEvent¾cȝš„button field

    要将listener加入到code中,有两个主要的æ–ÒŽ(gu¨©)³•åQ?br />½W¬ä¸€ä¸ªæ˜¯åœ¨componentçš„add...Listener()中创å»ÞZ¸€ä¸ªåŒ¿åæŽ¥å£ï¼Œ˜q™æ ·å¯ä»¥ä½¿listener的作用域仅限于此component,½CÞZ¾‹ä»£ç å¦‚下åQ?br />Button button = new Button(shell, SWT.PUSH | SWT.CENTER);//创徏了一个buttonåQŒåƈž®†å…¶åŠ å…¥åˆ°shellä¸?br />button.addMouseListener(new MouseListener() //创徏了一个匿名MouseListener接口åQŒåƈž®†å…¶ä¸Žbuttonå…Œ™”
    {
    public void mouseDown(MouseEvent e)
    {
    clkdwnEventHandler();
    }
    public void mouseUp(MouseEvent e)
    {
    clkupEventHandler();
    }
    public void mouseDoubleClick(MouseEvent e)
    {
    dblclkEventHandler(); //此接口中必须被实现的三个æ–ÒŽ(gu¨©)³•。一旦鼠标按下,攑ּ€æˆ–双击,ž®×ƒ¼š(x¨¬)有一个MouseEvent被发送到˜q™ä¸‰ä¸ªæ–¹æ³•中的一个,然后åQŒæ­¤æ–ÒŽ(gu¨©)³•再调用相兌™”çš„event-handlingæ–ÒŽ(gu¨©)³•åQˆå³ä¸‹æ–‡ä¸­çš„三个åQ‰ã€?/font>
    }
    });
    static void dblclkEventHandler()
    {
    System.out.println("Double click.");
    }
    static void clkdwnEventHandler()
    {
    System.out.println("Click - down.");
    }
    static void clkupEventHandler()
    {
    System.out.println("Click - up.");//event-handlers通过发送message到console来完成事件处�/font>
    }

    上一¾cÀL–¹æ³•çš„¾~ºç‚¹æ˜¯æ­¤listener仅限于此component内,而第二种æ–ÒŽ(gu¨©)³•便可解决˜q™ç§é—®é¢˜--独立的声明一个ç‘ô承MouseListener的接口,½CÞZ¾‹ä»£ç å¦‚下åQ?br />Button button = new Button(shell, SWT.PUSH | SWT.CENTER);
    button.addMouseListener(ExampleMouseListener);
    MouseListener ExampleMouseListener = new MouseListener()
    {
    public void mouseDoubleClick(MouseEvent e)
    {
    System.out.println("Double click.");
    }
    public void mouseDown(MouseEvent e)
    {
    System.out.println("Click - down.");
    }
    public void mouseUp(MouseEvent e)
    {
    System.out.println("Click - up.");
    }
    };

    使用MouseListener的缺点就是哪怕你只关心鼠标双å‡ÖMº‹ä»Óž¼Œå´ä»è¦å£°æ˜Žå…¶æŽ¥å£ä¸­æ‰€åŒ…含的所有方法ã€?/font> 

    • Adapter

    Adapter是ç‘ô承了Listener接口òq¶æä¾›äº†æ‰€æœ‰requiredæ–ÒŽ(gu¨©)³•的实现的abstract¾c…R€‚也ž®±æ˜¯è¯´å¦‚果你使用adapter而不是listener的话åQŒä½ åªéœ€è¦å†™ä½ æ„Ÿå…´è¶£çš„æ–¹æ³?br />
    只有那些listener有多个成员方法的event才有adapter,其完整列表见下图åQ?br />
    adapter同样是通过add...Listener()æ–ÒŽ(gu¨©)³•来创建的åQŒä¸Žlistener¾cÖM¼¼åQŒåŒæ ·ä¹Ÿå¯é€šè¿‡åŒ¿å¾cÕd’Œæœ¬åœ°¾cÖM¸¤¿Uæ–¹æ³•,下例为匿名类æ–ÒŽ(gu¨©)³•åQ?br />button.addMouseListener(new MouseAdapter()
    {
    public void mouseDoubleClick(MouseEvent e)
    {
    dblclkEventHandler();
    }
    )};
    static void dblclkEventHandler()
    {
    System.out.println("Double click.");
    }

    • Keyboard events

    ä»ÖM½•时候只要key被按下,ž®×ƒ¼š(x¨¬)创徏KeyEventåQŒå®ƒæœ‰ä¸¤ä¸ªå­¾c»ï¼š(x¨¬)TraverseEvent å’ŒVerifyEvent.
    TraverseEvent--当按下arrow key或tab key来focus on text widget�br />VerifyEvent--fires when the user enters text that the program needs to check before taking further action.

    除了¾l§æ‰¿è‡ªTypedEventå’ŒEventObjectçš„field,KeyEvent˜q˜åŒ…括三个member field来提供那些与触发事äšgçš„key相关的信息,具体如下åQ?br />character--代表被按下keyçš„char
    stateMask--Returns an integer representing the state of the keyboard modifier keys.By examining this integer, a program can determine whether any of the Alt, Ctrl, Shift, and Command keys are currently pressed.
    keyCode--Provides the SWT public constant corresponding to the typed key. KeyCode列表åQŒè§ä¸‹å›¾åQ?br />

    TraverseEvent中有两个fields:
    1. doit--˜q”回布尔å€û|¼Œè‹¥äØ“(f¨´)真则允许在各个component间切换focus,è‹¥äØ“(f¨´)假则不允许切换focus
    2. detail--It’s an integer that represents the identity of the key that caused the event. For example, if the user presses the Tab key to switch to a new component, the detail field will contain the SWT constant TRAVERSE_TAB_NEXT.

    每个¾cÕdž‹çš„Control对于所¾l™çš„traversal key都有不同的默认behavioråQŒå¦‚果设doit为trueåQŒåˆ™override了其默认的设¾|®ã€?br />
    VerifyEvent的field:
    1.startå’Œend--讑֮šäº†è¾“入的范围
    2.text--contains the input String under examination.
    3.doit--Having looked at the user’s text, you set the boolean doit field to allow (TRUE) or disallow (FALSE) the action.

    • untyped events

    更灵‹z»ï¼Œä½†ä¸å®‰å…¨åQŒä¸æŽ¨èä½¿ç”¨ã€?br />当一个代表着非类型化监听器的监听器类和GUI的某一¾l„äšg相联¾pÀL—¶åQŒå®ƒ?y¨­u)®Þpƒ½æŽ¥å—该组件所能发送的ä»ÖM¸€¾cÖMº‹ä»¶ã€‚因此,你需要操控这由Event¾cÖM»£è¡¨çš„æ•获的全部事ä»Óž¼Œå†›_®šç”¨æˆ·æ‰§è¡Œçš„那个动作。然后,正确的事件处理方法就被调用ã€?br />
    不是被包含在org.eclipse.swt.events包中åQŒè€Œæ˜¯è¢«åŒ…含在org.eclipse.swt.widgets包中ã€?br />
    代码½CÞZ¾‹å¦‚下åQ?br />Listener listener = new Listener ()
    {
    public void handleEvent (Event event)
    {
    switch (event.type)
    {
    case SWT.KeyDown:
    if (event.character == 'b')
    System.out.println("Key"+event.character);
    break;
    case SWT.MouseDown:
    if (event.button == 3)
    System.out.println("Right click");
    break;
    case SWT.MouseDoubleClick:
    System.out.println("Double click");
    break;
    }
    }
    };
    Button button = new Button(shell, SWT.CENTER);
    button.addListener(SWT.KeyDown, listener);
    button.addListener(SWT.MouseDown, listener);
    button.addListener(SWT.MouseDoubleClick, listener);


    Event¾cÕdŒ…含了所有typed event中的所有field,此外˜q˜æœ‰ä¸€ä¸ªtype fieldåQŒå…¶æ‰€æœ‰çš„值列表如下:(x¨¬)



    ]]>
    3.4 Compositeshttp://www.aygfsteel.com/joeyjong/archive/2006/03/22/36892.htmlž®ç¬¼é¦’头ž®ç¬¼é¦’头Wed, 22 Mar 2006 09:55:00 GMThttp://www.aygfsteel.com/joeyjong/archive/2006/03/22/36892.htmlhttp://www.aygfsteel.com/joeyjong/comments/36892.htmlhttp://www.aygfsteel.com/joeyjong/archive/2006/03/22/36892.html#Feedback0http://www.aygfsteel.com/joeyjong/comments/commentRss/36892.htmlhttp://www.aygfsteel.com/joeyjong/services/trackbacks/36892.html
  • Composite¾c?/strong>
  • getChildren()
    lists the children of a Composite as an array of Control objects.
    getLayout()、setLayout(Layout)
    处理layout对象
    getTabList()、setTabList(Control[])
    指定widgets在Composite中的Tab™åºåºåQˆæŒ‰é”®ç›˜tab键的切换™åºåºåQ?br />
    Composite¾cÀL˜¯Scrollable¾cȝš„直接子类åQŒè¿™ž®±æ˜¯è¯´SWT/JFace中的所有Composite对象都有Scrollbars.所有的Scrollable对象都可使用如下æ–ÒŽ(gu¨©)³•åQ?br />getClientArea()ã€?computeTrim(int, int,int, int)
    trim--Composite的不可编辑区域,如title bars, scrollbars, 或status bars½{?
    client area--那些可编辑,使用的区�br />getClientArea()--Returns the available display area of a Scrollable object
    computeTrim(int,int,int,int)--Returns the necessary dimensions of the Composite for the desired client area
    getHorizontalBar()、getVerticalBar()
    Returns the horizontal/vertical ScrollBar object

    Control, Scrollable, and Composite的关¾p»ï¼Œå¦‚下å›?br />  

    • Groups

    Composite子类中最½Ž€å•的一个,自èín不执行ä“Q何动作,仅仅是将一¾l„子widgets用矩形边框è“v来,此边框类ä¼égºŽä¹‹å‰æåˆ°çš„分隔符åQŒåŒæ ·ä¹Ÿæä¾›SWT.SHADOW_IN, SWT.SHADOW_OUT, SWT.SHADOW_NONE风格åQŒè¿˜å¯ä»¥é€šè¿‡é€‰æ‹©SWT.SHADOW_ETCHED_IN or SWT.SHADOW_ETCHED_OUT来客制化阴媄效果ã€?br />
    可以通过setText()æ–ÒŽ(gu¨©)³•来设定它的label

    与许多Widgetså­ç±»ä¸€æ øP¼ŒGroup¾cÖM¸å¯è¢«¾l§æ‰¿ã€?/font>

    • SashForms

    可以在子widgets之间创徏可移动的分界åQŒæ­¤åˆ†ç•Œ¿UîCØ“(f¨´)Sash.Sash¾cÕd¯ä»¥åœ¨org.eclipse.swt.widgets包中扑ֈ°åQŒè€ŒSashForms却存在于org.eclipse.swt.custom包中ã€?br />
    SWT.HORIZONTALå’ŒSWT.VERTICAL--用来讑֮šSash的方å?br />setOrientation()--用来讑֮šSash的方å?br />getMaximizedControl()--returns the Control object that has been expanded the most.
    getWeights()--returns an int array containing the weight of each of the SashForm’s children.uses an int array to specify weights for each of the widgets in the Composite.

    • TabFolders

    creating and populating a TabFolder的四个步骤:(x¨¬)
    1. 创徏一个TabFolder实例
    2. 为每个页面构å»ÞZ¸€ä¸ªTabItem对象
    3. 用setText()æ–ÒŽ(gu¨©)³•来设定tabçš„label
    4. setControl()--å…Œ™”一个controlåQŒå½“它的tab被选中æ—Óž¼Œæ˜„¡¤ºæ­¤control

    TabFolder¾cÀLä¾›äº†ä¸€äº›ç”¨æ¥èŽ·å¾—TabItems信息的方法,如下åQ?br />getItemCount()
    ˜q”回此TabFolder所包含的TabItemsæ•?br />getItems()
    ˜q”回TabItems对象的数¾l?br />getSelection()
    ¼‹®å®šuser选择了哪个TabItem
    setSelection()
    Makes this decision from within the application



    ]]>
    3.3 Buttonshttp://www.aygfsteel.com/joeyjong/archive/2006/03/21/36656.htmlž®ç¬¼é¦’头ž®ç¬¼é¦’头Tue, 21 Mar 2006 09:03:00 GMThttp://www.aygfsteel.com/joeyjong/archive/2006/03/21/36656.htmlhttp://www.aygfsteel.com/joeyjong/comments/36656.htmlhttp://www.aygfsteel.com/joeyjong/archive/2006/03/21/36656.html#Feedback0http://www.aygfsteel.com/joeyjong/comments/commentRss/36656.htmlhttp://www.aygfsteel.com/joeyjong/services/trackbacks/36656.html
  • SWT.PUSH
  • Button¾cȝš„默认风格ã€?br />SWT.FLAT

    • SWT.ARROW

    与SWT.UP, SWT.DOWN, SWT.LEFT, and SWT.RIGHT¾l„合åQŒä¹Ÿå¯ä‹É用SWT.FLAT

    • SWT.TOGGLE

    ¾cÖM¼¼äºŽSWT.PUSHåQŒä½†æ˜¯æŒ‰ä¸‹åŽä¼?x¨¬)保持按下状态。可通过setSelection(boolean)æ–ÒŽ(gu¨©)³•来改变其状态。接下去介绍的check按钮和radio按钮也share˜q™ä¸ªåŠŸèƒ½

    • SWT.CHECK

    廸™®®ç”¨æ•°¾l„来实现,如下åQ?br />Button[] checks = new Button[2];
    checks[0] = new Button(shell, SWT.CHECK);
    checks[0].setText("Choice 1");
    checks[0].setLocation(10,5);
    checks[0].pack();
    checks[1] = new Button(shell, SWT.CHECK);
    checks[1].setText("Choice 2");
    checks[1].setLocation(10,30);
    checks[1].pack();

    • SWT.RADIO

    廸™®®ç”¨æ•°¾l„来实现,如下åQ?/font>
    Button[] radios = new Button[3];
    radios[0] = new Button(shell, SWT.RADIO);
    radios[0].setSelected(true);
    radios[0].setText("Choice 1");
    radios[0].setLocation(10,5);
    radios[0].pack();
    radios[1] = new Button(shell, SWT.RADIO);
    radios[1].setText("Choice 2");
    radios[1].setLocation(10,30);
    radios[1].pack();
    radios[2] = new Button(shell, SWT.RADIO);
    radios[2].setText("Choice 3");
    radios[2].setLocation(10,55);
    radios[2].pack();
    for (int i=0; i<radios.length; i++)
    if (radios[i].getSelected())
    System.out.println(i);


    通过RadioGroupFieldEditorsž®†radio buttons¾l„合在一èµ?

    RadioGroupFieldEditor rgfe = new RadioGroupFieldEditor(
    "UserChoice", "Choose an option:", 1,
    new String[][] {{"Choice1", "ch1"},
    {"Choice2", "ch2"},
    {"Choice3", "ch3"}},
    shell, true);

    其中的各个参数含义如下—â€?.name for the type of value returned by the editor.2.group label.3.列数ã€?.creates a set of option names with their associated values.In this manner, the RadioGroupFieldEditor can display a series of radio buttons without allocating Button objects.5.ž®†editor加至Shell对象ã€?.specifies whether the radio buttons should be incorporated in a Group object.

    在SWT/JFaceä¸?container widgets是由Composite¾cÀL¥æä¾›çš„ã€?/font>



    ]]>
    3.2 Labelshttp://www.aygfsteel.com/joeyjong/archive/2006/03/21/36651.htmlž®ç¬¼é¦’头ž®ç¬¼é¦’头Tue, 21 Mar 2006 08:32:00 GMThttp://www.aygfsteel.com/joeyjong/archive/2006/03/21/36651.htmlhttp://www.aygfsteel.com/joeyjong/comments/36651.htmlhttp://www.aygfsteel.com/joeyjong/archive/2006/03/21/36651.html#Feedback0http://www.aygfsteel.com/joeyjong/comments/commentRss/36651.htmlhttp://www.aygfsteel.com/joeyjong/services/trackbacks/36651.html 用于在GUI中显½Cºé™æ€çš„信息åQšString或Image

    • 风格和分隔符

    寚w½æ–¹å¼åQšSWT.CENTER,SWT.LEFT, and SWT.RIGHT.
    分隔½W¦ï¼š(x¨¬)SWT.SEPARATOR;SWT.VERTICAL, SWT.HORIZONTAL;SWT.SHADOW_IN, SWT.SHADOW_OUT, and SWT.SHADOW_NONE.

    • Label的方æ³?/font>



    ]]>
    3.1 Introducing the Widget and Control classeshttp://www.aygfsteel.com/joeyjong/archive/2006/03/21/36641.htmlž®ç¬¼é¦’头ž®ç¬¼é¦’头Tue, 21 Mar 2006 07:11:00 GMThttp://www.aygfsteel.com/joeyjong/archive/2006/03/21/36641.htmlhttp://www.aygfsteel.com/joeyjong/comments/36641.htmlhttp://www.aygfsteel.com/joeyjong/archive/2006/03/21/36641.html#Feedback0http://www.aygfsteel.com/joeyjong/comments/commentRss/36641.htmlhttp://www.aygfsteel.com/joeyjong/services/trackbacks/36641.html
  • Widget¾c?/strong>
  • 是所有widget的父¾c…R€‚䨓(f¨´)抽象¾c»ï¼Œè€Œä¸”Eclipse.org也强烈反对去¾l§æ‰¿å®ƒï¼Œå› æ­¤ä½ æ—¢ä¸èƒ½¾l§æ‰¿ä»–,也不能直接ä‹É用它
    Widget¾cÖM¸­çš„重要方法,如下åQ?/font>

    • Control objects

    Control¾cÖM¸­çš„对象在OS中有一个直接的副本åQŒä½ å¯ä»¥é€šè¿‡¾cȝš„handle field来直接access.然而SWT˜q˜æä¾›äº†ä¸€äº›åœ¨Control¾cÖM¹‹å¤–çš„widgets,如下所½Cºï¼š(x¨¬)
     

    Control¾cȝš„两类æ–ÒŽ(gu¨©)³•åQ?/font>




    ]]>
    2.2 Programming in SWT/JFacehttp://www.aygfsteel.com/joeyjong/archive/2006/03/15/35451.htmlž®ç¬¼é¦’头ž®ç¬¼é¦’头Wed, 15 Mar 2006 09:07:00 GMThttp://www.aygfsteel.com/joeyjong/archive/2006/03/15/35451.htmlhttp://www.aygfsteel.com/joeyjong/comments/35451.htmlhttp://www.aygfsteel.com/joeyjong/archive/2006/03/15/35451.html#Feedback0http://www.aygfsteel.com/joeyjong/comments/commentRss/35451.htmlhttp://www.aygfsteel.com/joeyjong/services/trackbacks/35451.html
  • Model-based adapters
  • 与SWT widgets一起工作的JFace¾cÕd¯¿UîCØ“(f¨´)model-based adapters [或helper classes].˜q™äº›adapterå¯ä»¥åˆ†äØ“(f¨´)4¾c»ï¼š(x¨¬)
    1.Viewers
    ž®†GUI¾l„äšg中的信息与外观分¼›?[与SWT不同]
    2.Actions and contributions
    ½Ž€åŒ–了事äšg的处理,ž®†ç”¨æˆ·å‘½ä»¤çš„反应与引发反应的事äšg分离
    3.Image and font registries
    注册机制åQ?span id="BlogViewId">资源可以被按需分配和释æ”?/span>

    4.Dialogs and wizards
    信息框、错误框、进度框与向导框½{?/font>

    • SET/JFace应用½E‹åºçš„三ŒDµå¼¾l“æž„

    package com.swtjface.Ch2;

    import org.eclipse.jface.window.*;

    import org.eclipse.swt.*;

    import org.eclipse.swt.widgets.*;


    public class HelloSWT_JFace extends ApplicationWindow {

    public HelloSWT_JFace() {

    super(null); //1.Window allocation

    }

    protected Control createContents(Composite parent) {

    Text helloText = new Text(parent, SWT.CENTER);

    helloText.setText("Hello SWT and JFace!");

    parent.pack();

    return parent; //2.Window presentation

    /*处理½H—口的设è®?ç”׃ºŽApplicationWindow的可视部分不能被直接accessåQŒæ­¤æ–ÒŽ(gu¨©)³•˜qžåŒä¸€ä¸ªComposite来控制GUI的显½Cºï¼Œæ­¤container对象[Composite]是所有被加入应用½E‹åºçš„GUI¾l„äšg的父ä»?/font>*/

    }

    public static void main(String[] args) {

    HelloSWT_JFace awin = new HelloSWT_JFace();

    awin.setBlockOnOpen(true);

    awin.open();

    Display.getCurrent().dispose();  //3.Window operation

    /*è´Ÿè´£GUI的实际运作。在分派好ApplicationWindow的资源之后,mainæ–ÒŽ(gu¨©)³•使窗口显½Cºï¼Œå½“setBlockOnOpen()æ–ÒŽ(gu¨©)³•以一个true参数被调用的时候,½H—口关闭。然后ApplicationWindowçš„open()æ–ÒŽ(gu¨©)³•被调用,æ ÒŽ(gu¨©)®createContent()æ–ÒŽ(gu¨©)³•所˜q”回的Composite来显½Cºçª—口。然后程序通过dispose()æ–ÒŽ(gu¨©)³•释放GUIçš„Display实例.因䨓(f¨´)此程序中的所有widget都是displayçš„childåQŒæ‰€ä»¥ä¸€æ—¦é‡Šæ”¾DisplayåQŒæ‰€æœ‰çš„widget都被释放*/

    }

    }

    • SWT与SWT/JFace的区åˆ?/strong>

    SWTž®†GUI的外观和操作都放在它的Shell¾c»é‡ŒåQŒè€ŒSWT/JFace却将两者分¼›Õd¼€æ¥äº†åQŒå…¶ä¸­å¤–è§‚ç”±createContents()æ–ÒŽ(gu¨©)³•内的Compsite来控åˆÓž¼Œè€Œæ“ä½œéƒ¨åˆ†å¤§ä½“上是通过ApplicationWindow¾cȝš„实例来实现的ã€?/font>

    • ApplicationWindow

    SWT/JFace同样需要一个单独的Display实例åQŒä½†æ˜¯åªè¦ApplicationWindow通过一个null参数来构建,那么ž®×ƒ¼š(x¨¬)创徏它自å·Þqš„Shell。参阅下å›?br />
    ApplicationWindow在Shell的基¼‹€ä¸Šæä¾›äº†æ›´å¤šçš„途径来设计窗口,其相å…Ïx–¹æ³•如下:(x¨¬)
    addMenuBar()
    Configures the window with a top-level menu
    addToolBar()
    Adds a toolbar beneath the main menu
    addStatusLine()
    Creates a status area at the bottom of the window
    setStatus(String)
    Displays a message in the status area
    getSeparator()
    Returns the line separating the menu from the window
    setDefaultImage(Image)
    Displays an image when the application has no shell
    setExceptionHandler(IExceptionHandleråQ?/u>
    Configures the application to handle exceptions according to the specified interface



    ]]>
    2.1 Programming in SWThttp://www.aygfsteel.com/joeyjong/archive/2006/03/15/35403.htmlž®ç¬¼é¦’头ž®ç¬¼é¦’头Wed, 15 Mar 2006 05:51:00 GMThttp://www.aygfsteel.com/joeyjong/archive/2006/03/15/35403.htmlhttp://www.aygfsteel.com/joeyjong/comments/35403.htmlhttp://www.aygfsteel.com/joeyjong/archive/2006/03/15/35403.html#Feedback0http://www.aygfsteel.com/joeyjong/comments/commentRss/35403.htmlhttp://www.aygfsteel.com/joeyjong/services/trackbacks/35403.html
  • SWT应用½E‹åºçš„三ŒDµå¼¾l“æž„åQ?/strong>
  • package com.swtjface.Ch2;

    import org.eclipse.swt.*;

    import org.eclipse.swt.widgets.*;

    public class HelloSWT {

    public static void main (String [] args) {

    Display display = new Display();

    Shell shell = new Shell(display); //1.Allocation and initializationã€?br />/*生成一ä¸?font size="2">Displayå’?/font>Shell¾cȝš„实例åQ?span id="BlogViewId">GUI获取底层òq›_°çš„èµ„æºåÆˆå¼€è¾Ÿäº†ä¸€ä¸ªä¸»½H—口ã€?/

    Text helloText = new Text(shell, SWT.CENTER);

    helloText.setText("Hello SWT!");

    helloText.pack();//2.Adding widgets to the shell

    /* åœ?/font> Shell 上加入一个文本小部äšg。The code in this section also sets the parameters for these widgets, containers, and events to make sure they look and act as required.其中pack()æ–ÒŽ(gu¨©)³•是tell the Shell and Text components to use only as much space as they need.*/

    shell.pack();

    shell.open();

    while (!shell.isDisposed()){

    if (!display.readAndDispatch()) display.sleep();

    }

    display.dispose(); //3.GUI operation

    /*一旦Shellçš„open()æ–ÒŽ(gu¨©)³•被调ç”?应用½E‹åºçš„主½H—口和其子部仉™ƒ½ä¼?x¨¬)被呈现。只要Shell保持在打开状态,Display实例ž®×ƒ¼š(x¨¬)通过它的readAndDispatch()æ–ÒŽ(gu¨©)³•来追ít?span id="BlogViewId">在操作系¾lŸäº‹ä»‰™˜Ÿåˆ—中的相关用户事件。当½H—口关闭æ—Óž¼Œä¸?/font>Display对象åQˆåŒ…æ‹?/font>Shell以及(qi¨¢ng)其子部äšg½{‰ï¼‰ç›¸è”¾pȝš„资源ž®±å…¨éƒ¨é‡Šæ”?/font>*/

    }

    }

    • Display ¾c?/strong>

    Display ¾cÕdƈ不是可见的,但它负责监管着 GUI çš„èµ„æºåÆˆ½Ž¡ç†ç€å’Œæ“ä½œç³»¾lŸçš„通信ã€?span id="BlogViewId">它不光要å…Ïx³¨ç€å®ƒè‡ªå·Þqš„½H—口是如何显½Cºã€ç§»åŠ¨å’Œé‡ç”»çš„ï¼Œ˜q˜åŒæ—¶è¦¼‹®ä¿è¯¸å¦‚鼠标点击、键盘敲å‡È­‰äº‹äšg送达widgetsòq¶åŽ»å¤„ç†å®ƒä»¬ã€?/font>

    是ä“Qä½?/font> SWT å’?/font> JFace 应用½E‹åºçš„æ‰¿è½½ç€åQŒæ— è®ÞZ½ æ˜¯ç”¨ SWT/JFace 开发或是单ç”?/font> SWT 开发,你必™åÕdœ¨ä½ çš„½E‹åºä¸­åŒ…含这个类的一个实例ã€?br />
    Display ¾cȝš„主要ä»ÕdŠ¡ž®±æ˜¯è´Ÿè´£ž®†ä½ çš„代码重çš?/span> SWT å’?/span> JFace 命ä×o(h¨´)¾˜»è¯‘成底层的命ä×o(h¨´)来调取操作系¾lŸã€?/font> ˜q™ä¸€˜q‡ç¨‹åŒ…含了两部分åQ?/font> 1.Display 对象构徏一个代表着操作¾pȝ»Ÿòq›_°çš?/font> OS ¾cȝš„实例åQ›è¿™ä¸ªç±»é€šè¿‡ä¸€¾pÕdˆ—è¢«ç§°ä¹‹äØ“(f¨´)native methods的特ŒD?/font> Java ½E‹åºæä¾›äº†æŽ¥è§¦è®¡½Ž—机底层资源的途径ã€?. ˜q™ä¸ª Display 对象使用˜q™äº›æ–ÒŽ(gu¨©)³•来直接指令操作系¾lŸåƈ向应用程序传辄¡”¨æˆ·åŠ¨ä½œã€?br />
    if any features in your operating system aren’t incorporated into SWT, you can use the Java Native Interface to add them yourself.All it requires is a native Java method in the SWT package and a C function in the native graphics library that calls the operating system.

    • Display ¾cÈš„æ–ÒŽ(gu¨©)³•

    1.Display()--Allocates platform resources and creates a Display object
    must be used in any SWT-based GUI.它äñ”生一个Display¾cȝš„实例òq¶å°†å…¶å’ŒGUI相联¾p?/font>
    2.getCurrent()--Returns the user-interface thread
    must be used in any SWT-based GUI.它返回一个应用程序的ä¸Èº¿½E‹ï¼Œç”¨æˆ·ç•Œé¢¾U¿ç¨‹,通常和dispose()一起ä‹É用来¾l“束Display的操作ã€?/font>
    3.readAndDispatch()--Display object interprets events and passes them to receiver
    enable the application to receive notifications from the operating system whenever the user takes an action associated with the GUI.
    accesses the operating system’s event queue and determines whether any of the user’s actions are related to the GUI.
    Using this method, the HelloSWT class knows whether the user has decided to dispose of the Shell. If so, the method returns TRUE, and the application ends. Otherwise, the Display object invokes its sleep() method, and the application continues waiting.
    4.sleep()--Display object waits for events
    enable the application to receive notifications from the operating system whenever the user takes an action associated with the GUI.

  • Shell ¾c?/strong>
  • The Shell class accesses the operating system through the OS class to an extent, but only to keep track of opening, activating, maximizing, minimizing, and closing the main window.

    The main function of the Shell class is to provide a common connection point for the containers, widgets, and events that need to be integrated into the GUI. Shell serves as the parent class to these components.

    attached to the Display的shell--top-level shells
    NOT directly attached to the Display instance的shell--secondary shell

    在你çš?/font>GUI之内åQŒä½ å¯ä»¥è®‘Ö®šshell或其他小部äšg的风格参数å€û|¼Œè‹¥æ˜¯å¤šä¸ªå€¼åˆ™å¯ä»¥ç”¨â€?/font>|â€?/font>相连.除了提到的属性,默认为â€?span id="BlogViewId">SHELL_TRIM”ã€?br />
    SHELL_TRIM--有一个标题栏(SWT.TITLE)和用户可以最ž®åŒ–SWT.MIN)、最大化(SWT.MAX)、改变尺å¯?/font>(SWT.RESIZE)和关é—?/font>(SWT.CLOSE)
    DIALOG_TRIM--有一个标题栏、一个活动区的边�/font>(SWT.BORDER)和被关闭的能�/font>

    你还可以¼‹®å®š
    shellçš„åŞ态,以限定用户修æ”?/font>shellçš„modality,如A modal dialog box不能被移动或是改变尺寸,只可以ä‹É用给予的按钮关闭或是取消ã€?/font>


    NOT every platform can render these properties in GUI components.



    ]]>
    Ö÷Õ¾Ö©Öë³ØÄ£°å£º ÒÊË®ÏØ| ÄÚ»ÆÏØ| ÉÐÖ¾ÊÐ| ºÏ´¨ÊÐ| ÁÚË®| ÉÌÇðÊÐ| ÓåÖÐÇø| Õê·áÏØ| º×ÇìÏØ| ³¤´ºÊÐ| ÇàÖÝÊÐ| ¾©É½ÏØ| »³»¯ÊÐ| Ò˲ýÊÐ| ÎèÑôÏØ| ¾Ą̊ÊÐ| ÎÚÀ­ÌØÖÐÆì| ÉäºéÏØ| ºÎ÷ÊÐ| Èç¸ÞÊÐ| ºáÉ½ÏØ| ×ÓÖÞÏØ| Âé½­ÏØ| ÆîÃÅÏØ| ÐÂÖñÊÐ| ÀÖÒµÏØ| ÌÁ¹ÁÇø| ÂíÉ½ÏØ| ²×Ô´| µÂÇåÏØ| Ìì½òÊÐ| ×Ô¹±ÊÐ| Çì³ÇÏØ| ÁÉÔ´ÊÐ| Çຣʡ| ¾²ÀÖÏØ| ¶õÍÐ¿ËÆì| èµÉ½ÏØ| Îâ±¤ÏØ| ¹óÑôÊÐ| ¶«³ÇÇø|