子在川上曰

            逝者如斯夫不舍晝夜
          隨筆 - 71, 文章 - 0, 評論 - 915, 引用 - 0
          數(shù)據(jù)加載中……

          SWT的狀態(tài)欄和進(jìn)度條的實(shí)例

          網(wǎng)站很久沒更新了,最近忙著寫《Eclipse從入門到精通》的第二版,因?yàn)镾WT API變化的原因,程序全驗(yàn)證了一篇,有些實(shí)例重寫了,還增加了很多內(nèi)容。這里是新增的一個實(shí)例,來源于一個讀者的提問。

          這一節(jié)將實(shí)現(xiàn)一個如圖13.2所示的實(shí)例。為了能看清更面板的框架,把面板都設(shè)成了邊框型。實(shí)例有以下內(nèi)容:

          q??????? 如何創(chuàng)建一個簡單的狀態(tài)欄(基于RCP平臺能夠直接使用它的進(jìn)度條服務(wù))

          q??????? 如何在狀態(tài)欄中動態(tài)創(chuàng)建進(jìn)度條,同時如何用完后銷毀它。

          q??????? 如何用exclude屬性、setVisiblelayout方法來動態(tài)顯示/隱藏面板或組件。

          q??????? 再次表明在一個線程Thread里調(diào)用SWT組件都要用display.asyncExec()包裝一下,而且還要在使用組件前對其是否被銷毀做判斷

          20061212.jpg??

          13.2 狀態(tài)欄里的進(jìn)度條



          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);

          ?????????????????? // ---------創(chuàng)建窗口中的其他界面組件-------------

          ?????????????????? shell.setLayout(new GridLayout());

          ?????????????????? createMainComp(shell);//創(chuàng)建主面板

          ?????????????????? createStatusbar(shell);//創(chuàng)建工具欄

          ?????????????????? // -----------------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("隱藏狀態(tài)欄");

          ?????????????????? b1.addSelectionListener(new SelectionAdapter() {

          ??????????????????????????? privatebooleanflag = true;

          ??????????????????????????? publicvoid widgetSelected(SelectionEvent e) {

          ???????????????????????????????????? // statusbar.setVisible(false)來隱藏狀態(tài)欄是不夠的,還必須把它占用的空間也釋放出來,這時應(yīng)該用GridData.exclude

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

          ???????????????????????????????????? data.exclude = flag;

          ???????????????????????????????????? shell.layout();

          ???????????????????????????????????? b1.setText((flag ? "顯示" : "隱藏") + "狀態(tài)欄");

          ???????????????????????????????????? flag = !flag;

          ??????????????????????????? }

          ?????????????????? });

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

          ?????????????????? hideProbarButton.setText("隱藏進(jìn)度條");

          ?????????????????? hideProbarButton.setEnabled(false);

          ?????????????????? hideProbarButton.addSelectionListener(new SelectionAdapter() {

          ??????????????????????????? privatebooleanflag = false;

          ??????????????????????????? publicvoid widgetSelected(SelectionEvent e) {

          ???????????????????????????????????? progressBar.setVisible(flag);

          ???????????????????????????????????? hideProbarButton.setText((flag ? "隱藏" : "顯示") + "進(jìn)度條");

          ???????????????????????????????????? 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) // 根據(jù)停止標(biāo)志stopFlag來判斷是停止還是運(yùn)行

          ?????????????????????????????????????????????? stop();

          ???????????????????????????????????? else

          ?????????????????????????????????????????????? go();

          ??????????????????????????? }

          ??????????????????????????? privatevoid stop() {

          ???????????????????????????????????? b3.setEnabled(false);// 停止需要時間,在完全停止前要防止再次開始。

          ???????????????????????????????????? b3.setText("GO");

          ??????????????????????????? }

          ??????????????????????????? privatevoid go() {

          ???????????????????????????????????? b3.setText("STOP");

          ???????????????????????????????????? progressBar = createProgressBar(statusbar);

          ???????????????????????????????????? hideProbarButton.setEnabled(true);

          ???????????????????????????????????? statusbar.layout();// 重新布局一下工具欄,使進(jìn)度條顯示出來

          ???????????????????????????????????? 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("前進(jìn)到第" + i + "");

          ?????????????????????????????????????????????????????????????????????????? if (!progressBar.isDisposed())

          ??????????????????????????????????????????????????????????????????????????????????? progressBar.setSelection(i * 10);

          ???????????????????????????????????????????????????????????????? }

          ??????????????????????????????????????????????????????? });

          ?????????????????????????????????????????????? }

          ?????????????????????????????????????????????? privatevoid disposeProgressBar() {

          ??????????????????????????? ??????????????????????????? if (display.isDisposed())?? return;

          ??????????????????????????????????????????????????????? display.asyncExec(new Runnable() {

          ???????????????????????????????????????????????????????????????? publicvoid run() {

          ?????????????????????????????????????????????????????????????????????????? hideProbarButton.setEnabled(false);

          ??????????????????????????? // 這一句不能放在線程外執(zhí)行,否則progressBar被創(chuàng)建后就立即被dispose

          ?????????????????????????????????????????????????????????????????????????? progressBar.dispose();

          ?????????????????????????????????????????????????????????????????????????? b3.setEnabled(true);

          ???????????????????????????????????????????????????????????????? }

          ??????????????????????????????????????????????????????? });

          ?????????????????????????????????????????????? }

          ???????????????????????????????????? }.start();

          ??????????????????????????? }

          ?????????????????? });

          ???????? }

          ???????? privatevoid createStatusbar(Composite parent) {

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

          ?????????????????? //設(shè)置工具欄在Shell中的形狀為水平搶占充滿,并高19像素

          ?????????????????? GridData gridData = new GridData(GridData.FILL_HORIZONTAL);

          ?????????????????? gridData.heightHint = 19;

          ?????????????????? statusbar.setLayoutData(gridData);

          ?????????????????? //設(shè)置為用行列式布局管理狀態(tài)欄里的組件

          ?????????????????? RowLayout layout = new RowLayout();

          ?????????????????? layout.marginLeft = layout.marginTop = 0; //無邊距

          ?????????????????? statusbar.setLayout(layout);

          ?????????????????? //創(chuàng)建一個用于顯示文字的標(biāo)簽

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

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

          ???????? }

          ???????? //創(chuàng)建進(jìn)度條

          ???????? 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 陳剛 閱讀(10020) 評論(14)  編輯  收藏 所屬分類: Eclipse

          評論

          # re: SWT的狀態(tài)欄和進(jìn)度條的實(shí)例  回復(fù)  更多評論   

          SWT 固然好, 但是要命的是它的 API 老是變來變?nèi)? 寫的老 Plugin 在平臺一升級就不能用了, 這也是 SWT 相對于 Swing 來說為數(shù)不多的缺點(diǎn)之一: 兼容性太差, 起碼自己的新版本兼容自己的老版本, 或者 API 要穩(wěn)定一下嘛.. 沒人控制它的前后兼容性.
          2006-12-13 09:58 | BeanSoft

          # re: SWT的狀態(tài)欄和進(jìn)度條的實(shí)例  回復(fù)  更多評論   

          swt基本是向下兼容的,pugin是eclipse的問題和swt基本沒什么關(guān)系
          2006-12-13 21:20 | jrobot[匿名]

          # re: SWT的狀態(tài)欄和進(jìn)度條的實(shí)例  回復(fù)  更多評論   

          我現(xiàn)在仍然看不到進(jìn)度條啊?
          2006-12-15 11:24 | Warren.Wu

          # re: SWT的狀態(tài)欄和進(jìn)度條的實(shí)例  回復(fù)  更多評論   

          陳老師:
          你在準(zhǔn)備第二版?這可真是個好消息!能先透漏一下增加了那些新的東西嗎?經(jīng)常去書店,發(fā)現(xiàn)還是你的書講的詳細(xì),設(shè)身處地的在為初學(xué)者著想!

          我想說說我的想法:
          書中的內(nèi)容不要包容的太多,因?yàn)槲遗伦詈笥捎谄藁虺霭嫔绲脑驅(qū)е禄A(chǔ)的東西講不夠,高深的東西講不透。市面上有很多這樣的書,比如其他出版社出的《精通ecilpse》之類的,很令人討厭!這些書只是在介紹eclipse能做什么,但卻教不會讀者用eclispe怎樣做。

          圖13.2的截面是那個系統(tǒng)下的,很漂亮嘛!告訴我吧
          2006-12-30 15:47 | 高井林

          # re: SWT的狀態(tài)欄和進(jìn)度條的實(shí)例  回復(fù)  更多評論   

          我暈!被擺了一道,原來是XP的皮膚
          2007-03-14 15:56 | 催月淚

          # re: SWT的狀態(tài)欄和進(jìn)度條的實(shí)例  回復(fù)  更多評論   

          把 go()方法中的 hideProbarButton.setEnabled(true); 改成progressBar.setEnabled(true); 就可以看到進(jìn)度條了。
          2007-05-11 11:33 | 還是老妖

          # re: SWT的狀態(tài)欄和進(jìn)度條的實(shí)例  回復(fù)  更多評論   

          還是不能顯示進(jìn)度條啊!
          2007-05-24 01:09 | elvis

          # re: SWT的狀態(tài)欄和進(jìn)度條的實(shí)例  回復(fù)  更多評論   

          不能顯示是因?yàn)閟hell的size太小,水平拖動一下就可以了
          2007-08-21 11:36 | swewe

          # re: SWT的狀態(tài)欄和進(jìn)度條的實(shí)例  回復(fù)  更多評論   

          為什么我設(shè)置的進(jìn)度條樣式是SWT.SMOOTH,結(jié)果顯示出來的還是方格型的,好奇怪~!

          另外,我想讓進(jìn)度條是垂直時,它的默認(rèn)推進(jìn)方式是向上推進(jìn)的,我想做成向下推進(jìn)的,不知道怎么實(shí)現(xiàn)?
          2007-08-22 16:06 | cs

          # 為什么我設(shè)置的進(jìn)度條樣式是SWT.SMOOTH,結(jié)果顯示出來的還是方格型  回復(fù)  更多評論   

          為什么連陳剛老師寫的程序,在我這里運(yùn)行還是方格的,根本不是平滑的呢?
          2008-04-19 10:48 | tt

          # 為什么我設(shè)置的進(jìn)度條樣式是SWT.SMOOTH,結(jié)果顯示出來的還是方格型  回復(fù)  更多評論   

          等待陳剛老師解答,我試了很多次,把所有的都試便了,還是改變不了
          2008-04-19 10:50 | tt

          # re: SWT的狀態(tài)欄和進(jìn)度條的實(shí)例  回復(fù)  更多評論   

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

          # re: SWT的狀態(tài)欄和進(jìn)度條的實(shí)例[未登錄]  回復(fù)  更多評論   

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

          # re: SWT的狀態(tài)欄和進(jìn)度條的實(shí)例[未登錄]  回復(fù)  更多評論   

          老陳,怎么修改進(jìn)度條上面的Loading workbench ....???
          2010-03-22 18:03 | 哈哈
          主站蜘蛛池模板: 河池市| 泸水县| 纳雍县| 东丽区| 安阳县| 永登县| 德安县| 仁怀市| 历史| 兰溪市| 龙陵县| 文山县| 革吉县| 汶川县| 栾川县| 澄迈县| 高唐县| 江源县| 彭阳县| 府谷县| 滨州市| 石林| 资阳市| 福建省| 平凉市| 项城市| 乳源| 霞浦县| 鲁甸县| 化隆| 芦溪县| 古蔺县| 盐山县| 五大连池市| 肥乡县| 高青县| 孟津县| 宜章县| 平武县| 沅陵县| 射洪县|