今天學習了下
eclipse
從入門到精通的
SWT
多線程,但是一直有問題,我感覺現在完全是按照那里面的代碼寫的怎么還是出錯
org.eclipse.swt.SWTException
: Failed to execute runnable (java.lang.StackOverflowError)
??? at org.eclipse.swt.SWT.error(
SWT.java:2942
)
??? at org.eclipse.swt.SWT.error(
SWT.java:2865
)
??? at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(
Synchronizer.java:126
)
??? at org.eclipse.swt.widgets.Display.runAsyncMessages(
Display.java:3057
)
??? at org.eclipse.swt.widgets.Display.readAndDispatch(
Display.java:2716
)
??? at com.swtprocessmonitor.TaskGUI.open(
TaskGUI.java:77
)
?????? while (!shell.isDisposed()) {
?????????? if (!display.readAndDispatch())//77
????????????? display.sleep();}
??? at com.swtprocessmonitor.TaskGUI.main(
TaskGUI.java:87
)
Caused by: java.lang.StackOverflowError
org.eclipse.swt.SWTException
: Failed to execute runnable (java.lang.StackOverflowError)
??? at org.eclipse.swt.SWT.error(
SWT.java:2942
)
??? at org.eclipse.swt.SWT.error(
SWT.java:2865
)
??? at org.eclipse.swt.widgets.Synchronizer.syncExec(
Synchronizer.java:178
)
??? at org.eclipse.swt.widgets.Display.syncExec(
Display.java:3413
)
??? at com.swtprocessmonitor.Task.moveProgressBar(
Task.java:68
)
??? at com.swtprocessmonitor.Task.start(
Task.java:32
)
??? at com.swtprocessmonitor.TaskGUI$3.run(
TaskGUI.java:59
)
Caused by: java.lang.StackOverflowError
不知道該專門解決了
我的 TaskGUI.java
package com.swtprocessmonitor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class TaskGUI {
??????
??????
?????? final Display display = Display.getDefault();
?????? final Shell shell = new Shell();
?????? private Task task=new Task(this);
?????? private Button Execue_button = new Button(shell, SWT.NONE);
?????? private Button stop_Button = new Button(shell, SWT.NONE);
?????? private ProgressBar progressBar;
?????? private Text taskCountText;
?????? private Text consoletext;
?????? public void open()
?????? {
????????????? shell.setSize(500, 375);
????????????? shell.setText("SWT Application");
????????????? shell.open();
?????????????
????????????? Execue_button.setText("Execue");
????????????? Execue_button.setBounds(211, 52, 101, 25);
????????????? stop_Button.setText("stop");
????????????? stop_Button.setBounds(348, 52, 79, 25);
????????????? stop_Button.addSelectionListener(new SelectionAdapter() {
???????????????????? public void widgetSelected(SelectionEvent e) {
??????????????????????????? task.stop();
???????????????????? }
????????????? });
????????????? consoletext = new Text(shell, SWT.V_SCROLL | SWT.MULTI | SWT.BORDER);
????????????? //consoletext.setLayoutData();
????????????? consoletext.setBounds(93, 141, 277, 156);
????????????? Execue_button.addSelectionListener(new SelectionAdapter() {
???????????????????? public void widgetSelected(SelectionEvent e) {
???????????????????????????
?setButtonState(false);
???????????????????????????
?String? str=taskCountText.getText();
???????????????????????????
?final int inttaskCount=Integer.parseInt(str);
???????????????????????????
?System.out.println(inttaskCount+"inttaskCount");
???????????????????????????
?progressBar.setMaximum(inttaskCount-1);
???????????????????????????
?
???????????????????????????
?consoletext.insert("back door thread start!");
???????????????????????????
?new Thread()? {
?????????????????????????????????? public void run() {
?????????????????????????????????????????
????????????????????????????????????????? task.start(inttaskCount);
?????????????????????????????????? }
??????????????????????????? }.start();
???????????????????????????
?
???????????????????????????
????????????????????
??????
???????????????????? }
????????????? });
?????????????
?progressBar = new ProgressBar(shell, SWT.NONE);
????????????? progressBar.setBounds(89, 105, 276, 30);
????????????? taskCountText = new Text(getShell(), SWT.BORDER);
????????????? taskCountText.setBounds(72, 50, 120, 30);
????????????? shell.layout();
?????????????
????????????? shell.setLocation(6, -9);
????????????? while (!shell.isDisposed()) {
???????????????????? if (!display.readAndDispatch())
??????????????????????????? display.sleep();}
?????????????
?????? }
??????
??????
?????? public static void main(String[] args) {
????????????? try {
???????????????????? TaskGUI tg=new TaskGUI();
???????????????????? tg.open();
????????????? } catch (RuntimeException e) {
???????????????????? // TODO Auto-generated catch block
???????????????????? e.printStackTrace();
????????????? }
??????
?????? }
?????? public void? setButtonState(boolean bFlag)
?????? {
????????????? this.Execue_button.setEnabled(bFlag);
????????????? this.stop_Button.setEnabled(!bFlag);
?????? }
??????
?????? public Shell getShell()
?????? {
????????????? return shell;
?????? }
?????? public Text getConselText()
?????? {
????????????? return this.consoletext;
?????? }
?????? public ProgressBar getProgressBar()
?????? {
????????????? return this.getProgressBar();
?????? }
}
我的 Task.java
package com.swtprocessmonitor;
import org.eclipse.swt.widgets.Display;
public class Task {
?????? private TaskGUI tg;
?????? private boolean stopFlag;
?????? Task(TaskGUI tg)
?????? {
????????????? this.tg=tg;
?????? }
?????? public void stop()
?????? {
????????????? stopFlag=true;
?????? }
?????? public void start(int taskCount)
?????? {
????????????? stopFlag=false;
????????????? insertConselText("stat!");
????????????? for(int i=0;i<taskCount;i++)
????????????? {
???????????????????? if(stopFlag)
???????????????????? {
??????????????????????????? break;
???????????????????? }
???????????????????? try {
??????????????????????????? Thread.sleep(1000);
???????????????????? } catch (InterruptedException e) {
??????????????????????????? // TODO Auto-generated catch block
??????????????????????????? e.printStackTrace();
???????????????????? }
????????????????????
???????????????????? insertConselText(i+"");
???????????????????? moveProgressBar(i);
????????????????????
????????????????????
????????????? }
?????????????
????????????? insertConselText("finish");
????????????? setTaskButtonstate(true);
?????????????
?????????????
?????? }
?????? public void setTaskButtonstate(final boolean bFlag)
??????
?????? {
????????????? Display.getDefault().syncExec(new Runnable(){
???????????????????? public void run()
???????????????????? {
??????????????????????????? tg.setButtonState(bFlag);
???????????????????? }
????????????????????
????????????? });
?????? }
?????? public void insertConselText(final String str)
??????
?????? {
????????????? Display.getDefault().syncExec(new Runnable(){
???????????????????? public void run()
???????????????????? {
??????????????????????????? tg.getConselText().insert(str);
???????????????????? }
????????????????????
????????????? });
?????? }
??????
?????? public void moveProgressBar(final int progress)
??????
?????? {
????????????? Display.getDefault().syncExec(new Runnable(){
???????????????????? public void run()
???????????????????? {
??????????????????????????? tg.getProgressBar().setSelection(progress);
???????????????????? }
????????????????????
????????????? });
?????? }
}