在解答某个网友提出的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 ();
}
}这是运行时的效果图:

解决方案 »

  1.   

    这个例子的功能是来自一个网友的提问:http://topic.csdn.net/u/20081002/16/0c5ec881-c0df-424d-a6fc-edf58dcaa5fd.html其功能并无意义,写这些代码只是为了练习SWT,呵呵。
      

  2.   

    我也是业余写着玩的我的资源里面有
    集成了三个开发小工具。以前写的玩的感兴趣可以看看感觉一般吧,java写gui就是恶心、
      

  3.   

    java写gui就是恶心
    ================
    怎么这么说呢?
      

  4.   


    别在乎人家怎么说,你自己喜欢就好!呵呵。
    我就挺喜欢SWT的,当然,还有Swing,嘿嘿。
      

  5.   

    玩玩还可以的!~~之前用swt写了个售卡系统接近崩溃,一个类500~800行算正常。哎,想起来都想哭
      

  6.   

    玩玩还可以的!~~之前用swt写了个售卡系统接近崩溃,一个类500~800行算正常。哎,想起来都想哭
      

  7.   

    我现在做的服务器管理工具就是用SWT开发的,感觉挺好。
    还有就是ECLIPSE界面的核心就是SWT/JFace。感觉就更亲切了,如果做要学插件的朋友会SWT的话,很快就上手了。