SWT/Jface ProgressBar-平滑型-带下载百分比的例子

该示例展示了如何在Java中使用SWT/Jface库创建一个平滑的水平进度条,并实时更新显示下载进度百分比。程序创建一个Shell窗口,包含一个不可编辑的文本框显示百分比和一个进度条。通过后台线程更新进度条,当达到最大值时弹出信息提示对话框,表明下载已完成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
 * @Title: ProgressBarExample1.java
 * @Description: TODO
 * @author zouxs
 * @date 2012-9-25
 */
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;


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

public void ProgessBarExample1() throws InterruptedException
{
 final Display display = Display.getDefault();
 final Shell shell = new Shell(SWT.MIN|SWT.CLOSE);
 shell.setText("正在下載");
 shell.setSize(315, 127);
 shell.setLocation(350, 180);
 shell.setLayout(new FormLayout());

 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.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();
       return;
      }
     }
    });
   }
  }
 }.start();
 shell.open();
 shell.layout();
 while (!shell.isDisposed())
 {
  if (!display.readAndDispatch())
   display.sleep();
 }
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值