110 likes | 163 Views
Beispiele zum Vortrag JavaSWTSS06. Start-Klasse. public class start { public static void main(String[] args) { new beispiel1(); } }. Beispiel 1. import org.eclipse.swt.*; import org.eclipse.swt.widgets.*; public class beispiel1 { beispiel1() {
E N D
Start-Klasse public class start { public static void main(String[] args) { new beispiel1(); } }
Beispiel 1 import org.eclipse.swt.*; import org.eclipse.swt.widgets.*; public class beispiel1 { beispiel1() { //Initialisieren des Displays und der Shell Display display = new Display(); Shell shell = new Shell(display); shell.setSize(300,300); shell.setText("SWT Beispiel 1"); //Haupt-Schleife shell.open(); while(!shell.isDisposed()){ if(!display.readAndDispatch()) display.sleep(); } display.dispose(); } }
Beispiel 2 import org.eclipse.swt.*; import org.eclipse.swt.widgets.*; public class beispiel2 { beispiel2() { Display display = new Display(); Shell shell = new Shell(display); shell.setSize(300,300); shell.setText("SWT Beispiel 2"); //Hinzufügen eines Labels Label label1 = new Label(shell, SWT.NONE); label1.setText("Ein Label"); label1.setSize(200,20); label1.setLocation(30,30); //Hinzufügen eines Textfelds Text text1 = new Text(shell, SWT.BORDER); text1.setTextLimit(30); text1.setText("Ein Textfeld"); text1.setBounds(30,60,200,90); text1.setTextLimit(30);
//Hinzufügen einer Combobox Combo combo1 = new Combo(shell,SWT.DROP_DOWN | SWT.READ_ONLY); combo1.setItems(new String[] {"Eins","Zwei","Drei"}); combo1.select(1); combo1.setLocation(30,160); combo1.setSize(200,20); //Hinzufügen eines Buttons Button button1 = new Button(shell,SWT.PUSH); button1.setText("Ein Button"); button1.setLocation(30,220); button1.setSize(100,20); shell.open(); while(!shell.isDisposed()){ if(!display.readAndDispatch()) display.sleep(); } display.dispose(); } }
Beispiel 3 import org.eclipse.swt.*; import org.eclipse.swt.widgets.*; import org.eclipse.swt.browser.Browser; import org.eclipse.swt.layout.FillLayout; public class beispiel3 { //Ein einfacher Browser beispiel3() { Display display = new Display(); Shell shell = new Shell(display); shell.setSize(900,500); shell.setText("SWT Beispiel 4"); shell.setLayout(new FillLayout()); // FillLyaout verwenden //Browser initialisieren Browser browser = new Browser(shell, SWT.BORDER); browser.setSize(800, 400); browser.setUrl("http://www.google.de"); shell.open(); while(!shell.isDisposed()){ if(!display.readAndDispatch()) display.sleep(); } display.dispose(); } }
Beispiel 4 import org.eclipse.swt.*; import org.eclipse.swt.widgets.*; import org.eclipse.swt.events.*; //Für die Listener public class beispiel4 { Text text1; Combo combo1; beispiel4() { Display display = new Display(); final Shell shell = new Shell(display); shell.setSize(300,300); shell.setText("SWT Beispiel 3"); Label label1 = new Label(shell, SWT.NONE); label1.setText("Bitte einen Text eingeben:"); label1.setSize(200,20); label1.setLocation(30,30); Label seperator = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL); seperator.setBounds(30,60,200,20);
text1 = new Text(shell, SWT.BORDER); text1.setTextLimit(30); text1.setText("Ihr Text..."); text1.setBounds(30,90,200,60); combo1 = new Combo(shell,SWT.DROP_DOWN | SWT.READ_ONLY); combo1.setItems(new String[] {"Eintrag eins","Eintrag zwei","Eintrag drei"}); combo1.select(1); combo1.setLocation(30,160); combo1.setSize(200,20); //Hinzufügen eines Menüs //Menüleiste Menu menu = new Menu(shell, SWT.BAR); shell.setMenuBar(menu); //Datei-Menü Menu filemenu = new Menu(shell, SWT.DROP_DOWN); MenuItem mi = new MenuItem(menu, SWT.CASCADE); mi.setText("Datei"); mi.setMenu(filemenu); //Menüeintrag hinzufügen MenuItem me = new MenuItem(filemenu, SWT.PUSH); me.setText("Datei öffnen..."); //Filechooser final FileDialog fileChooser = new FileDialog(shell, SWT.OPEN|SWT.MULTI); fileChooser.setText("Open movie files"); fileChooser.setFilterExtensions(new String[] { "*.avi; *.mov; *.mpg", "*.*"}); fileChooser.setFilterNames(new String[] {"movie files (*.avi; *.mov; *.mpg)", "all (*.*)"});
//Listener für das Menü SelectionListener menulistener = new SelectionListener(){ public void widgetSelected(SelectionEvent e) { String filename = fileChooser.open(); text1.setText(filename); } public void widgetDefaultSelected(SelectionEvent e){} }; me.addSelectionListener(menulistener); Button button1 = new Button(shell,SWT.PUSH); button1.setText("Dialog anzeigen"); button1.setLocation(30,220); button1.setSize(100,20); //Listener für den Button button1.addSelectionListener(new SelectionListener(){ public void widgetSelected(SelectionEvent e) { showMessageBox(shell); } public void widgetDefaultSelected(SelectionEvent e) {} }); shell.open(); while(!shell.isDisposed()){ if(!display.readAndDispatch()) display.sleep(); } display.dispose(); }
public void showMessageBox(Shell shell) { MessageBox mb = new MessageBox(shell, SWT.ICON_INFORMATION); mb.setMessage(text1.getText()); mb.setText("Auswahl war:" + combo1.getText()); mb.open(); } }