swt進度條

 import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.CoolBar;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.swtdesigner.SWTResourceManager;

public class ProgressBarExample
{
 private Text text;
 private ProgressBar progressBar;
 private int max;
 private int i;
 private int Value;
 private int value;

 public void ProgessBarExample() throws InterruptedException
 {
  final Display display = Display.getDefault();
  final Shell shell = new Shell(SWT.MIN|SWT.CLOSE);
  shell.setImage(SWTResourceManager.getImage(ProgressBarExample.class, "/com/sun/java/swing/plaf/windows/icons/JavaCup32.png"));
  shell.setText("正在下載");
  shell.setSize(315, 127);
  shell.setLocation(350, 180);
  shell.setLayout(new FormLayout());
  shell.setBackground(SWTResourceManager.getColor(0, 0, 10));

  // 定義進度條
  progressBar = new ProgressBar(shell, SWT.HORIZONTAL);
  
  FormData data = new FormData(237, 20);
  data.top = new FormAttachment(100, -65);
  data.bottom = new FormAttachment(100, -48);
  data.right = new FormAttachment(100, -67);
  data.left = new FormAttachment(100, -272);
  progressBar.setLayoutData(data);
  
  max = progressBar.getMaximum();

  text = new Text(shell, SWT.BORDER);
  text.setBackground(SWTResourceManager.getColor(128, 255, 255));
  text.setEnabled(false);
  
  final FormData fd_text = new FormData();
  fd_text.right = new FormAttachment(0, 280);
  fd_text.top = new FormAttachment(progressBar, 0, SWT.TOP);
  fd_text.left = new FormAttachment(progressBar, 5, SWT.RIGHT);
  text.setLayoutData(fd_text);

  // 創建一個線程
  new Thread()
  {
   public void run()
   {
    for (i = 1; i < max; i++)
    {
     System.out.println(i);
     try
     {
      Thread.sleep(100);
     }
     catch (Throwable e) {

     }
     display.asyncExec(new Runnable()
     {
      public void run()
      {
       progressBar.setSelection(i);
       Value = progressBar.getMaximum();
       value = progressBar.getSelection();
       System.out.println("value:"+value);
       text.setText(value + "%");

       if (Value == value)
       {
        final Display dis = Display.getDefault();
        Shell sh = new Shell(dis);
        MessageDialog md = new MessageDialog(sh,"信息提示", null, "下載完成", 2,
        new String[] { "確定" }, 0);
        md.open();
        shell.dispose(); // 釋放shell對象
        return;
       }
      }
     });
    }
   }
  }.start();
  shell.open();
  shell.layout();
  while (!shell.isDisposed())
  {
   if (!display.readAndDispatch())
    display.sleep();
  }
 }

 public static void main(String args[]) throws InterruptedException
 {
  ProgressBarExample pe = new ProgressBarExample();
  pe.ProgessBarExample();
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

山月神话

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值