说明:SWT综合实例实现了一个QQ的简化界面,基本上用到了SWT的各方面;
使用SWT要先导入一个SWT的dll文件,本例导入的dll文件名为:swt-win32-3139.dll
QQ.java源文件内容如下:
package com.awt.sample;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.*;
public class QQ {
/*
为了自定义监听器类MyMouseListener中的代码能访问到以下对象,故将这些对象定义成类的实例变量
*/
private StackLayout stackLayout = new
StackLayout();
private Composite yourDataComp;
private Composite otherComp;
private List selectList;
private Composite rightComp;
/*
由于自定义方法程序较长,这次的主程序框架选择了Eclispe“新建”向导的第二种"public
open() mothd" */
public static void main(String[] args)
{
try {
QQ window =
new QQ();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
public void open() {
Display display =
Display.getDefault();
Shell shell = new
Shell();
shell.setSize(550, 350);
shell.setText("个人设置");
shell.setLayout(new
GridLayout());
{
//
分割窗口
SashForm
sashForm = new SashForm(shell, SWT.BORDER);
sashForm.setLayoutData(new
GridData(GridData.FILL_BOTH));
{
//
分割窗左边的列表框
selectList
= new List(sashForm, SWT.BORDER);
//
作为演示只加了两项
selectList.setItems(new
String[] { "个人资料", "联系方式" });
//
加一个鼠标监听器
selectList.addMouseListener((MouseListener)
new MyMouseListener());
}
{
//
右边的堆栈式面板
rightComp
= new Composite(sashForm, SWT.NONE);
rightComp.setLayout(stackLayout);
//
共两项,将生成此面板的代码提出成一个方法,保证代码结构的清晰
yourDataComp
= createYourDataComp(rightComp);// 个人资料的面板
otherComp
= createOtherComp(rightComp);// 联系方式的面板
//
在堆栈面板上先显示"个人资料"界面
stackLayout.topControl
= yourDataComp;
}
//
分割窗口的左右空间比例
sashForm.setWeights(new
int[] { 1, 4 });
}
{
//
界面的按钮的面板
Composite
buttonComp = new Composite(shell, SWT.BORDER);
//
使用GridData设置buttonsComp在它你容器shell中的布局方式
GridData
gridData = new GridData();
gridData.horizontalAlignment
= GridData.END;// 让buttonComp向右靠
buttonComp.setLayoutData(gridData);
//
设置buttonsComp的布局为RowLayout,用来设定buttonComp内组件的布局方式
RowLayout
rowLayout = new RowLayout();
rowLayout.spacing
= 15;// 按钮之间间隔15个元素
buttonComp.setLayout(rowLayout);
//
在buttonComp下建立三个按钮
Button btn1 =
new Button(buttonComp, SWT.NONE);
btn1.setText("确定");
btn1.addSelectionListener(new
SelectionAdapter() {
public
void widgetSelected(SelectionEvent e) {
MessageDialog.openInformation(null,
"提示", "你单击了'确定'按钮");
System.exit(0);
}
});
Button btn2 =
new Button(buttonComp, SWT.NONE);
btn2.setText("取消");
btn2.addSelectionListener(new
SelectionAdapter() {
public
void widgetSelected(SelectionEvent e) {
MessageDialog.openInformation(null,
"提示", "你单击了'取消'按钮");
System.exit(0);
}
});
Button btn3 =
new Button(buttonComp, SWT.NONE);
btn3.setText("应用");
btn3.addSelectionListener(new
SelectionAdapter() {
public
void widgetSelected(SelectionEvent e) {
MessageDialog.openInformation(null,
"提示", "你单击了'应用'按钮");
}
});
}
shell.layout();
shell.open();
while (!shell.isDisposed())
{
if
(!display.readAndDispatch())
display.sleep();
}
}
// 个人资料面板的生成
private Composite createYourDataComp(Composite
rightComp) {
Composite composite = new
Composite(rightComp, SWT.NONE);
composite.setLayout(new
GridLayout(6, false));// 个人资料面板分成6列
// 用户号码的标签及文本框
new Label(composite,
SWT.NONE).setText("用户号码");
// 只读型的文本框
Text text = new Text(composite,
SWT.READ_ONLY | SWT.BORDER);
//
水平抢占式充满,并占用三列的空间 createGridData是自定义方法
text.setLayoutData(createGridData(GridData.FILL_HORIZONTAL,
3));
//
图片部分,我们再用一个面板嵌套来管理
Composite photoComp = new
Composite(composite, SWT.BORDER);
//
水平垂直对齐式充满,横占两列,竖占4行
photoComp.setLayoutData(createGridData(GridData.HORIZONTAL_ALIGN_FILL
|
GridData.VERTICAL_ALIGN_FILL, 2, 4));
photoComp.setLayout(new
GridLayout(2, false));// 分2列
{
// 图片
Image类的使用暂还没讲到,我们先用一个Composite来代替图片
Composite
tempComp = new Composite(photoComp, SWT.BORDER);
tempComp.setLayoutData(new
GridData(50, 50));// 设定大小; 宽50,高50
//
选择图片的箭头型按钮,并设置它向下靠
Button
selPhotoButton = new Button(photoComp, SWT.ARROW | SWT.DOWN);
selPhotoButton.setLayoutData(new
GridData(
GridData.VERTICAL_ALIGN_END));
//
升级成为会员的按钮,横占photoComp的两列,并横向对齐充满
Button
updateButton = new Button(photoComp, SWT.NONE);
updateButton.setLayoutData(createGridData(
GridData.HORIZONTAL_ALIGN_FILL,
2));
updateButton.setText("升级成为会员");
}
new Label(composite,
SWT.NONE).setText("用户昵称");
Text nicknameText = new
Text(composite, SWT.BORDER);
nicknameText.setLayoutData(createGridData(
GridData.HORIZONTAL_ALIGN_FILL,
3));
new Label(composite,
SWT.NONE).setText("个性签名");
Text attachNameText = new
Text(composite, SWT.BORDER);
attachNameText.setLayoutData(createGridData(
GridData.HORIZONTAL_ALIGN_FILL,
3));
new Label(composite,
SWT.NONE).setText("等级");
{
//
Image类的使用暂还没有讲到,我们先用一个Composite来代替图片
Composite
tempComp = new Composite(composite, SWT.BORDER);
GridData
gridData = new GridData(GridData.FILL_HORIZONTAL
|
GridData.VERTICAL_ALIGN_BEGINNING);
gridData.horizontalSpan
= 3;
//
Composite默认的高度太高,故手工设定高度为20像素
gridData.heightHint
= 20;
tempComp.setLayoutData(gridData);
}
new Label(composite,
SWT.NONE).setText("姓名");
Text nameText = new
Text(composite, SWT.BORDER);
nameText.setLayoutData(new
GridData(GridData.HORIZONTAL_ALIGN_FILL));
new Label(composite,
SWT.NONE).setText("性别");
Combo setCombo = new
Combo(composite, SWT.NONE);
new Label(composite,
SWT.NONE).setText("年龄");
Text okText = new
Text(composite, SWT.BORDER);
okText.setLayoutData(new
GridData(GridData.HORIZONTAL_ALIGN_FILL));
new Label(composite,
SWT.NONE).setText("毕业院校");
Text schoolText = new
Text(composite, SWT.BORDER);
schoolText.setLayoutData(createGridData(GridData.HORIZONTAL_ALIGN_FILL,
3));
new Label(composite,
SWT.NONE).setText("生肖");
Combo animalCombo = new
Combo(composite, SWT.NONE);
animalCombo.setLayoutData(new
GridData(GridData.HORIZONTAL_ALIGN_FILL));
new Label(composite,
SWT.NONE).setText("职业");
Text jobText = new
Text(composite, SWT.BORDER);
jobText
.setLayoutData(createGridData(GridData.HORIZONTAL_ALIGN_FILL,
3));
new Label(composite,
SWT.NONE).setText("星座");
Text starText = new
Text(composite, SWT.BORDER);
Label introLabel = new
Label(composite, SWT.NONE);
//
默认是居中,改为顶端对齐
introLabel
.setLayoutData(new
GridData(GridData.VERTICAL_ALIGN_BEGINNING));
introLabel.setText("个人说明");
Text introlText = new
Text(composite, SWT.BORDER | SWT.WRAP);// WRAP自动换行
introlText.setLayoutData(createGridData(GridData.HORIZONTAL_ALIGN_FILL
|
GridData.FILL_VERTICAL, 5));
//
返回个人资料面板composite
return composite;
}
// 生成一个简单的联系方式的面板
private Composite createOtherComp(Composite
rightComp) {
Composite composite = new
Composite(rightComp, SWT.NONE);
composite.setLayout(new
GridLayout(4, false));
new Label(composite,
SWT.NONE).setText("国家/地区");
Combo contryCombo = new
Combo(composite, SWT.NONE);
contryCombo.setLayoutData(new
GridData(GridData.HORIZONTAL_ALIGN_FILL));
new Label(composite,
SWT.NONE).setText("省份");
Combo stateCombo = new
Combo(composite, SWT.NONE);
stateCombo.setLayoutData(new
GridData(GridData.HORIZONTAL_ALIGN_FILL));
new Label(composite,
SWT.NONE).setText("姓名");
Text nameText = new
Text(composite, SWT.BORDER);
nameText
.setLayoutData(createGridData(GridData.HORIZONTAL_ALIGN_FILL,
3));
new Label(composite,
SWT.NONE).setText("工号");
Text wnText = new
Text(composite, SWT.BORDER);
wnText.setLayoutData(createGridData(GridData.HORIZONTAL_ALIGN_FILL,
3));
new Label(composite,
SWT.NONE).setText("密码");
Text pswText = new
Text(composite, SWT.PASSWORD|SWT.BORDER);
pswText
.setLayoutData(createGridData(GridData.HORIZONTAL_ALIGN_FILL,
3));
return composite;
}
/*
*
生成GridData对象的重复代码太多,写成一个方式可以减少程序的行数,用起来也方便,而且还要注意一个GridData只能被一个组件使用,不能两个组件使用一个GridData,则
*
* 有一个组件将不会显示出来
*/
private GridData createGridData(int style, int
horizontalSpan) {
GridData gridData = new
GridData(style);
gridData.horizontalSpan =
horizontalSpan;
return gridData;
}
private GridData createGridData(int style,
int horizontalSpan,
int
verticalSpan) {
GridData gridData = new
GridData(style);
gridData.horizontalSpan =
horizontalSpan;
gridData.verticalSpan =
verticalSpan;
return gridData;
}
//
鼠标监听器,采用事件的命名内部类的写法
private class MyMouseListener extends
MouseAdapter {
public void
mouseDown(MouseEvent e) {
//
得到列表的当前选择项的序号(即单击的那项)
int
selectIndex = selectList.getSelectionIndex();
//
如果单击第一项"个人资料",则让相对的面板移至居上面
if
(selectIndex == 0)
stackLayout.topControl
= yourDataComp;
else
stackLayout.topControl
= otherComp;
rightComp.layout();//
刷新堆栈式布局的项容器
}
}
}
度条对话框,源文件内容如下:
package com.awt.sample;
import java.lang.reflect.InvocationTargetException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class ProgressDialog {
public static void main(String[] args)
{
// TODO 自动生成方法存根
new
ProgressDialog().monitor();
}
public void monitor() {
Display display =
Display.getDefault();
final Shell shell = new
Shell();
shell.setSize(550, 350);
shell.setLocation(450,
200);
shell.setText("进度条对话框测试");
shell.setLayout(new
RowLayout());
Button button = new
Button(shell, SWT.BORDER | SWT.CENTER);
button.setText(" GO ");
button.addSelectionListener(new
SelectionAdapter() {
public void
widgetSelected(SelectionEvent e) {
//
第一步:创建一个进度条对话框对象
System.out.println("开始2");
ProgressMonitorDialog
pmd = new
ProgressMonitorDialog(shell);//ProgressMonitorDialog是SWT的类
//
第二步:创建进度条对话框的处理过程对象
IRunnableWithProgress
rwp = new IRunnableWithProgress() {
public
void run(IProgressMonitor monitor)
throws
InvocationTargetException,
InterruptedException
{
//
TODO 自动生成方法存根
monitor.beginTask("开始执行......",
5);// 10,表示进度条分为10格
//
10次循环,每次间隔一秒
for
(int i = 0; i < 5; i++) {
try
{
Thread.sleep(1000);//
暂停一秒
}
catch (Throwable e) {
}
monitor.setTaskName("第"
+ (i + 1) + "次循环");
monitor.worked(1);//
进度条前进一步
}
monitor.done();//
进度条前进到底
}
};
//
第三步:进行对话框,第二个参数为false表示对话框上的取消按钮不可用
try
{
//
第一个参数表示:进度条对话框出现时(true,鼠标为沙漏;false,鼠标为正常状)
//
第二个参数表示:进度条对话框中的"取消"按钮是否可用(true,可用;false,不可用)
pmd.run(false,
false, rwp);//运行进度条对话框
}
catch (InvocationTargetException e1) {
//
TODO 自动生成 catch 块
e1.printStackTrace();
}
catch (InterruptedException e1) {
//
TODO 自动生成 catch 块
e1.printStackTrace();
}
}
});
// shell.layout();
shell.open();
while (!shell.isDisposed())
{
if
(!display.readAndDispatch())
display.sleep();
}
}
}