62,614
社区成员
发帖
与我相关
我的任务
分享
package test;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.ProgressBar;
public class Bar extends Shell {
/**
* Launch the application.
* @param args
*/
public static void main(String args[]) {
try {
Display display = Display.getDefault();
Bar shell = new Bar(display);
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the shell.
* @param display
*/
public Bar(final Display display) {
super(display, SWT.SHELL_TRIM);
final ProgressBar progressBar = new ProgressBar(this, SWT.NONE);//HORIZONTAL | SWT.INDETERMINATE);
progressBar.setBounds(119, 34, 170, 17);
progressBar.setVisible(false);
Button btnNewButton = new Button(this, SWT.NONE);
btnNewButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
progressBar.setVisible(true);
try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
new Thread(new Sleep1(display)).start();
progressBar.setVisible(false);
}
});
btnNewButton.setBounds(169, 82, 72, 22);
btnNewButton.setText("New Button");
createContents();
}
/**
* Create contents of the shell.
*/
protected void createContents() {
setText("SWT Application");
setSize(450, 300);
}
@Override
protected void checkSubclass() {
// Disable the check that prevents subclassing of SWT components
}
}
public class Sleep1 implements Runnable{
private Display display;
public Sleep1(Display display) {
this.display=display;
}
@Override
public void run() {
display.syncExec(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}});
}
}
public class Bar extends Shell {
/**
* Launch the application.
*
* @param args
*/
public static void main(String args[]) {
try {
Display display = Display.getDefault();
Bar shell = new Bar(display);
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the shell.
*
* @param display
*/
public Bar(final Display display) {
super(display, SWT.SHELL_TRIM);
final ProgressBar progressBar = new ProgressBar(this, SWT.NONE);
progressBar.setBounds(119, 34, 170, 17);
progressBar.setVisible(false);
Button btnNewButton = new Button(this, SWT.NONE);
btnNewButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// progressBar.setVisible(true);
new Thread(new Sleep1(display, progressBar)).start();
}
});
btnNewButton.setBounds(169, 82, 72, 22);
btnNewButton.setText("New Button");
createContents();
}
/**
* Create contents of the shell.
*/
protected void createContents() {
setText("SWT Application");
setSize(450, 300);
}
@Override
protected void checkSubclass() {
// Disable the check that prevents subclassing of SWT components
}
public class Sleep1 implements Runnable {
private Display display;
private ProgressBar progressBar;
public Sleep1(Display display, ProgressBar progressBar) {
this.display = display;
this.progressBar = progressBar;
}
@Override
public void run() {
display.syncExec(new Runnable() {
@Override
public void run() {
progressBar.setVisible(true); // 这句也可以放在外面, 可以省代码
}
});
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
display.syncExec(new Runnable() {
@Override
public void run() {
progressBar.setVisible(false);
}
});
}
}
}
package test;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.ProgressBar;
public class Bar extends Shell {
/**
* Launch the application.
* @param args
*/
public static void main(String args[]) {
try {
Display display = Display.getDefault();
Bar shell = new Bar(display);
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the shell.
* @param display
*/
public Bar(final Display display) {
super(display, SWT.SHELL_TRIM);
final ProgressBar progressBar = new ProgressBar(this, SWT.NONE);//HORIZONTAL | SWT.INDETERMINATE);
progressBar.setBounds(119, 34, 170, 17);
progressBar.setVisible(false);
Button btnNewButton = new Button(this, SWT.NONE);
btnNewButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
progressBar.setVisible(true);
new Thread(new Sleep1(display)).start();
progressBar.setVisible(false);
}
});
btnNewButton.setBounds(169, 82, 72, 22);
btnNewButton.setText("New Button");
createContents();
}
/**
* Create contents of the shell.
*/
protected void createContents() {
setText("SWT Application");
setSize(450, 300);
}
@Override
protected void checkSubclass() {
// Disable the check that prevents subclassing of SWT components
}
}