swt入门 --常用组件的使用
swt的常用组件button ,text ,combo,list ,还有一些容器类composite ,group,这里选择几个,列写简单的用法不写解释了,因为代码很简单,而且代码上的注释足以说明.
1,combo 和text
package
com.test;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class CTShow {
private static List list;
private static Combo combo;
private static Text text;
public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setSize(478, 120);
shell.setText("combo and text");
shell.open();
//构造text
text = new Text(shell, SWT.BORDER);
text.setBounds(160, 34, 281, 20);
//构造combo
combo = new Combo(shell, SWT.NONE);
combo.setItems(new String[] { "one", "two", "three", "four"});
combo.setData("one", "1");
combo.setData("two", "2");
combo.setData("three", "3");
combo.setData("four", "4");
combo.setBounds(28, 34, 102, 20);
//添加监听器
combo.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e){
String selText = combo.getText();
String selValue = (String)combo.getData(selText);
text.setText("text: " + selText + " value: " + selValue);
}});
shell.layout();
//消息循环
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
}
2,list、 button 、messagebox
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class CTShow {
private static List list;
private static Combo combo;
private static Text text;
public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setSize(478, 120);
shell.setText("combo and text");
shell.open();
//构造text
text = new Text(shell, SWT.BORDER);
text.setBounds(160, 34, 281, 20);
//构造combo
combo = new Combo(shell, SWT.NONE);
combo.setItems(new String[] { "one", "two", "three", "four"});
combo.setData("one", "1");
combo.setData("two", "2");
combo.setData("three", "3");
combo.setData("four", "4");
combo.setBounds(28, 34, 102, 20);
//添加监听器
combo.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e){
String selText = combo.getText();
String selValue = (String)combo.getData(selText);
text.setText("text: " + selText + " value: " + selValue);
}});
shell.layout();
//消息循环
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
}
package
com.test;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
public class LBMShow {
private static List list;
/** *//**
* Launch the application
* @param args
*/
public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setSize(192, 217);
shell.setText("list and button and messagebox");
//
shell.open();
//构造list
list = new List(shell, SWT.BORDER|SWT.MULTI|SWT.V_SCROLL);
list.setItems(new String[] { "语文", "数学", "英语", "物理", "化学"});
list.setData("化学", "HX");
list.setData("物理", "WL");
list.setData("语文", "YW");
list.setData("数学", "SX");
list.setData("英语", "YY");
list.setBounds(29, 25, 100, 75);
//构造button
final Button button = new Button(shell, SWT.NONE);
button.setText("提示");
button.setBounds(48, 121, 58, 22);
//添加监听器
button.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e){
for (String str : list.getSelection()) {
//messagebox提示
MessageDialog.openInformation(null, null, str);
}
}
});
shell.layout();
//消息循环
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
}
3,group组
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
public class LBMShow {
private static List list;
/** *//**
* Launch the application
* @param args
*/
public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setSize(192, 217);
shell.setText("list and button and messagebox");
//
shell.open();
//构造list
list = new List(shell, SWT.BORDER|SWT.MULTI|SWT.V_SCROLL);
list.setItems(new String[] { "语文", "数学", "英语", "物理", "化学"});
list.setData("化学", "HX");
list.setData("物理", "WL");
list.setData("语文", "YW");
list.setData("数学", "SX");
list.setData("英语", "YY");
list.setBounds(29, 25, 100, 75);
//构造button
final Button button = new Button(shell, SWT.NONE);
button.setText("提示");
button.setBounds(48, 121, 58, 22);
//添加监听器
button.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e){
for (String str : list.getSelection()) {
//messagebox提示
MessageDialog.openInformation(null, null, str);
}
}
});
shell.layout();
//消息循环
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
}
package
com.test;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class GroupShow {
private static Text text;
public static void main(String[] args) {
final Display display = Display.getDefault();
Shell shell = new Shell();
shell.setText("group");
shell.open();
//构造group
final Group group = new Group(shell, SWT.NONE);
group.setText(" 组 ");
group.setBounds(41, 28, 143, 100);
//把按钮放在group中,注意new Button(group这里
final Button button = new Button(group, SWT.NONE);
button.setText("按钮");
button.setBounds(42, 59, 48, 22);
//构造text
text = new Text(group, SWT.BORDER);
text.setBounds(32, 28, 80, 25);
//
shell.layout();
while(!shell.isDisposed()){
if (!display.readAndDispatch()){
display.sleep();
}
}
}
}
====================================================
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class GroupShow {
private static Text text;
public static void main(String[] args) {
final Display display = Display.getDefault();
Shell shell = new Shell();
shell.setText("group");
shell.open();
//构造group
final Group group = new Group(shell, SWT.NONE);
group.setText(" 组 ");
group.setBounds(41, 28, 143, 100);
//把按钮放在group中,注意new Button(group这里
final Button button = new Button(group, SWT.NONE);
button.setText("按钮");
button.setBounds(42, 59, 48, 22);
//构造text
text = new Text(group, SWT.BORDER);
text.setBounds(32, 28, 80, 25);
//
shell.layout();
while(!shell.isDisposed()){
if (!display.readAndDispatch()){
display.sleep();
}
}
}
}
Swt/Jface tableViewer入门教程一(显示tableViewer)
1,简单显示,表格的式样见注释中的内容
import
org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
public class TestTableViewer {
private static Table table;
/** *//**
* Launch the application
* @param args
*/
public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setSize(500, 375);
shell.setText("SWT Application");
//注意这里,SWT.NULTI代表可以选择多行,SWT.FULL_SELECTION代表可以整行选择,SWT.BORDER边框,SWT.V_SCROLL ,SWT.H_SCROLL滚动条
final TableViewer tableViewer = new TableViewer(shell, SWT.MULTI | SWT.FULL_SELECTION | SWT.BORDER|SWT.V_SCROLL|SWT.H_SCROLL);
table = tableViewer.getTable();
table.setLinesVisible(true);
table.setHeaderVisible(true);
table.setBounds(97, 79, 373, 154);
final TableColumn newColumnTableColumn = new TableColumn(table, SWT.NONE);
newColumnTableColumn.setWidth(39);
newColumnTableColumn.setText("ID");
final TableColumn newColumnTableColumn_1 = new TableColumn(table, SWT.NONE);
newColumnTableColumn_1.setWidth(85);
newColumnTableColumn_1.setText("姓名");
final TableColumn newColumnTableColumn_2 = new TableColumn(table, SWT.NONE);
newColumnTableColumn_2.setWidth(41);
newColumnTableColumn_2.setText("性别");
final TableColumn newColumnTableColumn_3 = new TableColumn(table, SWT.NONE);
newColumnTableColumn_3.setWidth(43);
newColumnTableColumn_3.setText("年龄");
final TableColumn newColumnTableColumn_4 = new TableColumn(table, SWT.NONE);
newColumnTableColumn_4.setWidth(126);
newColumnTableColumn_4.setText("创建日期");
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
}
2,加入布局
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
public class TestTableViewer {
private static Table table;
/** *//**
* Launch the application
* @param args
*/
public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setSize(500, 375);
shell.setText("SWT Application");
//注意这里,SWT.NULTI代表可以选择多行,SWT.FULL_SELECTION代表可以整行选择,SWT.BORDER边框,SWT.V_SCROLL ,SWT.H_SCROLL滚动条
final TableViewer tableViewer = new TableViewer(shell, SWT.MULTI | SWT.FULL_SELECTION | SWT.BORDER|SWT.V_SCROLL|SWT.H_SCROLL);
table = tableViewer.getTable();
table.setLinesVisible(true);
table.setHeaderVisible(true);
table.setBounds(97, 79, 373, 154);
final TableColumn newColumnTableColumn = new TableColumn(table, SWT.NONE);
newColumnTableColumn.setWidth(39);
newColumnTableColumn.setText("ID");
final TableColumn newColumnTableColumn_1 = new TableColumn(table, SWT.NONE);
newColumnTableColumn_1.setWidth(85);
newColumnTableColumn_1.setText("姓名");
final TableColumn newColumnTableColumn_2 = new TableColumn(table, SWT.NONE);
newColumnTableColumn_2.setWidth(41);
newColumnTableColumn_2.setText("性别");
final TableColumn newColumnTableColumn_3 = new TableColumn(table, SWT.NONE);
newColumnTableColumn_3.setWidth(43);
newColumnTableColumn_3.setText("年龄");
final TableColumn newColumnTableColumn_4 = new TableColumn(table, SWT.NONE);
newColumnTableColumn_4.setWidth(126);
newColumnTableColumn_4.setText("创建日期");
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
}
显示的效果不好,我们要加入布局,让view填充整个画面在shell.open()后加上
ok,效果达到
3,为TableViewer加上数据,光突突的样子不好看。这里是demo所以不再从数据库里边取数据了。自己构造一个List用来做数据。实际使用中也是这么使用,不过不同的是这里是随意构造的list,而显示实际使用中是从数据库取出数据构造list.
(1)构造一个people类,同时给People类提供一个构造假数据的方法getPeople()
import
java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class People {
private Long id;
private String name;
private String sex;
private Integer age;
private Date createDate;
public People(Long id,String name,String sex,Integer age,Date createDate){
this.id = id;
this.name = name;
this.sex = sex;
this.age = age;
this.createDate = createDate;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
import java.util.Date;
import java.util.List;
public class People {
private Long id;
private String name;
private String sex;
private Integer age;
private Date createDate;
public People(Long id,String name,String sex,Integer age,Date createDate){
this.id = id;
this.name = name;
this.sex = sex;
this.age = age;
this.createDate = createDate;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}