欢迎各位喜欢SWT的朋友来谈谈自己使用SWT的一些感受和心得

accp206 2008-10-03 07:18:02
在解答某个网友提出的SWT问题时,突然对SWT产生了浓厚兴趣,就玩了一下,呵呵。
这估且算是我的第一个比较正式的SWT示例吧,说实话有一定的成就感哦。呵呵。
喜欢SWT的朋友,不妨谈谈自己使用SWT的一些感受和心得。
并无分相赠,是否顶帖,全在于您的兴致,呵呵。


因为一直使用AWT/Swing,感觉不太适应SWT的编程方式。
但很喜欢SWT,就组件而言,喜欢它的DateTime及ExpandBar。
这两个组件是目前的SwingAPI中是所没有的,以前在Swing用的都是自己写的,没有SWT提供的好玩。呵呵。

这是个使用Table的示例,完整代码如下:

package com.accp;

import java.text.NumberFormat;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class TableDemo {

String [] columnTitles = { "菜名", "单价" };

String [][] tableData = {
{ "板栗山鸡", "10" },
{ "番茄鱼片", "20" },
{ "豆腐", "25" },
{ "甜汁三文鱼", "10" },
{ "清蒸大闸蟹", "15" },
{ "生鱼丝薄饼", "20" },
{ "土豆粉", "28" },
{ "猪蹄汤肉酿鲫鱼", "40" },
{ "北京烤鸭", "25" }
};

NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();

Table table;
Button btnTotal;
Text txtTotal;

void createWidgets(Shell shell) {
//
shell.setText("表格示例");
shell.setLayout(new GridLayout(2, true));
//
Group tableGroup = new Group(shell, SWT.NONE);
tableGroup.setLayoutData (new GridData (SWT.FILL, SWT.FILL, true, true));
tableGroup.setLayout (new GridLayout ());
tableGroup.setText("数据");
//表格
table = new Table(tableGroup, SWT.FULL_SELECTION | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
table.setHeaderVisible(true);
table.setLinesVisible(true);
//表头
for (int i = 0; i < columnTitles.length; i++) {
TableColumn tableColumn = new TableColumn(table, SWT.NONE);
tableColumn.setText(columnTitles[i]);
}
//
for (int i = 0; i < tableData.length; i++) {
TableItem item = new TableItem(table, SWT.NONE);
for (int j = 0; j < tableData[i].length; j++) {
item.setText(j, tableData[i][j]);
}
}
//
Group controlGroup = new Group(shell, SWT.NONE);
controlGroup.setLayoutData (new GridData (SWT.FILL, SWT.FILL, true, true));
controlGroup.setLayout (new GridLayout ());
controlGroup.setText("操作");
//
Composite panel = new Composite(controlGroup, SWT.NONE);
panel.setLayoutData(new GridData (SWT.CENTER, SWT.DEFAULT, true, true));
panel.setLayout( new FillLayout(SWT.VERTICAL) );
//
new Label(panel, SWT.NONE).setText("所有单价的总和");
//
txtTotal = new Text(panel, SWT.BORDER | SWT.RIGHT | SWT.READ_ONLY );
//
btnTotal = new Button(panel, SWT.NONE);
btnTotal.setText("计算");

//pack
for (int i = 0; i < table.getColumnCount(); i++) {
TableColumn tableColumn = table.getColumn(i);
tableColumn.pack();
}
shell.pack();
//事件
btnTotal.addSelectionListener(new SelectionAdapter () {
public void widgetSelected (SelectionEvent event) {
double total = 0, price;
for( int i = 0; i < table.getItemCount(); i++ )
{
TableItem item = table.getItem( i );
price = Double.parseDouble( item.getText( 1 ) );
total += price;
}
txtTotal.setText( currencyFormat.format( total ) );
}
});
}

public Shell open (Display display) {
Shell shell = new Shell(display, SWT.DIALOG_TRIM );
createWidgets( shell );
shell.open();
return shell;
}

public static void main(String[] args) {
Display display = new Display ();
TableDemo example = new TableDemo ();
Shell shell = example.open (display);
while (!shell.isDisposed ())
if (!display.readAndDispatch ()) display.sleep ();
display.dispose ();
}
}

这是运行时的效果图:
...全文
272 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
qingkangxu 2008-10-16
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 qingkangxu 的回复:]
我现在做的服务器管理工具就是用SWT开发的,感觉挺好。
还有就是ECLIPSE界面的核心就是SWT/JFace。感觉就更亲切了,如果做要学插件的朋友会SWT的话,很快就上手了。
[/Quote]
说错话了,不是“我现在做的服务器管理工具”,是“我们项目现在做的服务器管理工具”
qingkangxu 2008-10-16
  • 打赏
  • 举报
回复
我现在做的服务器管理工具就是用SWT开发的,感觉挺好。
还有就是ECLIPSE界面的核心就是SWT/JFace。感觉就更亲切了,如果做要学插件的朋友会SWT的话,很快就上手了。
beckhamcat1 2008-10-15
  • 打赏
  • 举报
回复
玩玩还可以的!~~之前用swt写了个售卡系统接近崩溃,一个类500~800行算正常。哎,想起来都想哭
beckhamcat1 2008-10-15
  • 打赏
  • 举报
回复
玩玩还可以的!~~之前用swt写了个售卡系统接近崩溃,一个类500~800行算正常。哎,想起来都想哭
lizhiweiaini 2008-10-13
  • 打赏
  • 举报
回复
看看,学习学习
DiscussQuestions 2008-10-11
  • 打赏
  • 举报
回复
我也来顶贴了~!!
学习!~!
都还可以1~!~!像《玩转地球》一样的玩转代码就可以了!~!
呵呵
accp206 2008-10-09
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 javajlu 的回复:]
java写gui就是恶心
================
怎么这么说呢?
[/Quote]

别在乎人家怎么说,你自己喜欢就好!呵呵。
我就挺喜欢SWT的,当然,还有Swing,嘿嘿。
javajlu 2008-10-09
  • 打赏
  • 举报
回复
java写gui就是恶心
================
怎么这么说呢?
accp206 2008-10-04
  • 打赏
  • 举报
回复
谢谢楼上顶帖,呵呵。
sunyujia 2008-10-04
  • 打赏
  • 举报
回复
我也是业余写着玩的我的资源里面有
集成了三个开发小工具。以前写的玩的感兴趣可以看看

感觉一般吧,java写gui就是恶心、
accp206 2008-10-03
  • 打赏
  • 举报
回复
这个例子的功能是来自一个网友的提问:http://topic.csdn.net/u/20081002/16/0c5ec881-c0df-424d-a6fc-edf58dcaa5fd.html

其功能并无意义,写这些代码只是为了练习SWT,呵呵。

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧