当前位置:   article > 正文

swt入门---http://www.blogjava.net/dreamstone/archive/2007/08/02/134074.html

sex tokyo

swt入门 --常用组件的使用

swt的常用组件button ,text ,combo,list ,还有一些容器类composite ,group,这里选择几个,列写简单的用法
不写解释了,因为代码很简单,而且代码上的注释足以说明.
1,combo  和text
None.gif package  com.test;
None.gif
None.gif
import  org.eclipse.swt.SWT;
None.gif
import  org.eclipse.swt.events.SelectionAdapter;
None.gif
import  org.eclipse.swt.events.SelectionEvent;
None.gif
import  org.eclipse.swt.widgets.Combo;
None.gif
import  org.eclipse.swt.widgets.Display;
None.gif
import  org.eclipse.swt.widgets.List;
None.gif
import  org.eclipse.swt.widgets.Shell;
None.gif
import  org.eclipse.swt.widgets.Text;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   class  CTShow  dot.gif {
InBlock.gif
InBlock.gif    
private static List list;
InBlock.gif    
private static Combo combo;
InBlock.gif    
private static Text text;
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static void main(String[] args) dot.gif{
InBlock.gif        
final Display display = Display.getDefault();
InBlock.gif        
final Shell shell = new Shell();
InBlock.gif        shell.setSize(
478120);
InBlock.gif        shell.setText(
"combo and text");
InBlock.gif        shell.open();
InBlock.gif        
//构造text
InBlock.gif
        text = new Text(shell, SWT.BORDER);
InBlock.gif        text.setBounds(
1603428120);
InBlock.gif        
//构造combo
InBlock.gif
        combo = new Combo(shell, SWT.NONE);
ExpandedSubBlockStart.gifContractedSubBlock.gif        combo.setItems(
new String[] dot.gif{ "one""two""three""four"});
InBlock.gif        combo.setData(
"one""1");
InBlock.gif        combo.setData(
"two""2");
InBlock.gif        combo.setData(
"three""3");
InBlock.gif        combo.setData(
"four""4");
InBlock.gif        combo.setBounds(
283410220);
InBlock.gif        
//添加监听器
ExpandedSubBlockStart.gifContractedSubBlock.gif
        combo.addSelectionListener(new SelectionAdapter()dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
public void widgetSelected(SelectionEvent e)dot.gif{
InBlock.gif                String selText 
= combo.getText();
InBlock.gif                String selValue 
= (String)combo.getData(selText);
InBlock.gif                text.setText(
"text: " + selText + " value: " + selValue);
ExpandedSubBlockEnd.gif            }
}
);
InBlock.gif        
InBlock.gif        shell.layout();
InBlock.gif        
//消息循环
ExpandedSubBlockStart.gifContractedSubBlock.gif
        while (!shell.isDisposed()) dot.gif{
InBlock.gif            
if (!display.readAndDispatch())
InBlock.gif                display.sleep();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif
2,list、 button 、messagebox
None.gif package  com.test;
None.gif
None.gif
import  org.eclipse.jface.dialogs.MessageDialog;
None.gif
import  org.eclipse.swt.SWT;
None.gif
import  org.eclipse.swt.events.SelectionAdapter;
None.gif
import  org.eclipse.swt.events.SelectionEvent;
None.gif
import  org.eclipse.swt.widgets.Button;
None.gif
import  org.eclipse.swt.widgets.Display;
None.gif
import  org.eclipse.swt.widgets.List;
None.gif
import  org.eclipse.swt.widgets.Shell;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   class  LBMShow  dot.gif {
InBlock.gif
InBlock.gif    
private static List list;
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
InBlock.gif     * Launch the application
InBlock.gif     * 
@param args
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static void main(String[] args) dot.gif{
InBlock.gif        
final Display display = Display.getDefault();
InBlock.gif        
final Shell shell = new Shell();
InBlock.gif        shell.setSize(
192217);
InBlock.gif        shell.setText(
"list and button and messagebox");
InBlock.gif        
//
InBlock.gif

InBlock.gif        shell.open();
InBlock.gif        
//构造list
InBlock.gif
        list = new List(shell, SWT.BORDER|SWT.MULTI|SWT.V_SCROLL);
ExpandedSubBlockStart.gifContractedSubBlock.gif        list.setItems(
new String[] dot.gif{ "语文""数学""英语""物理""化学"});
InBlock.gif        list.setData(
"化学""HX");
InBlock.gif        list.setData(
"物理""WL");
InBlock.gif        list.setData(
"语文""YW");
InBlock.gif        list.setData(
"数学""SX");
InBlock.gif        list.setData(
"英语""YY");
InBlock.gif        list.setBounds(
292510075);
InBlock.gif        
//构造button
InBlock.gif
        final Button button = new Button(shell, SWT.NONE);
InBlock.gif        button.setText(
"提示");
InBlock.gif        button.setBounds(
481215822);
InBlock.gif        
//添加监听器
ExpandedSubBlockStart.gifContractedSubBlock.gif
        button.addSelectionListener(new SelectionAdapter()dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
public void widgetSelected(SelectionEvent e)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
for (String str : list.getSelection()) dot.gif{
InBlock.gif                    
//messagebox提示
InBlock.gif
                    MessageDialog.openInformation(nullnull, str);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }
);
InBlock.gif        
InBlock.gif        shell.layout();
InBlock.gif        
//消息循环
ExpandedSubBlockStart.gifContractedSubBlock.gif
        while (!shell.isDisposed()) dot.gif{
InBlock.gif            
if (!display.readAndDispatch())
InBlock.gif                display.sleep();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif
3,group组
None.gif package  com.test;
None.gif
None.gif
import  org.eclipse.swt.SWT;
None.gif
import  org.eclipse.swt.widgets.Button;
None.gif
import  org.eclipse.swt.widgets.Display;
None.gif
import  org.eclipse.swt.widgets.Group;
None.gif
import  org.eclipse.swt.widgets.Shell;
None.gif
import  org.eclipse.swt.widgets.Text;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   class  GroupShow  dot.gif {
InBlock.gif    
private static Text text;
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static void main(String[] args) dot.gif{
InBlock.gif        
final Display display = Display.getDefault();
InBlock.gif        Shell shell 
= new Shell();
InBlock.gif        shell.setText(
"group");
InBlock.gif        shell.open();
InBlock.gif        
//构造group
InBlock.gif
        final Group group = new Group(shell, SWT.NONE);
InBlock.gif        group.setText(
"   组  ");
InBlock.gif        group.setBounds(
4128143100);
InBlock.gif        
//把按钮放在group中,注意new Button(group这里
InBlock.gif
        final Button button = new Button(group, SWT.NONE);
InBlock.gif        button.setText(
"按钮");
InBlock.gif        button.setBounds(
42594822);
InBlock.gif        
//构造text
InBlock.gif
        text = new Text(group, SWT.BORDER);
InBlock.gif        text.setBounds(
32288025);
InBlock.gif        
//
InBlock.gif
        shell.layout();
ExpandedSubBlockStart.gifContractedSubBlock.gif        
while(!shell.isDisposed())dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if (!display.readAndDispatch())dot.gif{
InBlock.gif                display.sleep();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

====================================================

Swt/Jface tableViewer入门教程一(显示tableViewer)

1,简单显示,表格的式样见注释中的内容
None.gif import  org.eclipse.jface.viewers.TableViewer;
None.gif
import  org.eclipse.swt.SWT;
None.gif
import  org.eclipse.swt.widgets.Display;
None.gif
import  org.eclipse.swt.widgets.Shell;
None.gif
import  org.eclipse.swt.widgets.Table;
None.gif
import  org.eclipse.swt.widgets.TableColumn;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   class  TestTableViewer  dot.gif {
InBlock.gif    
private static Table table;
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
InBlock.gif     * Launch the application
InBlock.gif     * 
@param args
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static void main(String[] args) dot.gif{
InBlock.gif        
final Display display = Display.getDefault();
InBlock.gif        
final Shell shell = new Shell();
InBlock.gif        shell.setSize(
500375);
InBlock.gif        shell.setText(
"SWT Application");
InBlock.gif        
//注意这里,SWT.NULTI代表可以选择多行,SWT.FULL_SELECTION代表可以整行选择,SWT.BORDER边框,SWT.V_SCROLL ,SWT.H_SCROLL滚动条
InBlock.gif
        final TableViewer tableViewer = new TableViewer(shell, SWT.MULTI | SWT.FULL_SELECTION | SWT.BORDER|SWT.V_SCROLL|SWT.H_SCROLL);
InBlock.gif        
InBlock.gif        table 
= tableViewer.getTable();
InBlock.gif        table.setLinesVisible(
true);
InBlock.gif        table.setHeaderVisible(
true);
InBlock.gif        table.setBounds(
9779373154);
InBlock.gif
InBlock.gif        
final TableColumn newColumnTableColumn = new TableColumn(table, SWT.NONE);
InBlock.gif        newColumnTableColumn.setWidth(
39);
InBlock.gif        newColumnTableColumn.setText(
"ID");
InBlock.gif
InBlock.gif        
final TableColumn newColumnTableColumn_1 = new TableColumn(table, SWT.NONE);
InBlock.gif        newColumnTableColumn_1.setWidth(
85);
InBlock.gif        newColumnTableColumn_1.setText(
"姓名");
InBlock.gif        
InBlock.gif        
final TableColumn newColumnTableColumn_2 = new TableColumn(table, SWT.NONE);
InBlock.gif        newColumnTableColumn_2.setWidth(
41);
InBlock.gif        newColumnTableColumn_2.setText(
"性别");
InBlock.gif
InBlock.gif        
final TableColumn newColumnTableColumn_3 = new TableColumn(table, SWT.NONE);
InBlock.gif        newColumnTableColumn_3.setWidth(
43);
InBlock.gif        newColumnTableColumn_3.setText(
"年龄");
InBlock.gif
InBlock.gif        
final TableColumn newColumnTableColumn_4 = new TableColumn(table, SWT.NONE);
InBlock.gif        newColumnTableColumn_4.setWidth(
126);
InBlock.gif        newColumnTableColumn_4.setText(
"创建日期");
InBlock.gif        
InBlock.gif        shell.open();
ExpandedSubBlockStart.gifContractedSubBlock.gif        
while (!shell.isDisposed()) dot.gif{
InBlock.gif            
if (!display.readAndDispatch())
InBlock.gif                display.sleep();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
2,加入布局
显示的效果不好,我们要加入布局,让view填充整个画面在shell.open()后加上
ok,效果达到
3,为TableViewer加上数据,光突突的样子不好看。这里是demo所以不再从数据库里边取数据了。自己构造一个List用来做数据。实际使用中也是这么使用,不过不同的是这里是随意构造的list,而显示实际使用中是从数据库取出数据构造list.
(1)构造一个people类,同时给People类提供一个构造假数据的方法getPeople()
None.gif import  java.util.ArrayList;
None.gif
import  java.util.Date;
None.gif
import  java.util.List;
ExpandedBlockStart.gifContractedBlock.gif
public   class  People dot.gif {
InBlock.gif    
private Long id;
InBlock.gif    
private String name;
InBlock.gif    
private String sex;
InBlock.gif    
private Integer age;
InBlock.gif    
private Date createDate;
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public People(Long id,String name,String sex,Integer age,Date createDate)dot.gif{
InBlock.gif        
this.id = id;
InBlock.gif        
this.name = name;
InBlock.gif        
this.sex = sex;
InBlock.gif        
this.age = age;
InBlock.gif        
this.createDate = createDate;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public Integer getAge() dot.gif{
InBlock.gif        
return age;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void setAge(Integer age) dot.gif{
InBlock.gif        
this.age = age;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public Date getCreateDate() dot.gif{
InBlock.gif        
return createDate;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void setCreateDate(Date createDate) dot.gif{
InBlock.gif        
this.createDate = createDate;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public Long getId() dot.gif{
InBlock.gif        
return id;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void setId(Long id) dot.gif{
InBlock.gif        
this.id = id;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public String getName() dot.gif{
InBlock.gif        
return name;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void setName(String name) dot.gif{
InBlock.gif        
this.name = name;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public String getSex() dot.gif{
InBlock.gif        
return sex;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gif
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/104311
推荐阅读
相关标签
  

闽ICP备14008679号