17
Jan
2011
Leeres Fenster
- package A1l2;
-
- import org.eclipse.swt.widgets.Display;
- import org.eclipse.swt.widgets.Shell;
-
- public class Example {
-
- Display display;
- Shell shell;
-
- public Example(Display display) {
- this.display = display;
- shell = new Shell(display);
- shell.setText("Aufgabe 1.2");
- }
-
- private void createGUI() {
-
- }
-
- private void run() {
- createGUI();
- shell.open();
- while(!shell.isDisposed()) {
- if(!display.readAndDispatch()) {
- display.sleep();
- }
- }
- display.dispose();
- }
-
- new Example(Display.getDefault()).run();
- }
- }
Einfaches Thread Beispiel:
- package A1l2;
-
- import org.eclipse.swt.SWT;
- import org.eclipse.swt.layout.FillLayout;
- import org.eclipse.swt.widgets.Button;
- import org.eclipse.swt.widgets.Display;
- import org.eclipse.swt.widgets.Shell;
-
- public class Example {
-
- Display display;
- Shell shell;
-
- public Example(Display display) {
- this.display = display;
- shell = new Shell(display);
- shell.setText("Aufgabe 1.2");
- }
-
- private void createGUI() {
-
- FillLayout fillLayout = new FillLayout();
- fillLayout.type = SWT.VERTICAL;
- shell.setLayout(fillLayout);
- Messenger newThread = new Messenger(display, button);
- newThread.start();
-
- shell.pack();
- }
-
- private void run() {
- createGUI();
- shell.open();
- while(!shell.isDisposed()) {
- if(!display.readAndDispatch()) {
- display.sleep();
- }
- }
- display.dispose();
- }
-
- new Example(Display.getDefault()).run();
- }
- }
- package A1l2;
-
- import org.eclipse.swt.widgets.Button;
- import org.eclipse.swt.widgets.Display;
-
-
- private int titleCounter = 0;
- private Display display;
- private boolean runnable = true;
-
- this.display = display;
- this.button = button;
- }
-
- public void run() {
- while(runnable) {
- try {
- sleep(1000);
- if(!display.isDisposed()) {
- uiThread();
- } else {
- runnable = false;
- }
- }
- }
-
- private void uiThread() {
- public void run() {
- titleCounter = titleCounter == 10 ? 0 : titleCounter + 1;
- button.setText("Timer: "+titleCounter);
- }
- });
- }
-
- }