上面我们介绍了,树和表格,还有选项卡。这次我们来介绍下:link,List,Combo,Browser。
1.link
Styles:BORDER.
Events:Selection,其他也都是Control 默认能出发的event
例子:
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Link link = new Link(shell, SWT.NONE);
String text = "The SWT component is designed to provide <a>efficient</a>, <a>portable</a> <a href=\"native\">access to the user-interface facilities of the operating systems</a> on which it is implemented.";
link.setText(text);
// link.setSize(60, 400);
link.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
System.out.println("Selection: " + event.text);
}
});
shell.setSize(400,400);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
效果图:
2.List:
Styles:SINGLE,MULTI,H_SCROLL,V_SCROLL,BORDER.
Events:Selection,其他也都是Control 默认能出发的event.
列子:
public void show_List(Composite parent) {
String[] testArr = new String[] { "1", "2", "3", "4", "5" };
List sList = new List(parent, SWT.BORDER | SWT.SINGLE);
sList.setItems(testArr);
List mList = new List(parent, SWT.BORDER | SWT.MULTI);
mList.setItems(testArr);
List scList = new List(parent, SWT.BORDER | SWT.SINGLE | SWT.V_SCROLL
| SWT.H_SCROLL);
scList.setItems(testArr);
List smList = new List(parent, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL
| SWT.H_SCROLL);
smList.setItems(testArr);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout(SWT.VERTICAL));
(new TestList()).show_List(shell);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
效果图:
3.Combo:
Styles:DROP_DOWN,SIMPLE,READ_ONLY.
Events:ModifyListener,SelectionListener,VerifyListener,其他也都是Control 默认能出发的event。
例子:
String[] WEEK = { "Monday", "Tuesday", "Wednesday"};
Display display = new Display();
Shell shell = new Shell(display);
shell.setBounds(500, 100, 500, 300);
shell.setText("Combo");
shell.setLayout(new GridLayout(3, true));
//´´½¨Combo×é¼þ£¬ÎªÏÂÀÁбíÑùʽ
final Combo dc = new Combo(shell, SWT.DROP_DOWN);
dc.setItems(WEEK);
dc.addSelectionListener(new SelectionAdapter(){
@Override
public void widgetSelected(SelectionEvent e) {
String key = ""+dc.getSelectionIndex();
String value = dc.getText();
System.out.println("key:"+key+" value:"+value);
}
});
//´´½¨Combo×é¼þ£¬ÎªÏÂÀÁбíÑùʽ£¬ÇÒÖ»¶Á
final Combo rc = new Combo(shell, SWT.DROP_DOWN | SWT.READ_ONLY);
String[] items={"scorm 2004","scorm 1.2"};
rc.setItems(items);
rc.select(0);
rc.setData("0","scorm2004");
rc.setData("1","scorm1.2");
rc.addSelectionListener(new SelectionAdapter(){
@Override
public void widgetSelected(SelectionEvent e) {
String key = ""+rc.getSelectionIndex();
System.out.println("key:"+key);
String value = (String) rc.getData(key);
System.out.println("key:"+key+" value:"+value);
}
});
//rc.setItems(MONTHS);
//´´½¨Combo×é¼þ£¬ÎªList×é¼þÑùʽ
Combo sc = new Combo(shell, SWT.SIMPLE);
sc.setItems(WEEK);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
效果图:
4.Browser:
Styles:MOZILLA,WEBKIT,BORDER.
Events:AuthenticationListener(认证监听),CloseWindowListener,LocationListener,OpenWindowListener,ProgressListener,StatusTextListener,TitleListener,VisibilityWindowListener,其他也都是Control 默认能出发的event
例子:
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Text;
public class TestBrowse {
//¶¨Òåä¯ÀÀÆ÷µÄ±êÌâ
public static final String APP_TITLE = "Simple SWT Browser";
//¶¨ÒåÖ÷Ò³µÄurl
public static final String HOME_URL = "http://www.eclipse.org/vep/";
//ÉùÃ÷Ö÷´°¿ÚºÍÆäËü¿Ø¼þ
private org.eclipse.swt.widgets.Shell sShell = null;
private Button backButton = null;//ºóÍ˰´Å¥
private Button forwardButton = null;//ǰ½ø°´Å¥
private Button stopButton = null;//Í£Ö¹°´Å¥
private Text locationText = null;//ÏÔʾurlµÄÎı¾¿ò
private Button goButton = null;//תÏò°´Å¥
private Browser browser = null;//ä¯ÀÀÆ÷¶ÔÏó
private Button homeButton = null;//Ö÷Ò³°´Å¥
private Label statusText = null;//ÏÔʾä¯ÀÀÆ÷״̬µÄÎı¾¿ò
private ProgressBar progressBar = null;//×°ÔØÒ³ÃæÊ±µÄ½ø¶ÈÌõ
private Button refreshButton = null;//ˢа´Å¥
//³õʼ»¯ä¯ÀÀÆ÷
private void createBrowser() {
org.eclipse.swt.layout.GridData gridData3 = new org.eclipse.swt.layout.GridData();
//´´½¨ä¯ÀÀÆ÷¶ÔÏó
browser = new Browser(sShell, SWT.BORDER);
gridData3.horizontalSpan = 7;
gridData3.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
gridData3.verticalAlignment = org.eclipse.swt.layout.GridData.FILL;
gridData3.grabExcessVerticalSpace = true;
//ÉèÖÃä¯ÀÀÆ÷²¼¾Ö
browser.setLayoutData(gridData3);
//Ϊä¯ÀÀÆ÷×¢²á±êÌâ¸Ä±äʼþ
browser.addTitleListener(new org.eclipse.swt.browser.TitleListener() {
public void changed(org.eclipse.swt.browser.TitleEvent e) {
sShell.setText(APP_TITLE + " - " + e.title);
}
});
//Ϊä¯ÀÀÆ÷×¢²áµØÖ·¸Ä±äʼþ
browser.addLocationListener(new org.eclipse.swt.browser.LocationListener() {
public void changing(org.eclipse.swt.browser.LocationEvent e) {
locationText.setText(e.location);
}
public void changed(org.eclipse.swt.browser.LocationEvent e) {
}
});
//Ϊä¯ÀÀÆ÷×¢²á×°ÔØÍøÒ³Ê¼þ
browser.addProgressListener(new org.eclipse.swt.browser.ProgressListener() {
//µ±×°ÔØÊ±£¬ÉèÖÃ×°ÔØµÄ½ø¶È£¬²¢ÇÒÉèÖÃÍ£Ö¹°´Å¥¿ÉÓÃ
public void changed(org.eclipse.swt.browser.ProgressEvent e) {
if (!stopButton.isEnabled() && e.total != e.current) {
stopButton.setEnabled(true);
}
progressBar.setMaximum(e.total);
progressBar.setSelection(e.current);
}
//×°ÔØÍê³ÉºóÉèÖÃÍ£Ö¹°´Å¥£¬ºóÍ˰´Å¥£¬Ç°½ø°´Å¥ºÍ½ø¶ÈÌõµÄ״̬
public void completed(org.eclipse.swt.browser.ProgressEvent e) {
stopButton.setEnabled(false);
backButton.setEnabled(browser.isBackEnabled());
forwardButton.setEnabled(browser.isForwardEnabled());
progressBar.setSelection(0);
}
});
//×¢²áä¯ÀÀÆ÷״̬¸Ä±äʼþ
browser.addStatusTextListener(new org.eclipse.swt.browser.StatusTextListener() {
public void changed(org.eclipse.swt.browser.StatusTextEvent e) {
statusText.setText(e.text);
}
});
//³õʼ״̬´ò¿ªÖ÷Ò³µÄurl
browser.setUrl(HOME_URL);
}
public static void main(String[] args) {
org.eclipse.swt.widgets.Display display = org.eclipse.swt.widgets.Display.getDefault();
TestBrowse thisClass = new TestBrowse();
thisClass.createSShell();
thisClass.sShell.open();
while (!thisClass.sShell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
//´´½¨´°¿ÚºÍ´°¿ÚµÄ¿Ø¼þ
private void createSShell() {
sShell = new org.eclipse.swt.widgets.Shell();
org.eclipse.swt.layout.GridLayout gridLayout1 = new GridLayout();
org.eclipse.swt.layout.GridData gridData2 = new org.eclipse.swt.layout.GridData();
org.eclipse.swt.layout.GridData gridData4 = new org.eclipse.swt.layout.GridData();
org.eclipse.swt.layout.GridData gridData5 = new org.eclipse.swt.layout.GridData();
org.eclipse.swt.layout.GridData gridData6 = new org.eclipse.swt.layout.GridData();
org.eclipse.swt.layout.GridData gridData7 = new org.eclipse.swt.layout.GridData();
org.eclipse.swt.layout.GridData gridData8 = new org.eclipse.swt.layout.GridData();
backButton = new Button(sShell, SWT.ARROW | SWT.LEFT);
forwardButton = new Button(sShell, SWT.ARROW | SWT.RIGHT);
stopButton = new Button(sShell, SWT.NONE);
refreshButton = new Button(sShell, SWT.NONE);
homeButton = new Button(sShell, SWT.NONE);
locationText = new Text(sShell, SWT.BORDER);
goButton = new Button(sShell, SWT.NONE);
createBrowser();
progressBar = new ProgressBar(sShell, SWT.BORDER);
statusText = new Label(sShell, SWT.NONE);
sShell.setText(APP_TITLE);
sShell.setLayout(gridLayout1);
gridLayout1.numColumns = 7;
backButton.setEnabled(false);
backButton.setToolTipText("Navigate back to the previous page");
backButton.setLayoutData(gridData6);
forwardButton.setEnabled(false);
forwardButton.setToolTipText("Navigate forward to the next page");
forwardButton.setLayoutData(gridData5);
stopButton.setText("Stop");
stopButton.setEnabled(false);
stopButton.setToolTipText("Stop the loading of the current page");
goButton.setText("Go!");
goButton.setLayoutData(gridData8);
goButton.setToolTipText("Navigate to the selected web address");
gridData2.grabExcessHorizontalSpace = true;
gridData2.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
gridData2.verticalAlignment = org.eclipse.swt.layout.GridData.CENTER;
locationText.setLayoutData(gridData2);
locationText.setText(HOME_URL);
locationText.setToolTipText("Enter a web address");
homeButton.setText("Home");
homeButton.setToolTipText("Return to home page");
statusText.setText("Done");
statusText.setLayoutData(gridData7);
gridData4.horizontalSpan = 5;
progressBar.setLayoutData(gridData4);
progressBar.setEnabled(false);
progressBar.setSelection(0);
gridData5.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
gridData5.verticalAlignment = org.eclipse.swt.layout.GridData.FILL;
gridData6.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
gridData6.verticalAlignment = org.eclipse.swt.layout.GridData.FILL;
gridData7.horizontalSpan = 1;
gridData7.grabExcessHorizontalSpace = true;
gridData7.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
gridData7.verticalAlignment = org.eclipse.swt.layout.GridData.CENTER;
gridData8.horizontalAlignment = org.eclipse.swt.layout.GridData.END;
gridData8.verticalAlignment = org.eclipse.swt.layout.GridData.CENTER;
refreshButton.setText("Refresh");
refreshButton.setToolTipText("Refresh the current page");
sShell.setSize(new org.eclipse.swt.graphics.Point(553, 367));
//×¢²áÏÔʾµØÖ·µÄÎı¾¿òʼþ
locationText.addMouseListener(new org.eclipse.swt.events.MouseAdapter() {
public void mouseUp(org.eclipse.swt.events.MouseEvent e) {
locationText.selectAll();
}
});
locationText.addKeyListener(new org.eclipse.swt.events.KeyAdapter() {
public void keyPressed(org.eclipse.swt.events.KeyEvent e) {
// Handle the press of the Enter key in the locationText.
// This will browse to the entered text.
if (e.character == SWT.LF || e.character == SWT.CR) {
e.doit = false;
browser.setUrl(locationText.getText());
}
}
});
refreshButton.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
browser.refresh();//ÖØÐÂÔØÈë
}
});
locationText.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
browser.setUrl(locationText.getText());//ÉèÖÃä¯ÀÀÆ÷µÄÖ¸ÏòµÄurl
}
});
stopButton.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
browser.stop();//Í£Ö¹×°ÔØÍøÒ³
}
});
backButton.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
browser.back();//ºóÍË
}
});
forwardButton.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
browser.forward();//ǰ½ø
}
});
homeButton.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
browser.setUrl(HOME_URL);//ÉèÖÃÖ÷Ò³
}
});
goButton.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
browser.setUrl(locationText.getText());//תÏòµØÖ·µÄÍøÒ³
}
});
}
效果图:
参考资料:
http://www.eclipse.org/swt/widgets/