Unterschiede
Hier werden die Unterschiede zwischen der gewählten und der aktuellen Version gezeigt.
swing:tutorial 2014/05/22 10:56 | swing:tutorial 2020/01/22 20:59 aktuell | ||
---|---|---|---|
Zeile 2: | Zeile 2: | ||
====== Tutorial/Einfürung in Java-Swing ====== | ====== Tutorial/Einfürung in Java-Swing ====== | ||
//javax.swing.*//\\ | //javax.swing.*//\\ | ||
- | Dieses Tutorial soll einen Einführung in Swing geben.\\ | + | ==== Dieses Tutorial soll einen Einführung in Swing geben! ==== |
Vorausetzung für dieses Tutorial sind allgemeine Java-Kenntnisse.\\ | Vorausetzung für dieses Tutorial sind allgemeine Java-Kenntnisse.\\ | ||
Wie sollte es auch anders sein, wir beginnen mit dem brühmten "Hallo World!":\\ | 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> | <code java> | ||
package com.sowas.javawiki.swingtutorial; | package com.sowas.javawiki.swingtutorial; | ||
+ | |||
+ | import javax.swing.JFrame; | ||
+ | import javax.swing.JLabel; | ||
+ | import java.awt.Dimension; | ||
public class SwingTutorial extends JFrame { | public class SwingTutorial extends JFrame { | ||
- | public SwingTutorial() { | + | public SwingTutorial() { |
- | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | + | getContentPane().add(new JLabel("Hallo World!")); |
- | setSize(400, 300); | + | setSize(new Dimension(150, 60)); |
- | setLocationRelativeTo(null); | + | } |
- | + | ||
- | Container cp = getContentPane(); | + | |
- | cp.add(new JLabel("Hallo World!"); | + | |
- | } | + | |
- | public static void main(String[] args) { | + | public static void main(String[] args) { |
- | SwingTutorial swingTutorial = new SwingTutorial(); | + | SwingTutorial swingTutorial = new SwingTutorial(); |
- | swingTutorial.setVisible(true); | + | swingTutorial.setVisible(true); |
} | } | ||
} | } | ||
</code> | </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> | <html> | ||
Zeile 44: | Zeile 52: | ||
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> | src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> | ||
</script> | </script> | ||
- | </html> | + | </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 >>]] | ||