当一个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>
除了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
å¦‚æžœä½ ä¸æƒ³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.
两个é‡è¦çš„æŽ¥å£ï¼š(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 />
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.
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());
}
}
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是抽象类�/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);
}
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;
}
}
两个途径æ¥å°†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 />
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
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--åªå¯¹æŸä¸€¾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是ç‘ô承了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.");
}
ä»Ö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.
更絋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¨¬)
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 /> Â
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>
å¯ä»¥åœ¨å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.
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
Button¾cÈš„é»˜è®¤é£Žæ ¼ã€?br />SWT.FLAT
与SWT.UP, SWT.DOWN, SWT.LEFT, and SWT.RIGHT¾l„åˆåQŒä¹Ÿå¯ä‹É用SWT.FLAT
¾cÖM¼¼äºŽSWT.PUSHåQŒä½†æ˜¯æŒ‰ä¸‹åŽä¼?x¨¬)ä¿æŒæŒ‰ä¸‹çжæ€ã€‚å¯é€šè¿‡setSelection(boolean)æ–ÒŽ(gu¨©)³•æ¥æ”¹å˜å…¶çжæ€ã€‚接下去介ç»çš„check按钮和radio按钮也share˜q™ä¸ªåŠŸèƒ½
廸™®®ç”¨æ•°¾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();
廸™®®ç”¨æ•°¾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>
寚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.
是所有widget的父¾c…R€‚䨓(f¨´)抽象¾c»ï¼Œè€Œä¸”Eclipse.org也强烈å对去¾l§æ‰¿å®ƒï¼Œå› æ¤ä½ æ—¢ä¸èƒ½¾l§æ‰¿ä»–,也ä¸èƒ½ç›´æŽ¥ä‹É用它
Widget¾cÖM¸çš„é‡è¦æ–¹æ³•,如下åQ?/font>
Control¾cÖM¸çš„对象在OS䏿œ‰ä¸€ä¸ªç›´æŽ¥çš„副本åQŒä½ å¯ä»¥é€šè¿‡¾cÈš„handle fieldæ¥ç›´æŽ¥access.然而SWT˜q˜æä¾›äº†ä¸€äº›åœ¨Control¾cÖM¹‹å¤–çš„widgets,如下所½Cºï¼š(x¨¬)
Â
Control¾cÈš„两类æ–ÒŽ(gu¨©)³•åQ?/font>
与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>
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ž®†GUI的外观和æ“作都放在它的Shell¾c»é‡ŒåQŒè€ŒSWT/JFaceå´å°†ä¸¤è€…分¼›Õd¼€æ¥äº†åQŒå…¶ä¸å¤–è§‚ç”±createContents()æ–ÒŽ(gu¨©)³•内的Compsiteæ¥æŽ§åˆÓž¼Œè€Œæ“作部分大体上是通过ApplicationWindow¾cÈš„实例æ¥å®žçŽ°çš„ã€?/font>
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
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Õ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.
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.
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.