子在川上曰

  逝者如斯夫不舍昼夜
随笔 - 71, 文章 - 0, 评论 - 915, 引用 - 0
数据加载中……

SWT的状态栏和进度条的实例

网站很久没更新了,最近忙着写《Eclipse从入门到精通》的第二版,因为SWT API变化的原因,程序全验证了一篇,有些实例重写了,还增加了很多内容。这里是新增的一个实例,来源于一个读者的提问。

这一节将实现一个如图13.2所示的实例。为了能看清更面板的框架,把面板都设成了边框型。实例有以下内容:

q        如何创建一个简单的状态栏(基于RCP平台能够直接使用它的进度条服务)

q        如何在状态栏中动态创建进度条,同时如何用完后销毁它。

q        如何用exclude属性、setVisiblelayout方法来动态显示/隐藏面板或组件。

q        再次表明在一个线程Thread里调用SWT组件都要用display.asyncExec()包装一下,而且还要在使用组件前对其是否被销毁做判断

20061212.jpg  

13.2 状态栏里的进度条



import org.eclipse.swt.SWT;
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.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;

public
class ProgressBar3 {

         private Display display;

         private Shell shell;

         private Composite statusbar;

         private Label statusbarLabel;

         private ProgressBar progressBar;

         private Button hideProbarButton;

         publicstaticvoid main(String[] args) {      new ProgressBar3().open();     }

         privatevoid open() {

                   display = Display.getDefault();

                   shell = new Shell();

                   shell.setSize(250, 170);

                   // ---------创建窗口中的其他界面组件-------------

                   shell.setLayout(new GridLayout());

                   createMainComp(shell);//创建主面板

                   createStatusbar(shell);//创建工具栏

                   // -----------------END------------------------

                   shell.layout();

                   shell.open();

                   while (!shell.isDisposed()) {

                            if (!display.readAndDispatch())

                                     display.sleep();

                   }

                   display.dispose();

         }

         privatevoid createMainComp(Composite parent) {

                   Composite comp = new Composite(parent, SWT.BORDER);

                   comp.setLayoutData(new GridData(GridData.FILL_BOTH));

                   comp.setLayout(new RowLayout());

                   createButton(comp);

         }

         privatevoid createButton(Composite parent) {

                   final Button b1 = new Button(parent, SWT.NONE);

                   b1.setText("隐藏状态栏");

                   b1.addSelectionListener(new SelectionAdapter() {

                            privatebooleanflag = true;

                            publicvoid widgetSelected(SelectionEvent e) {

                                     // statusbar.setVisible(false)来隐藏状态栏是不够的,还必须把它占用的空间也释放出来,这时应该用GridData.exclude

                                     GridData data = (GridData) statusbar.getLayoutData();

                                     data.exclude = flag;

                                     shell.layout();

                                     b1.setText((flag ? "显示" : "隐藏") + "状态栏");

                                     flag = !flag;

                            }

                   });

                   hideProbarButton = new Button(parent, SWT.NONE);

                   hideProbarButton.setText("隐藏进度条");

                   hideProbarButton.setEnabled(false);

                   hideProbarButton.addSelectionListener(new SelectionAdapter() {

                            privatebooleanflag = false;

                            publicvoid widgetSelected(SelectionEvent e) {

                                     progressBar.setVisible(flag);

                                     hideProbarButton.setText((flag ? "隐藏" : "显示") + "进度条");

                                     flag = !flag;

                            }

                   });

                   final Button b3 = new Button(parent, SWT.NONE);

                   b3.setText(" GO ");

                   b3.addSelectionListener(new SelectionAdapter() {

                            privatebooleanstopFlag = true;

                            publicvoid widgetSelected(SelectionEvent e) {

                                     stopFlag = !stopFlag;

                                     if (stopFlag) // 根据停止标志stopFlag来判断是停止还是运行

                                               stop();

                                     else

                                               go();

                            }

                            privatevoid stop() {

                                     b3.setEnabled(false);// 停止需要时间,在完全停止前要防止再次开始。

                                     b3.setText("GO");

                            }

                            privatevoid go() {

                                     b3.setText("STOP");

                                     progressBar = createProgressBar(statusbar);

                                     hideProbarButton.setEnabled(true);

                                     statusbar.layout();// 重新布局一下工具栏,使进度条显示出来

                                     new Thread() {

                                               publicvoid run() {

                                                        for (int i = 1; i < 11; i++) {

                                                                 if (display.isDisposed() || stopFlag) {

                                                                           disposeProgressBar();

                                                                           return;

                                                                 }

                                                                 moveProgressBar(i);

                                                                 try {  Thread.sleep(1000);          } catch (Throwable e2) {} //停一秒

                                                        }

                                                        disposeProgressBar();

                                               }

                                               privatevoid moveProgressBar(finalint i) {

                                                        display.asyncExec(new Runnable() {

                                                                 publicvoid run() {

                                                                           if (!statusbarLabel.isDisposed())

                                                                                    statusbarLabel.setText("前进到第" + i + "");

                                                                           if (!progressBar.isDisposed())

                                                                                    progressBar.setSelection(i * 10);

                                                                 }

                                                        });

                                               }

                                               privatevoid disposeProgressBar() {

                                                        if (display.isDisposed())   return;

                                                        display.asyncExec(new Runnable() {

                                                                 publicvoid run() {

                                                                           hideProbarButton.setEnabled(false);

                            // 这一句不能放在线程外执行,否则progressBar被创建后就立即被dispose

                                                                           progressBar.dispose();

                                                                           b3.setEnabled(true);

                                                                 }

                                                        });

                                               }

                                     }.start();

                            }

                   });

         }

         privatevoid createStatusbar(Composite parent) {

                   statusbar = new Composite(parent, SWT.BORDER);

                   //设置工具栏在Shell中的形状为水平抢占充满,并高19像素

                   GridData gridData = new GridData(GridData.FILL_HORIZONTAL);

                   gridData.heightHint = 19;

                   statusbar.setLayoutData(gridData);

                   //设置为用行列式布局管理状态栏里的组件

                   RowLayout layout = new RowLayout();

                   layout.marginLeft = layout.marginTop = 0; //无边距

                   statusbar.setLayout(layout);

                   //创建一个用于显示文字的标签

                   statusbarLabel = new Label(statusbar, SWT.BORDER);

                   statusbarLabel.setLayoutData(new RowData(70, -1));

         }

         //创建进度条

         private ProgressBar createProgressBar(Composite parent) {

                   ProgressBar progressBar = new ProgressBar(parent, SWT.SMOOTH);

                   progressBar.setMinimum(0); // 最小值

                   progressBar.setMaximum(100);// 最大值

                   return progressBar;

         }

}

posted on 2006-12-12 22:15 陈刚 阅读(10001) 评论(14)  编辑  收藏 所属分类: Eclipse

评论

# re: SWT的状态栏和进度条的实例  回复  更多评论   

SWT 固然好, 但是要命的是它的 API 老是变来变去, 写的老 Plugin 在平台一升级就不能用了, 这也是 SWT 相对于 Swing 来说为数不多的缺点之一: 兼容性太差, 起码自己的新版本兼容自己的老版本, 或者 API 要稳定一下嘛.. 没人控制它的前后兼容性.
2006-12-13 09:58 | BeanSoft

# re: SWT的状态栏和进度条的实例  回复  更多评论   

swt基本是向下兼容的,pugin是eclipse的问题和swt基本没什么关系
2006-12-13 21:20 | jrobot[匿名]

# re: SWT的状态栏和进度条的实例  回复  更多评论   

我現在仍然看不到進度條啊?
2006-12-15 11:24 | Warren.Wu

# re: SWT的状态栏和进度条的实例  回复  更多评论   

陈老师:
你在准备第二版?这可真是个好消息!能先透漏一下增加了那些新的东西吗?经常去书店,发现还是你的书讲的详细,设身处地的在为初学者着想!

我想说说我的想法:
书中的内容不要包容的太多,因为我怕最后由于篇幅所限或出版社的原因导致基础的东西讲不够,高深的东西讲不透。市面上有很多这样的书,比如其他出版社出的《精通ecilpse》之类的,很令人讨厌!这些书只是在介绍eclipse能做什么,但却教不会读者用eclispe怎样做。

图13.2的截面是那个系统下的,很漂亮嘛!告诉我吧
2006-12-30 15:47 | 高井林

# re: SWT的状态栏和进度条的实例  回复  更多评论   

我晕!被摆了一道,原来是XP的皮肤
2007-03-14 15:56 | 催月泪

# re: SWT的状态栏和进度条的实例  回复  更多评论   

把 go()方法中的 hideProbarButton.setEnabled(true); 改成progressBar.setEnabled(true); 就可以看到进度条了。
2007-05-11 11:33 | 还是老妖

# re: SWT的状态栏和进度条的实例  回复  更多评论   

还是不能显示进度条啊!
2007-05-24 01:09 | elvis

# re: SWT的状态栏和进度条的实例  回复  更多评论   

不能显示是因为shell的size太小,水平拖动一下就可以了
2007-08-21 11:36 | swewe

# re: SWT的状态栏和进度条的实例  回复  更多评论   

为什么我设置的进度条样式是SWT.SMOOTH,结果显示出来的还是方格型的,好奇怪~!

另外,我想让进度条是垂直时,它的默认推进方式是向上推进的,我想做成向下推进的,不知道怎么实现?
2007-08-22 16:06 | cs

# 为什么我设置的进度条样式是SWT.SMOOTH,结果显示出来的还是方格型  回复  更多评论   

为什么连陈刚老师写的程序,在我这里运行还是方格的,根本不是平滑的呢?
2008-04-19 10:48 | tt

# 为什么我设置的进度条样式是SWT.SMOOTH,结果显示出来的还是方格型  回复  更多评论   

等待陈刚老师解答,我试了很多次,把所有的都试便了,还是改变不了
2008-04-19 10:50 | tt

# re: SWT的状态栏和进度条的实例  回复  更多评论   

有可能是你 windows的原因。
2008-04-21 22:00 | 陈 刚

# re: SWT的状态栏和进度条的实例[未登录]  回复  更多评论   

GridData.exclude属性是swt3.1里面的,3.0以前没有怎么办啊?
2008-11-25 14:09 | aa

# re: SWT的状态栏和进度条的实例[未登录]  回复  更多评论   

老陈,怎么修改进度条上面的Loading workbench ....???
2010-03-22 18:03 | 哈哈

只有注册用户登录后才能发表评论。


网站导航: