info:sek-ii:e2:java-grundlagen:l12-gui
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Beide Seiten der vorigen RevisionVorhergehende ÜberarbeitungNächste Überarbeitung | Vorhergehende Überarbeitung | ||
info:sek-ii:e2:java-grundlagen:l12-gui [2021-06-17 12:38] – [BetterBox#8] christian.weber | info:sek-ii:e2:java-grundlagen:l12-gui [2021-06-23 18:52] (aktuell) – [BetterBox#13] christian.weber | ||
---|---|---|---|
Zeile 15: | Zeile 15: | ||
Erstelle mit dem JavaEditor einen neuen JFrame. | Erstelle mit dem JavaEditor einen neuen JFrame. | ||
- | {{ : | + | {{ : |
</ | </ | ||
<bbox w2|**Schritt 2:**> | <bbox w2|**Schritt 2:**> | ||
Zeile 21: | Zeile 21: | ||
In der oberen Leiste findest du die Steuerelemente, | In der oberen Leiste findest du die Steuerelemente, | ||
- | {{ : | + | {{ : |
</ | </ | ||
<bbox w2|**Schritt 3:**> | <bbox w2|**Schritt 3:**> | ||
Zeile 27: | Zeile 27: | ||
Erstelle das folgende Formular, indem du die passenden Steuerelemente von der Palette auf das Formular ziehst. | Erstelle das folgende Formular, indem du die passenden Steuerelemente von der Palette auf das Formular ziehst. | ||
- | {{ : | + | {{ : |
</ | </ | ||
<bbox w2|**Schritt 4:**> | <bbox w2|**Schritt 4:**> | ||
Zeile 33: | Zeile 33: | ||
Verändere die Eigenschaften (Attribute) der einzelnen Steuerelemente, | Verändere die Eigenschaften (Attribute) der einzelnen Steuerelemente, | ||
- | {{ : | + | {{ : |
</ | </ | ||
<bbox w1|**Schritt 5:**> | <bbox w1|**Schritt 5:**> | ||
Zeile 53: | Zeile 53: | ||
Den ganzen Code drum herum ignorieren wir erst einmal. Am Ende sollte es so aussehen: | Den ganzen Code drum herum ignorieren wir erst einmal. Am Ende sollte es so aussehen: | ||
- | {{ : | + | {{ : |
- | Führe das Programm mit einem Klick auf den " | + | Führe das Programm mit einem Klick auf den " |
</ | </ | ||
</ | </ | ||
+ | </ | ||
+ | <lösung w1 collapsed|**Lösungen zu Aufgabe 1:**> | ||
+ | <code text HalloHorst.jfm> | ||
+ | object HalloHorst: TFGUIForm | ||
+ | Left = 672 | ||
+ | Top = 440 | ||
+ | BorderIcons = [biSystemMenu] | ||
+ | Caption = ' | ||
+ | ClientHeight = 65 | ||
+ | ClientWidth = 225 | ||
+ | Color = clBtnFace | ||
+ | ParentFont = True | ||
+ | FormStyle = fsStayOnTop | ||
+ | OldCreateOrder = True | ||
+ | Position = poDesigned | ||
+ | Visible = True | ||
+ | OnClose = FormClose | ||
+ | OnCloseQuery = FormCloseQuery | ||
+ | OnResize = FormResize | ||
+ | FrameType = 5 | ||
+ | Resizable = False | ||
+ | Undecorated = False | ||
+ | Background = clBtnFace | ||
+ | PixelsPerInch = 96 | ||
+ | TextHeight = 13 | ||
+ | object lName: TJLabel | ||
+ | Tag = 1 | ||
+ | Left = 8 | ||
+ | Top = 8 | ||
+ | Width = 46 | ||
+ | Height = 20 | ||
+ | Hint = ' | ||
+ | Font.Charset = DEFAULT_CHARSET | ||
+ | Font.Color = clWindowText | ||
+ | Font.Height = -12 | ||
+ | Font.Name = ' | ||
+ | Font.Style = [fsBold] | ||
+ | Foreground = 3355443 | ||
+ | Background = clBtnFace | ||
+ | Text = ' | ||
+ | HorizontalAlignment = LEFT | ||
+ | VerticalAlignment = CENTER | ||
+ | HorizontalTextPosition = RIGHT | ||
+ | VerticalTextPosition = CENTER | ||
+ | IconTextGap = 4 | ||
+ | DisplayedMnemonic = 0 | ||
+ | DisplayedMnemonicIndex = 0 | ||
+ | end | ||
+ | object jTextField1: | ||
+ | Tag = 2 | ||
+ | Left = 64 | ||
+ | Top = 8 | ||
+ | Width = 150 | ||
+ | Height = 20 | ||
+ | Cursor = crIBeam | ||
+ | Hint = ' | ||
+ | Font.Charset = DEFAULT_CHARSET | ||
+ | Font.Color = clWindowText | ||
+ | Font.Height = -12 | ||
+ | Font.Name = ' | ||
+ | Font.Style = [] | ||
+ | Foreground = 3355443 | ||
+ | Background = clWhite | ||
+ | HorizontalAlignment = LEFT | ||
+ | Text = '' | ||
+ | Editable = True | ||
+ | end | ||
+ | object bSageHallo: TJButton | ||
+ | Tag = 4 | ||
+ | Left = 8 | ||
+ | Top = 32 | ||
+ | Width = 211 | ||
+ | Height = 25 | ||
+ | Hint = ' | ||
+ | Font.Charset = DEFAULT_CHARSET | ||
+ | Font.Color = clWindowText | ||
+ | Font.Height = -12 | ||
+ | Font.Name = ' | ||
+ | Font.Style = [fsBold] | ||
+ | Foreground = 3355443 | ||
+ | Background = 15658734 | ||
+ | actionPerformed = ' | ||
+ | Text = 'Sage Hallo!' | ||
+ | Mnemonic = 0 | ||
+ | DisplayedMnemonicIndex = 0 | ||
+ | Selected = False | ||
+ | BorderPainted = True | ||
+ | FocusPainted = False | ||
+ | ContentAreaFilled = True | ||
+ | HorizontalAlignment = CENTER | ||
+ | VerticalAlignment = CENTER | ||
+ | HorizontalTextPosition = RIGHT | ||
+ | VerticalTextPosition = CENTER | ||
+ | IconTextGap = 4 | ||
+ | RolloverEnabled = False | ||
+ | Border.BorderType = NoBorder | ||
+ | Border.LineColor = clBlack | ||
+ | Border.LineThickness = 0 | ||
+ | Border.LineRounded = False | ||
+ | Border.EtchHighlightColor = clBlack | ||
+ | Border.EtchShadowColor = clBlack | ||
+ | Border.Etchtype = 0 | ||
+ | Border.BevelHighlightColor = clBlack | ||
+ | Border.BevelShadowColor = clBlack | ||
+ | Border.Beveltype = 0 | ||
+ | Border.MatteColor = clBlack | ||
+ | Border.MatteTop = 0 | ||
+ | Border.MatteLeft = 0 | ||
+ | Border.MatteBottom = 0 | ||
+ | Border.MatteRight = 0 | ||
+ | end | ||
+ | end | ||
+ | </ | ||
+ | <code java HalloHorst.java> | ||
+ | import java.awt.*; | ||
+ | import java.awt.event.*; | ||
+ | import javax.swing.*; | ||
+ | import javax.swing.event.*; | ||
+ | |||
+ | /** | ||
+ | * | ||
+ | * Beschreibung | ||
+ | * | ||
+ | * @version 1.0 vom 17.06.2021 | ||
+ | * @author | ||
+ | */ | ||
+ | |||
+ | public class HalloHorst extends JFrame { | ||
+ | // Anfang Attribute | ||
+ | private JLabel lName = new JLabel(); | ||
+ | private JTextField jTextField1 = new JTextField(); | ||
+ | private JButton bSageHallo = new JButton(); | ||
+ | // Ende Attribute | ||
+ | | ||
+ | public HalloHorst() { | ||
+ | // Frame-Initialisierung | ||
+ | super(); | ||
+ | setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); | ||
+ | int frameWidth = 241; | ||
+ | int frameHeight = 104; | ||
+ | setSize(frameWidth, | ||
+ | Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); | ||
+ | int x = (d.width - getSize().width) / 2; | ||
+ | int y = (d.height - getSize().height) / 2; | ||
+ | setLocation(x, | ||
+ | setTitle(" | ||
+ | setResizable(false); | ||
+ | Container cp = getContentPane(); | ||
+ | cp.setLayout(null); | ||
+ | // Anfang Komponenten | ||
+ | | ||
+ | lName.setBounds(8, | ||
+ | lName.setText(" | ||
+ | cp.add(lName); | ||
+ | jTextField1.setBounds(64, | ||
+ | cp.add(jTextField1); | ||
+ | bSageHallo.setBounds(8, | ||
+ | bSageHallo.setText(" | ||
+ | bSageHallo.setMargin(new Insets(2, 2, 2, 2)); | ||
+ | bSageHallo.addActionListener(new ActionListener() { | ||
+ | public void actionPerformed(ActionEvent evt) { | ||
+ | bSageHallo_ActionPerformed(evt); | ||
+ | } | ||
+ | }); | ||
+ | cp.add(bSageHallo); | ||
+ | // Ende Komponenten | ||
+ | | ||
+ | setVisible(true); | ||
+ | } // end of public HalloHorst | ||
+ | | ||
+ | // Anfang Methoden | ||
+ | | ||
+ | public static void main(String[] args) { | ||
+ | new HalloHorst(); | ||
+ | } // end of main | ||
+ | | ||
+ | public void bSageHallo_ActionPerformed(ActionEvent evt) { | ||
+ | // TODO hier Quelltext einfügen | ||
+ | JOptionPane.showMessageDialog(this, | ||
+ | | ||
+ | } // end of bSageHallo_ActionPerformed | ||
+ | |||
+ | // Ende Methoden | ||
+ | } // end of class HalloHorst | ||
+ | </ | ||
+ | </ | ||
+ | <aufgabe w1|**Aufgabe 2:**> | ||
+ | |||
+ | - Erstelle den folgenden JFrame: {{ : | ||
+ | - Vor der '' | ||
+ | static int zufallszahl = 0; | ||
+ | | ||
+ | public static void main(String[] args) { | ||
+ | new Zahlenraten(); | ||
+ | |||
+ | zufallszahl = (int)Math.floor(Math.random() * 100 + 1); | ||
+ | } // end of main | ||
+ | </ | ||
+ | - Wenn der Raten-Button angeklickt wird, soll mit <code java>int geraten = Integer.parseInt(jTextField1.getText());</ | ||
+ | * Wenn die geratene Zahl zu hoch ist, soll als Nachricht "Zu hoch geraten" | ||
+ | * Wenn die geratene Zahl zu niedrig ist, soll als Nachricht "Zu niedrig geraten" | ||
+ | * Wenn richtig geraten wurde, soll als Nachricht " | ||
+ | </ | ||
+ | <lösung collapsed w1|**Lösung zu Aufgabe 2**> | ||
+ | <code text Zahlenraten.jfm> | ||
+ | object Zahlenraten: | ||
+ | Left = 913 | ||
+ | Top = 253 | ||
+ | BorderIcons = [biSystemMenu] | ||
+ | Caption = ' | ||
+ | ClientHeight = 73 | ||
+ | ClientWidth = 233 | ||
+ | Color = clBtnFace | ||
+ | ParentFont = True | ||
+ | FormStyle = fsStayOnTop | ||
+ | OldCreateOrder = True | ||
+ | Position = poDesigned | ||
+ | ShowHint = True | ||
+ | Visible = True | ||
+ | OnClose = FormClose | ||
+ | OnCloseQuery = FormCloseQuery | ||
+ | OnResize = FormResize | ||
+ | FrameType = 5 | ||
+ | Resizable = False | ||
+ | Undecorated = False | ||
+ | Background = clBtnFace | ||
+ | PixelsPerInch = 96 | ||
+ | TextHeight = 13 | ||
+ | object lZahl: TJLabel | ||
+ | Tag = 1 | ||
+ | Left = 8 | ||
+ | Top = 8 | ||
+ | Width = 62 | ||
+ | Height = 20 | ||
+ | Hint = ' | ||
+ | Font.Charset = DEFAULT_CHARSET | ||
+ | Font.Color = clWindowText | ||
+ | Font.Height = -12 | ||
+ | Font.Name = ' | ||
+ | Font.Style = [fsBold] | ||
+ | Foreground = 3355443 | ||
+ | Background = clBtnFace | ||
+ | Text = ' | ||
+ | HorizontalAlignment = LEFT | ||
+ | VerticalAlignment = CENTER | ||
+ | HorizontalTextPosition = RIGHT | ||
+ | VerticalTextPosition = CENTER | ||
+ | IconTextGap = 4 | ||
+ | DisplayedMnemonic = 0 | ||
+ | DisplayedMnemonicIndex = 0 | ||
+ | end | ||
+ | object jTextField1: | ||
+ | Tag = 2 | ||
+ | Left = 80 | ||
+ | Top = 8 | ||
+ | Width = 145 | ||
+ | Height = 25 | ||
+ | Cursor = crIBeam | ||
+ | Hint = ' | ||
+ | Font.Charset = DEFAULT_CHARSET | ||
+ | Font.Color = clWindowText | ||
+ | Font.Height = -12 | ||
+ | Font.Name = ' | ||
+ | Font.Style = [] | ||
+ | Foreground = 3355443 | ||
+ | Background = clWhite | ||
+ | HorizontalAlignment = LEFT | ||
+ | Text = '' | ||
+ | Editable = True | ||
+ | end | ||
+ | object bRaten: TJButton | ||
+ | Tag = 4 | ||
+ | Left = 8 | ||
+ | Top = 40 | ||
+ | Width = 219 | ||
+ | Height = 25 | ||
+ | Hint = ' | ||
+ | Font.Charset = DEFAULT_CHARSET | ||
+ | Font.Color = clWindowText | ||
+ | Font.Height = -12 | ||
+ | Font.Name = ' | ||
+ | Font.Style = [fsBold] | ||
+ | Foreground = 3355443 | ||
+ | Background = 15658734 | ||
+ | actionPerformed = ' | ||
+ | Text = ' | ||
+ | Mnemonic = 0 | ||
+ | DisplayedMnemonicIndex = 0 | ||
+ | Selected = False | ||
+ | BorderPainted = True | ||
+ | FocusPainted = False | ||
+ | ContentAreaFilled = True | ||
+ | HorizontalAlignment = CENTER | ||
+ | VerticalAlignment = CENTER | ||
+ | HorizontalTextPosition = RIGHT | ||
+ | VerticalTextPosition = CENTER | ||
+ | IconTextGap = 4 | ||
+ | RolloverEnabled = False | ||
+ | Border.BorderType = NoBorder | ||
+ | Border.LineColor = clBlack | ||
+ | Border.LineThickness = 0 | ||
+ | Border.LineRounded = False | ||
+ | Border.EtchHighlightColor = clBlack | ||
+ | Border.EtchShadowColor = clBlack | ||
+ | Border.Etchtype = 0 | ||
+ | Border.BevelHighlightColor = clBlack | ||
+ | Border.BevelShadowColor = clBlack | ||
+ | Border.Beveltype = 0 | ||
+ | Border.MatteColor = clBlack | ||
+ | Border.MatteTop = 0 | ||
+ | Border.MatteLeft = 0 | ||
+ | Border.MatteBottom = 0 | ||
+ | Border.MatteRight = 0 | ||
+ | end | ||
+ | end | ||
+ | </ | ||
+ | <code java Zahlenraten.java> | ||
+ | import java.awt.*; | ||
+ | import java.awt.event.*; | ||
+ | import javax.swing.*; | ||
+ | import javax.swing.event.*; | ||
+ | |||
+ | /** | ||
+ | * | ||
+ | * Beschreibung | ||
+ | * | ||
+ | * @version 1.0 vom 17.06.2021 | ||
+ | * @author | ||
+ | */ | ||
+ | |||
+ | public class Zahlenraten extends JFrame { | ||
+ | // Anfang Attribute | ||
+ | private JLabel lZahl = new JLabel(); | ||
+ | private JTextField jTextField1 = new JTextField(); | ||
+ | private JButton bRaten = new JButton(); | ||
+ | // Ende Attribute | ||
+ | | ||
+ | public Zahlenraten() { | ||
+ | // Frame-Initialisierung | ||
+ | super(); | ||
+ | setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); | ||
+ | int frameWidth = 249; | ||
+ | int frameHeight = 112; | ||
+ | setSize(frameWidth, | ||
+ | Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); | ||
+ | int x = (d.width - getSize().width) / 2; | ||
+ | int y = (d.height - getSize().height) / 2; | ||
+ | setLocation(x, | ||
+ | setTitle(" | ||
+ | setResizable(false); | ||
+ | Container cp = getContentPane(); | ||
+ | cp.setLayout(null); | ||
+ | // Anfang Komponenten | ||
+ | | ||
+ | lZahl.setBounds(8, | ||
+ | lZahl.setText(" | ||
+ | cp.add(lZahl); | ||
+ | jTextField1.setBounds(80, | ||
+ | cp.add(jTextField1); | ||
+ | bRaten.setBounds(8, | ||
+ | bRaten.setText(" | ||
+ | bRaten.setMargin(new Insets(2, 2, 2, 2)); | ||
+ | bRaten.addActionListener(new ActionListener() { | ||
+ | public void actionPerformed(ActionEvent evt) { | ||
+ | bRaten_ActionPerformed(evt); | ||
+ | } | ||
+ | }); | ||
+ | cp.add(bRaten); | ||
+ | // Ende Komponenten | ||
+ | | ||
+ | setVisible(true); | ||
+ | } // end of public Zahlenraten | ||
+ | | ||
+ | // Anfang Methoden | ||
+ | | ||
+ | static int zufallszahl = 0; | ||
+ | | ||
+ | public static void main(String[] args) { | ||
+ | new Zahlenraten(); | ||
+ | | ||
+ | zufallszahl = (int)Math.floor(Math.random() * 100 + 1); | ||
+ | } // end of main | ||
+ | | ||
+ | public void bRaten_ActionPerformed(ActionEvent evt) { | ||
+ | // TODO hier Quelltext einfügen | ||
+ | int geraten = Integer.parseInt(jTextField1.getText()); | ||
+ | | ||
+ | if (geraten > zufallszahl) { | ||
+ | JOptionPane.showMessageDialog(this, | ||
+ | } else if (geraten < zufallszahl) { | ||
+ | JOptionPane.showMessageDialog(this, | ||
+ | } else { | ||
+ | JOptionPane.showMessageDialog(this, | ||
+ | } // end of if-else | ||
+ | | ||
+ | } // end of bRaten_ActionPerformed | ||
+ | |||
+ | // Ende Methoden | ||
+ | } // end of class Zahlenraten | ||
+ | </ | ||
+ | </ | ||
+ | <aufgabe w1|**Aufgabe 3:**> | ||
+ | |||
+ | Erstellt eigene GUI-Programme, | ||
+ | * entweder die [[info: | ||
+ | * oder euch eigene Programme ausdenkt. | ||
+ | |||
+ | **Gebt mindestens eines dieser GUI-Programme ab (jeweils die .java und die .jfm Datei!)** | ||
</ | </ | ||
</ | </ |
info/sek-ii/e2/java-grundlagen/l12-gui.1623926280.txt.gz · Zuletzt geändert: 2021-06-17 12:38 von christian.weber