Wiki: Mathe und Info

Unterrichtsmaterialien für Mathematik und Informatik

Benutzer-Werkzeuge

Webseiten-Werkzeuge


info:sek-ii:e2:java-grundlagen:l12-gui

Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen angezeigt.

Link zu dieser Vergleichsansicht

Beide Seiten der vorigen RevisionVorhergehende Überarbeitung
Nächste Überarbeitung
Vorhergehende Überarbeitung
info:sek-ii:e2:java-grundlagen:l12-gui [2021-06-17 12:38] – [BetterBox#8] christian.weberinfo: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.
  
-{{ :info:sek-ii:e2:java-grundlagen:l12-gui-christian.weber-2021-06-17-12-32-50.png }}+{{ :info:sek-ii:e2:java-grundlagen:l12-gui-christian.weber-2021-06-17-12-32-50.png?nolink }}
 </bbox> </bbox>
 <bbox w2|**Schritt 2:**> <bbox w2|**Schritt 2:**>
Zeile 21: Zeile 21:
 In der oberen Leiste findest du die Steuerelemente, wie z.B. Schaltflächen (Buttons), Marken (Labels) und Textfelder (TextFields). In der oberen Leiste findest du die Steuerelemente, wie z.B. Schaltflächen (Buttons), Marken (Labels) und Textfelder (TextFields).
  
-{{ :info:sek-ii:e2:java-grundlagen:l12-gui-christian.weber-2021-06-17-12-29-55.png }}+{{ :info:sek-ii:e2:java-grundlagen:l12-gui-christian.weber-2021-06-17-12-29-55.png?nolink }}
 </bbox> </bbox>
 <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.
  
-{{ :info:sek-ii:e2:java-grundlagen:l12-gui-christian.weber-2021-06-17-12-29-26.png }}+{{ :info:sek-ii:e2:java-grundlagen:l12-gui-christian.weber-2021-06-17-12-29-26.png?nolink }}
 </bbox> </bbox>
 <bbox w2|**Schritt 4:**> <bbox w2|**Schritt 4:**>
Zeile 33: Zeile 33:
 Verändere die Eigenschaften (Attribute) der einzelnen Steuerelemente, indem du diese unten rechts anpasst, sodass dein Formular genau wie in Schritt 3 aussieht. Passe die Texte des Labels und des Buttons an. Verändere die Eigenschaften (Attribute) der einzelnen Steuerelemente, indem du diese unten rechts anpasst, sodass dein Formular genau wie in Schritt 3 aussieht. Passe die Texte des Labels und des Buttons an.
  
-{{ :info:sek-ii:e2:java-grundlagen:l12-gui-christian.weber-2021-06-17-12-34-34.png }}+{{ :info:sek-ii:e2:java-grundlagen:l12-gui-christian.weber-2021-06-17-12-34-34.png?nolink }}
 </bbox> </bbox>
 <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:
  
-{{ :info:sek-ii:e2:java-grundlagen:l12-gui-christian.weber-2021-06-17-12-36-34.png }}+{{ :info:sek-ii:e2:java-grundlagen:l12-gui-christian.weber-2021-06-17-12-36-34.png?nolink }}
  
-Führe das Programm mit einem Klick auf den "Starten"-Knopf {{:info:sek-ii:e2:java-grundlagen:l12-gui-christian.weber-2021-06-17-12-36-54.png}} aus und probiere dein erstes GUI-Programm aus!+Führe das Programm mit einem Klick auf den "Starten"-Knopf {{:info:sek-ii:e2:java-grundlagen:l12-gui-christian.weber-2021-06-17-12-36-54.png?nolink}} aus und probiere dein erstes GUI-Programm aus!
 </bbox> </bbox>
 </grid> </grid>
 +</aufgabe>
 +<lösung w1 collapsed|**Lösungen zu Aufgabe 1:**>
 +<code text HalloHorst.jfm>
 +object HalloHorst: TFGUIForm
 +  Left = 672
 +  Top = 440
 +  BorderIcons = [biSystemMenu]
 +  Caption = 'HalloHorst'
 +  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 = 'jLabel1'
 +    Font.Charset = DEFAULT_CHARSET
 +    Font.Color = clWindowText
 +    Font.Height = -12
 +    Font.Name = 'Dialog'
 +    Font.Style = [fsBold]
 +    Foreground = 3355443
 +    Background = clBtnFace
 +    Text = 'Name:'
 +    HorizontalAlignment = LEFT
 +    VerticalAlignment = CENTER
 +    HorizontalTextPosition = RIGHT
 +    VerticalTextPosition = CENTER
 +    IconTextGap = 4
 +    DisplayedMnemonic = 0
 +    DisplayedMnemonicIndex = 0
 +  end
 +  object jTextField1: TJTextField
 +    Tag = 2
 +    Left = 64
 +    Top = 8
 +    Width = 150
 +    Height = 20
 +    Cursor = crIBeam
 +    Hint = 'jTextField1'
 +    Font.Charset = DEFAULT_CHARSET
 +    Font.Color = clWindowText
 +    Font.Height = -12
 +    Font.Name = 'Dialog'
 +    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 = 'jButton1'
 +    Font.Charset = DEFAULT_CHARSET
 +    Font.Color = clWindowText
 +    Font.Height = -12
 +    Font.Name = 'Dialog'
 +    Font.Style = [fsBold]
 +    Foreground = 3355443
 +    Background = 15658734
 +    actionPerformed = 'bSageHallo_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>
 +<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, frameHeight);
 +    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
 +    int x = (d.width - getSize().width) / 2;
 +    int y = (d.height - getSize().height) / 2;
 +    setLocation(x, y);
 +    setTitle("HalloHorst");
 +    setResizable(false);
 +    Container cp = getContentPane();
 +    cp.setLayout(null);
 +    // Anfang Komponenten
 +    
 +    lName.setBounds(8, 8, 46, 20);
 +    lName.setText("Name:");
 +    cp.add(lName);
 +    jTextField1.setBounds(64, 8, 150, 20);
 +    cp.add(jTextField1);
 +    bSageHallo.setBounds(8, 32, 211, 25);
 +    bSageHallo.setText("Sage Hallo!");
 +    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, "Hallo, " + jTextField1.getText());
 +    
 +  } // end of bSageHallo_ActionPerformed
 +
 +  // Ende Methoden
 +} // end of class HalloHorst
 +</code>
 +</lösung>
 +<aufgabe w1|**Aufgabe 2:**>
 +
 +  - Erstelle den folgenden JFrame: {{ :info:sek-ii:e2:java-grundlagen:l12-gui-christian.weber-2021-06-17-12-49-15.png?nolink }}
 +  - Vor der ''main(...)''-Methode soll eine Zufallszahl deklariert werden, die in der ''main(...)''-Methode generiert wird: <code java>
 +static int zufallszahl = 0;
 +  
 +public static void main(String[] args) {
 +  new Zahlenraten();
 +
 +  zufallszahl = (int)Math.floor(Math.random() * 100 + 1);
 +} // end of main
 +</code>
 +  - Wenn der Raten-Button angeklickt wird, soll mit <code java>int geraten = Integer.parseInt(jTextField1.getText());</code> die geratene Zahl ermittelt werden. Danach musst du mit Hilfe von ''if''-Anweisungen und dem ''JOptionPane'' von oben folgendes umsetzen:
 +    * Wenn die geratene Zahl zu hoch ist, soll als Nachricht "Zu hoch geraten" ausgegeben werden.
 +    * Wenn die geratene Zahl zu niedrig ist, soll als Nachricht "Zu niedrig geraten" ausgegeben werden.
 +    * Wenn richtig geraten wurde, soll als Nachricht "Richtig geraten, die Zahl war: XX" ausgegeben werden (XX soll durch die Zahl ersetzt werden).
 +</aufgabe>
 +<lösung collapsed w1|**Lösung zu Aufgabe 2**>
 +<code text Zahlenraten.jfm>
 +object Zahlenraten: TFGUIForm
 +  Left = 913
 +  Top = 253
 +  BorderIcons = [biSystemMenu]
 +  Caption = 'Zahlenraten'
 +  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 = 'jLabel1'
 +    Font.Charset = DEFAULT_CHARSET
 +    Font.Color = clWindowText
 +    Font.Height = -12
 +    Font.Name = 'Dialog'
 +    Font.Style = [fsBold]
 +    Foreground = 3355443
 +    Background = clBtnFace
 +    Text = 'Zahl:'
 +    HorizontalAlignment = LEFT
 +    VerticalAlignment = CENTER
 +    HorizontalTextPosition = RIGHT
 +    VerticalTextPosition = CENTER
 +    IconTextGap = 4
 +    DisplayedMnemonic = 0
 +    DisplayedMnemonicIndex = 0
 +  end
 +  object jTextField1: TJTextField
 +    Tag = 2
 +    Left = 80
 +    Top = 8
 +    Width = 145
 +    Height = 25
 +    Cursor = crIBeam
 +    Hint = 'jTextField1'
 +    Font.Charset = DEFAULT_CHARSET
 +    Font.Color = clWindowText
 +    Font.Height = -12
 +    Font.Name = 'Dialog'
 +    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 = 'jButton1'
 +    Font.Charset = DEFAULT_CHARSET
 +    Font.Color = clWindowText
 +    Font.Height = -12
 +    Font.Name = 'Dialog'
 +    Font.Style = [fsBold]
 +    Foreground = 3355443
 +    Background = 15658734
 +    actionPerformed = 'bRaten_ActionPerformed'
 +    Text = 'Raten!'
 +    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>
 +<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, frameHeight);
 +    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
 +    int x = (d.width - getSize().width) / 2;
 +    int y = (d.height - getSize().height) / 2;
 +    setLocation(x, y);
 +    setTitle("Zahlenraten");
 +    setResizable(false);
 +    Container cp = getContentPane();
 +    cp.setLayout(null);
 +    // Anfang Komponenten
 +    
 +    lZahl.setBounds(8, 8, 62, 20);
 +    lZahl.setText("Zahl:");
 +    cp.add(lZahl);
 +    jTextField1.setBounds(80, 8, 145, 25);
 +    cp.add(jTextField1);
 +    bRaten.setBounds(8, 40, 219, 25);
 +    bRaten.setText("Raten!");
 +    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, "Du hast zu hoch geraten!");  
 +    } else if (geraten < zufallszahl) {
 +      JOptionPane.showMessageDialog(this, "Du hast zu niedrig geraten!");  
 +    } else {
 +      JOptionPane.showMessageDialog(this, "Du hast richtig geraten! Die Zahl war: " + zufallszahl);  
 +    } // end of if-else
 +    
 +  } // end of bRaten_ActionPerformed
 +
 +  // Ende Methoden
 +} // end of class Zahlenraten
 +</code>
 +</lösung>
 +<aufgabe w1|**Aufgabe 3:**>
 +
 +Erstellt eigene GUI-Programme, indem ihr:
 +  * entweder die [[info:sek-ii:e2:java-grundlagen:l10-strings-eingabe-ausgabe#aufgabe_5weitere_java-programme|Konsolen-Programme]] (Taschen-, Währungs-, Temperatur-, BMI- oder Blutalkoholspiegelrechner) als GUI-Programme nachbaut
 +  * oder euch eigene Programme ausdenkt.
 +
 +**Gebt mindestens eines dieser GUI-Programme ab (jeweils die .java und die .jfm Datei!)**
 </aufgabe> </aufgabe>
 </grid> </grid>
info/sek-ii/e2/java-grundlagen/l12-gui.1623926280.txt.gz · Zuletzt geändert: 2021-06-17 12:38 von christian.weber