1 import org.eclipse.swt.widgets.Display;
2 import org.eclipse.swt.widgets.Shell;
3
4 import java.util.Random;
5
6 import org.eclipse.swt.SWT;
7 import org.eclipse.swt.events.SelectionAdapter;
8 import org.eclipse.swt.events.SelectionEvent;
9 import org.eclipse.swt.layout.GridData;
10 import org.eclipse.swt.layout.GridLayout;
11 import org.eclipse.swt.widgets.Button;
12 import org.eclipse.swt.widgets.Display;
13 import org.eclipse.swt.widgets.Group;
14 import org.eclipse.swt.widgets.Label;
15 import org.eclipse.swt.widgets.ProgressBar;
16 import org.eclipse.swt.widgets.Scale;
17 import org.eclipse.swt.widgets.Shell;
18 import org.eclipse.swt.widgets.Spinner;
19
20
21 public class threadtest{
22 public threadtest thisme=this ;
23 private Spinner spinner_1;
24 private Scale scale;
25 ProgressBar progressBar;
26 private Button button;
27 private Button button_1;
28 private Button button_2;
29 Button button_3;
30 private Button button_4;
31 private Button button_5;
32 private Button button_6;
33 private Button button_7;
34 private Group group;
35 Spinner spinner;
36 private Mythread mythread;
37 protected Shell shell;
38 /**
39 * Launch the application
40 * @param args
41 */
42 public static void main(String[] args) {
43 try {
44 threadtest window = new threadtest();
45 window.open();
46 } catch (Exception e) {
47 e.printStackTrace();
48 }
49 }
50
51 /**
52 * Open the window
53 */
54 public void open() {
55 final Display display = Display.getDefault();
56 createContents();
57 shell.open();
58 shell.layout();
59 while (!shell.isDisposed()) {
60 if (!display.readAndDispatch())
61 display.sleep();
62 }
63 }
64
65 /**
66 * Create contents of the window
67 */
68 protected void createContents() {
69 shell = new Shell();
70 shell.setLayout(new GridLayout());
71 shell.setSize(500, 375);
72 shell.setText("SWT Application");
73 //
74 oo(shell);
75 }
76
77 void oo(Shell shell2) {
78 // TODO 自动生成方法存根
79 group = new Group(shell2, SWT.NONE);
80 group.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
81 // group = new Group(shell, SWT.NONE);
82 final GridLayout gridLayout = new GridLayout();
83 gridLayout.numColumns = 8;
84 group.setLayout(gridLayout);
85 group.setText("线程一");
86 button = new Button(group, SWT.NONE);
87 button.addSelectionListener(new SelectionAdapter() {
88 public void widgetSelected(SelectionEvent arg0) {
89 // System.out.println(spinner_1);
90 mythread = new Mythread(thisme);
91 mythread.start();
92 button.setEnabled(!button.getEnabled());
93 button_2.setEnabled(!button_2.getEnabled());
94 }
95 });
96 button.setText("启动");
97
98 button_2 = new Button(group, SWT.NONE);
99 button_2.setEnabled(false);
100 button_2.setText("停止");
101 button_2.addSelectionListener(new SelectionAdapter() {
102 @SuppressWarnings("deprecation")
103 public void widgetSelected(SelectionEvent arg0) {
104 mythread.stop();
105 button.setEnabled(!button.getEnabled());
106 button_2.setEnabled(!button_2.getEnabled());
107 }
108 });
109
110 button_1 = new Button(group, SWT.NONE);
111 button_1.setEnabled(false);
112 button_1.setText("暂停");
113
114 button_4 = new Button(group, SWT.NONE);
115 button_4.addSelectionListener(new SelectionAdapter() {
116 public void widgetSelected(SelectionEvent arg0) {
117 mythread.sleeptime=spinner_1.getSelection();
118 mythread.sleeptrue=true;
119 System.out.println(spinner_1.getSelection());
120 }
121 });
122 button_4.setText("睡眠");
123
124 button_5 = new Button(group, SWT.NONE);
125 button_5.addSelectionListener(new SelectionAdapter() {
126 public void widgetSelected(SelectionEvent arg0) {
127 mythread.suspend();
128 }
129 });
130 button_5.setText("挂起");
131
132 button_6 = new Button(group, SWT.NONE);
133 button_6.addSelectionListener(new SelectionAdapter() {
134 public void widgetSelected(SelectionEvent arg0) {
135 mythread.resume();
136 }
137 });
138 button_6.setText("恢复");
139
140 spinner = new Spinner(group, SWT.BORDER);
141
142 button_3 = new Button(group, SWT.TOGGLE);
143 button_3.setSelection(true);
144 button_3.setText("随机速度");
145
146 progressBar = new ProgressBar(group, SWT.NONE);
147 progressBar.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 8, 1));
148
149 new Label(group, SWT.NONE).setText("休眠");
150
151 spinner_1 = new Spinner(group, SWT.BORDER);
152 spinner_1.setMaximum(10000);
153 spinner_1.addSelectionListener(new SelectionAdapter() {
154 public void widgetSelected(SelectionEvent arg0) {
155 scale.setSelection(spinner_1.getSelection());
156 }
157 });
158
159 new Label(group, SWT.NONE).setText("毫秒");
160
161 scale = new Scale(group, SWT.NONE);
162 scale.setRedraw(true);
163 scale.setPageIncrement(1000);
164 scale.setIncrement(1000);
165 scale.addSelectionListener(new SelectionAdapter() {
166 public void widgetSelected(SelectionEvent arg0) {
167 spinner_1.setSelection(scale.getSelection());
168 }
169 });
170 scale.setMaximum(10000);
171 scale.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 5, 1));
172
173 }
174
175 public void sd(int ss) {
176 mythread.sd=spinner.getSelection();
177 progressBar.setSelection(ss++);
178 }
179
180 public void sd() {
181 if(button_3.getSelection()){
182 spinner.setSelection((int) (new Random().nextDouble()*100));
183 }
184 }
185
186 }
1 import org.eclipse.swt.widgets.Display;
2 import org.eclipse.swt.widgets.Shell;
3
4 import java.util.Random;
5
6 import org.eclipse.swt.events.SelectionAdapter;
7 import org.eclipse.swt.widgets.Button;
8 import org.eclipse.swt.widgets.Display;
9 import org.eclipse.swt.widgets.Group;
10 import org.eclipse.swt.widgets.ProgressBar;
11 import org.eclipse.swt.widgets.Scale;
12 import org.eclipse.swt.widgets.Shell;
13 import org.eclipse.swt.widgets.Spinner;
14
15
16 class Mythread extends Thread{
17 int sd=0;
18 private int i=0;
19 public threadtest thisme;
20 int sleeptime=0;
21 boolean taskrun=true;
22 public boolean sleeptrue=false;
23
24
25
26 public Mythread(threadtest thisme) {
27 this.thisme=thisme;
28 }
29 public void run() {
30 while (true) {
31 if(i>100|i==0){
32 i=0;
33 sd1();
34 }
35 i++;
36 if(sleeptrue){
37 try {
38 Thread.sleep(sleeptime);
39 sleeptrue=false;
40 } catch (InterruptedException e) {
41 e.printStackTrace();
42 }
43
44 }
45 sds();
46 try {
47 Thread.sleep(100-sd);
48 } catch (InterruptedException e) {
49 e.printStackTrace();
50 }
51 }
52
53
54 }
55 private void sd1() {
56 Display.getDefault().syncExec(new Runnable() {
57 public void run() {
58 thisme.sd();
59 }
60 });
61 }
62 private void sds() {
63 Display.getDefault().syncExec(new Runnable() {
64 public void run() {
65 // System.out.println(i);
66 thisme.sd(i);
67 }
68 });
69 }
70
71 }
1 import org.eclipse.swt.widgets.Display;
2 import org.eclipse.swt.widgets.Shell;
3
4 import org.eclipse.swt.SWT;
5 import org.eclipse.swt.events.SelectionAdapter;
6 import org.eclipse.swt.events.SelectionEvent;
7 import org.eclipse.swt.layout.GridData;
8 import org.eclipse.swt.layout.GridLayout;
9 import org.eclipse.swt.widgets.Button;
10 import org.eclipse.swt.widgets.Display;
11 import org.eclipse.swt.widgets.Group;
12 import org.eclipse.swt.widgets.Label;
13 import org.eclipse.swt.widgets.ProgressBar;
14 import org.eclipse.swt.widgets.Scale;
15 import org.eclipse.swt.widgets.Shell;
16 import org.eclipse.swt.widgets.Spinner;
17
18
19 public class thread {
20
21 private Spinner spinner_1;
22 private Scale scale;
23 private ProgressBar progressBar;
24 public Button button;
25 public Button button_1;
26 public Button button_2;
27 public Button button_3;
28 public Button button_4;
29 public Button button_5;
30 public Button button_6;
31 public Button button_7;
32 protected Shell shell;
33 public Group group;
34 public Spinner spinner;
35 // Mythread mythread;
36 /**
37 * Launch the application
38 * @param args
39 */
40 public static void main(String[] args) {
41 try {
42 thread window = new thread();
43 window.open();
44 } catch (Exception e) {
45 e.printStackTrace();
46 }
47 }
48
49 /**
50 * Open the window
51 */
52 public void open() {
53 final Display display = Display.getDefault();
54 createContents();
55 shell.open();
56 shell.layout();
57 while (!shell.isDisposed()) {
58 if (!display.readAndDispatch())
59 display.sleep();
60 }
61 }
62
63 /**
64 * Create contents of the window
65 */
66 protected void createContents() {
67 shell = new Shell();
68 shell.setLayout(new GridLayout());
69 shell.setSize(500, 375);
70 shell.setText("SWT Application");
71 threadtest[] tt= new threadtest[6];
72 for (int i = 0; i < tt.length; i++) {
73 tt[i]=new threadtest();
74 }
75 for (int i = 0; i < tt.length; i++) {
76 tt[i].oo(shell);
77 }
78 }
79
80 }