Unterschiede
Hier werden die Unterschiede zwischen der gewählten und der aktuellen Version gezeigt.
| swing:tutorial 2014/05/22 10:47 | swing:tutorial 2020/01/22 20:59 aktuell | ||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| - | Dieses Tutorial soll einen Einführung in Swing geben.\\ | + | [[swing:swing|Java - AWT/Swing]]\\ |
| + | ====== Tutorial/Einfürung in Java-Swing ====== | ||
| + | //javax.swing.*//\\ | ||
| + | ==== Dieses Tutorial soll einen Einführung in Swing geben! ==== | ||
| + | Vorausetzung für dieses Tutorial sind allgemeine Java-Kenntnisse.\\ | ||
| + | |||
| + | Wie sollte es auch anders sein, wir beginnen mit dem brühmten "Hallo World!":\\ | ||
| + | Da es sich um eine Swing-Anwendung handeln soll benötigen wir ein Hauptfenster. Das ist in Swing der JFrame.\\ | ||
| + | Dieser verfügt über eine ContentPane, in welcher die GUI-Elemente angeordnet werden.\\ | ||
| + | In unserem Fall fügen wir einfach ein Label (JLabel) mit unserem "Hello world!" hinzu.\\ | ||
| + | \\ | ||
| + | {{:swing:hallo-world.png|}} | ||
| + | \\ | ||
| + | \\ | ||
| + | <code java> | ||
| + | package com.sowas.javawiki.swingtutorial; | ||
| + | |||
| + | import javax.swing.JFrame; | ||
| + | import javax.swing.JLabel; | ||
| + | import java.awt.Dimension; | ||
| + | |||
| + | public class SwingTutorial extends JFrame { | ||
| + | public SwingTutorial() { | ||
| + | getContentPane().add(new JLabel("Hallo World!")); | ||
| + | setSize(new Dimension(150, 60)); | ||
| + | } | ||
| + | |||
| + | public static void main(String[] args) { | ||
| + | SwingTutorial swingTutorial = new SwingTutorial(); | ||
| + | swingTutorial.setVisible(true); | ||
| + | } | ||
| + | } | ||
| + | </code> | ||
| + | Damit bekommen wir tatsächlich das "Hallo World!" auf den Bildschirm, aber nur oben links in der Ecke und schließen kann man es leider auch nicht ;-).\\ | ||
| + | \\ | ||
| + | <html> | ||
| + | <script type="text/javascript"><!-- | ||
| + | google_ad_client="pub-9681858985507948"; | ||
| + | google_ad_width = 468; | ||
| + | google_ad_height = 60; | ||
| + | google_ad_format = "468x60_as"; | ||
| + | google_ad_type = "text"; | ||
| + | google_ad_channel = ""; | ||
| + | google_color_border = "cccccc"; | ||
| + | google_color_bg = "FFFFFF"; | ||
| + | google_color_link = "1d2d8c"; | ||
| + | google_color_text = "000000"; | ||
| + | google_color_url = "1d2d8c"; | ||
| + | //--> | ||
| + | </script> | ||
| + | <script type="text/javascript" | ||
| + | src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> | ||
| + | </script> | ||
| + | </html>\\ | ||
| + | \\ | ||
| + | Darum nun eine zweite verbesserte Variante, welche zentriert auf dem Bildschirm erscheint und auch wieder geschlossen werden kann:\\ | ||
| + | <code java> | ||
| + | package com.sowas.javawiki.swingtutorial; | ||
| + | |||
| + | import javax.swing.JFrame; | ||
| + | import javax.swing.JLabel; | ||
| + | import java.awt.Container; | ||
| + | |||
| + | public class SwingTutorial extends JFrame { | ||
| + | public SwingTutorial() { | ||
| + | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Damit beendet der Exit-Button die Anwendung tatsächlich | ||
| + | setSize(150, 60); | ||
| + | setLocationRelativeTo(null); // Auf dem Bildschirm zentrieren | ||
| + | |||
| + | Container cp = getContentPane(); | ||
| + | cp.add(new JLabel("Hallo World!")); | ||
| + | } | ||
| + | |||
| + | public static void main(String[] args) { | ||
| + | SwingTutorial swingTutorial = new SwingTutorial(); | ||
| + | swingTutorial.setVisible(true); | ||
| + | } | ||
| + | } | ||
| + | </code> | ||
| + | \\ | ||
| + | \\ | ||
| + | [[swing:tutorial-2|Fortsetzung >>]] | ||