|
1 | 1 | [](https://github.com/eclipse-platform/eclipse.platform.swt/actions/workflows/maven.yml) 
|
2 | 2 |
|
| 3 | +# About |
| 4 | + |
| 5 | +SWT is a cross-platform GUI library for JVM based desktop applications. |
| 6 | +The best known SWT-based application is [Eclipse](https://www.eclipse.org). |
| 7 | + |
| 8 | +## Getting Started |
| 9 | + |
| 10 | +SWT comes with platform-specific jar files. |
| 11 | +Download them from https://download.eclipse.org/eclipse/downloads/index.html and add the jar file to your classpath. |
| 12 | + |
| 13 | +### Example |
| 14 | + |
| 15 | +```java |
| 16 | +import org.eclipse.swt.SWT; |
| 17 | +import org.eclipse.swt.layout.GridData; |
| 18 | +import org.eclipse.swt.layout.GridLayout; |
| 19 | +import org.eclipse.swt.widgets.Button; |
| 20 | +import org.eclipse.swt.widgets.Display; |
| 21 | +import org.eclipse.swt.widgets.Label; |
| 22 | +import org.eclipse.swt.widgets.Shell; |
| 23 | +import org.eclipse.swt.widgets.Text; |
| 24 | + |
| 25 | +public class HelloWorld { |
| 26 | + |
| 27 | + public static void main(String[] args) { |
| 28 | + final Display display = new Display(); |
| 29 | + |
| 30 | + final Shell shell = new Shell(display); |
| 31 | + shell.setText("Hello World"); |
| 32 | + shell.setLayout(new GridLayout(2, false)); |
| 33 | + |
| 34 | + final Label label = new Label(shell, SWT.LEFT); |
| 35 | + label.setText("Your &Name:"); |
| 36 | + label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false)); |
| 37 | + |
| 38 | + final Text text = new Text(shell, SWT.BORDER | SWT.SINGLE); |
| 39 | + final GridData data = new GridData(SWT.FILL, SWT.CENTER, true, false); |
| 40 | + data.minimumWidth = 120; |
| 41 | + text.setLayoutData(data); |
| 42 | + |
| 43 | + final Button button = new Button(shell, SWT.PUSH); |
| 44 | + button.setText("Say Hello"); |
| 45 | + shell.setDefaultButton(button); |
| 46 | + button.setLayoutData(new GridData(SWT.END, SWT.CENTER, false, false, 2, 1)); |
| 47 | + |
| 48 | + final Label output = new Label(shell, SWT.CENTER); |
| 49 | + output.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1)); |
| 50 | + |
| 51 | + button.addListener(SWT.Selection, event -> { |
| 52 | + String name = text.getText().trim(); |
| 53 | + if (name.length() == 0) { |
| 54 | + name = "world"; |
| 55 | + } |
| 56 | + output.setText("Hello " + name + "!"); |
| 57 | + }); |
| 58 | + |
| 59 | + shell.setSize(shell.computeSize(SWT.DEFAULT, SWT.DEFAULT)); |
| 60 | + shell.open(); |
| 61 | + |
| 62 | + while (!shell.isDisposed()) { |
| 63 | + if (!display.readAndDispatch()) { |
| 64 | + display.sleep(); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + display.dispose(); |
| 69 | + } |
| 70 | +} |
| 71 | +``` |
| 72 | +First, a `Display` is created which is something like the central place of all GUI-related code. |
| 73 | +Then a `Shell` is created which in our example is a top-level window. |
| 74 | +Then all child controls and listeners are created, including their layout information. |
| 75 | +Finally, we set the window's size determines by its child controls and open the window. |
| 76 | +The `while`-loop processes all GUI related events until the shell is disposed which happens when closing. |
| 77 | +Before exiting, any claimed GUI resources needs to be freed. |
| 78 | + |
3 | 79 | Contributing to SWT
|
4 | 80 | ===================
|
5 | 81 |
|
|
0 commit comments