java progressbar swt_Eclipse-SWT学习之进度条的SWT实现

本文详细介绍了在Eclipse的SWT环境中如何使用Java实现进度条组件ProgressBar,包括横向、平滑和不确定进度条的创建及线程同步更新。通过示例代码展示了如何设置最大值、最小值并动态更新进度。
摘要由CSDN通过智能技术生成

图示:

package ProgressBar;

import java.awt.DisplayMode;

import org.eclipse.swt.SWT;

import org.eclipse.swt.layout.GridLayout;

import org.eclipse.swt.graphics.Point;

import org.eclipse.swt.widgets.ProgressBar;

import org.eclipse.swt.widgets.Shell;

import org.eclipse.swt.widgets.Display;

public class Example_ProgressBar {

private Shell sShell = null;

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

/*

*

*

*

* */

Display display = Display.getDefault();

Example_ProgressBar thisClass = new Example_ProgressBar();

thisClass.createSShell(display);

thisClass.sShell.open();

while (!thisClass.sShell.isDisposed()) {

if (!display.readAndDispatch())

display.sleep();

}

display.dispose();

}

/**

* This method initializes sShell

*/

private void createSShell(Display display) {

sShell = new Shell(display);

sShell.setText("Shell");

sShell.setSize(new Point(300, 200));

sShell.setLayout(new GridLayout());

ProgressBar pro=progress(display);

pro.pack();

sShell.pack();

}

//进度条

private ProgressBar progress(final Display display){

final ProgressBar progress=new ProgressBar(sShell,SWT.HORIZONTAL);

progress.setMaximum(100);

progress.setMinimum(0);

final int Maximum=progress.getMaximum();

final int Minimum=progress.getMinimum();

//创建线程

Runnable Run=new Runnable(){

public void run(){

for(int i=Minimum;i

try{

//让线程睡眠0.1秒

Thread.sleep(100);

}catch(InterruptedException e){

e.printStackTrace();

}

//让UI线程更新滚动条

display.asyncExec(new Runnable(){

public void run(){

if(progress.isDisposed()) return ;

progress.setSelection(progress.getSelection()+1);

}

});

}

}

};

new Thread(Run).start();

return progress;

}

}

2.///

import org.eclipse.swt.SWT;

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 ProgressBarExamples {

Display display = new Display();

Shell shell = new Shell(display);

public ProgressBarExamples() {

init();

ProgressBar pb1 = new ProgressBar(shell, SWT.NULL);

final ProgressBar pb2 = new ProgressBar(shell, SWT.SMOOTH);

ProgressBar pb3 = new ProgressBar(shell, SWT.INDETERMINATE);

// pb2.addPaintListener(new PaintListener() {

// public void paintControl(PaintEvent e) {

// Point point = pb2.getSize();

//

// Font font = new Font(shell.getDisplay(),"Courier",10,SWT.BOLD);

// e.gc.setFont(font);

// e.gc.setForeground(shell.getDisplay().getSystemColor(SWT.COLOR_WHITE));

//

// FontMetrics fontMetrics = e.gc.getFontMetrics();

// int stringWidth = fontMetrics.getAverageCharWidth() * 4;

// int stringHeight = fontMetrics.getHeight();

//

// e.gc.drawString("60%", (point.x-stringWidth)/2 , (point.y-stringHeight)/2, true);

// font.dispose();

// }

// });

pb1.setSelection(60);

pb2.setSelection(60);

pb1.setBounds(100, 10, 200, 20);

pb2.setBounds(100, 40, 200, 20);

//pb3.setBounds(100, 70, 200, 20);

Label label = new Label(shell, SWT.NULL);

label.setText("(default)");

Label label2 = new Label(shell, SWT.NULL);

label2.setText("SWT.SMOOTH");

label.setAlignment(SWT.RIGHT);

label2.setAlignment(SWT.RIGHT);

label.setBounds(10, 10, 80, 20);

label2.setBounds(10, 40, 80, 20);

shell.pack();

shell.open();

//textUser.forceFocus();

// Set up the event loop.

while (!shell.isDisposed()) {

if (!display.readAndDispatch()) {

// If no more entries in event queue

display.sleep();

}

}

display.dispose();

}

private void init() {

}

public static void main(String[] args) {

new ProgressBarExamples();

}

}

3.

进度条组件ProgressBar

ProgressBar是SWT中的进度条组件。进度条供给了对比长光阴操作的进度信息。添加ProgressBar组件的步骤如下:

1. 创立 ProgressBar对象,并指定创立的样式,例如“ProgressBar pb1 = new ProgressBar (shell, SWT.HORIZONTAL | SWT.SMOOTH);”。

2. 设置ProgressBar的最大值和最小值,例如“pb1.setMaximum(30);”。

3. 在长光阴的任务 中设置当前进度条的进度,例如“progressBar.setSelection (progressBar.getSelection() + 1);”。

进度条能反响当前的工作进度,为了配合处理 长光阴的任务 ,进度条经常配合线程应用,以免产生 阻塞影响界面的操作。为了更好地控制 ProgressBar组件,下面通过一个实例演示如何创立 ProgressBar组件,代码如例程6所示。

例程6 ProgressBarExample.java

public class ProgressBarExample {

public static void main(String[] args) {

Display display = new Display();

Shell shell = new Shell(display);

shell.setLayout(new GridLayout());

//添加平滑的进度条

ProgressBar pb1 = new ProgressBar(shell, SWT.HORIZONTAL | SWT.SMOOTH);

pb1.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

//设置进度条的最小值

pb1.setMinimum(0);

//设置进度条的最大值

pb1.setMaximum(30);

//添加主动

递增的进度条

ProgressBar pb2 = new ProgressBar(shell, SWT.HORIZONTAL |

SWT.INDETERMINATE);

pb2.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

//添加线程,在线程中处理

长光阴

的任务

,并最终反响在平滑进度条上

new LongRunningOperation(display, pb1).start();

shell.open();

while (!shell.isDisposed()) {

if (!display.readAndDispatch()) {

display.sleep();

}

}

}

}

class LongRunningOperation extends Thread {

private Display display;

private ProgressBar progressBar;

public LongRunningOperation(Display display, ProgressBar progressBar) {

this.display = display;

this.progressBar = progressBar;

}

public void run() {

//模仿

长光阴

的任务

for (int i = 0; i < 30; i++) {

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

}

display.asyncExec(new Runnable() {

public void run() {

if (progressBar.isDisposed()) return;

//进度条递增

progressBar.setSelection(progressBar.getSelection() + 1);

}

});

}

}

}

以上代码添加了两个进度条,一个进度条为主动显示增加进度的信息(SWT.INDETERMINAT样式),另外一个进度条通过线程处理 长光阴的任务 ,并设定进度条的信息。

作者: ysmxiaoqi

发布时间: 2010-09-06

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值