📄 progressbar2.java
字号:
package cn.com.chengang.swt;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
public class ProgressBar2 {
public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setSize(327, 253);
// ---------创建窗口中的其他界面组件-------------
shell.setLayout(new RowLayout());
// 创建一个滑动条。这里用平滑型式样SWT.SMOOTH,默认是方格型
final ProgressBar progressBar = new ProgressBar(shell, SWT.SMOOTH);
progressBar.setMinimum(0); // 最小值
progressBar.setMaximum(100);// 最大值
// 创建一个GO按钮
Button button = new Button(shell, SWT.NONE);
button.setText("GO");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
new Thread() {
public void run() {
for (int i = 1; i < 11; i++) {// 每次循环进度条前进一格,同时停顿一秒
final int i_copy = i; // 匿名内部类访问的局域变量必须是final型的,所以中转一下
display.asyncExec(new Runnable() {
public void run() {
progressBar.setSelection(i_copy * 10);
}
});
try {Thread.sleep(1000);} catch (Throwable e2) {} // 间隔一秒
}
}
}.start();
}
});
// -----------------END------------------------
shell.layout();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -