News Ticker

Java 11: GUI Kontoprogramm mit verschiedenen Konten

Im letzten Tutorial haben ich euch ja gezeigt wie ihr eine Grafische Oberfläche mit Java erzeugt. Das Kontoprogramm welches wir hier geschrieben haben erweitern wir jetzt etwas. Jetzt soll es möglich sein das wir auch mehre Konten haben welche verwaltet werden können.

Dazu legt ihr ein weiteres Label an, in dem Label gebt ihr den Text Kontonummer ein. Außerdem erstellt ihr ein Textfeld in welches man die Entsprechende Kontonummer eintragen kann.
Eure Grafische Oberfläche sollte dann etwa so aussehen:
Kono_GUI
Ich gebe euch noch einen kleinen Tipp. Bei den Kontonummern solltet ihr mit einem Array arbeiten. Sonst ist bei dieser kleinen Programmänderung aber nichts schweres dabei. Es werden nur dinge verwendet welche ich euch in den alten Java Tutorials schon gezeigt habe.

Euer Fertiger Quellcode sollte etwa so aussehen:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
/*
 * StartKonto.java
 *
 * Created on 01.08.2009, 16:26:57
 */
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
import javax.swing.JOptionPane;
 
/**
 *
 * @author Andreas
 */
public class StartKonto extends javax.swing.JFrame {
 
    double kontostand = 0.0;
    double letzteBuchung;
    double betrag;
    String eingabe;
    String ausgabe;
    String Kontonummerstr;
    int Kontonummerint;
    double [] Kontonumer = new double [100] ;
 
    BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
 
    /** Creates new form StartKonto */
    public StartKonto() {
        initComponents();
    }
 
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {
 
        jLabBuchung = new javax.swing.JLabel();
        jLabKontostand = new javax.swing.JLabel();
        jLabEingabe = new javax.swing.JLabel();
        jLabAusgabe = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jTextEingabe = new javax.swing.JTextField();
        jLabText = new javax.swing.JLabel();
        jLabKontonummer = new javax.swing.JLabel();
        jTextKontonummer = new javax.swing.JTextField();
 
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
 
        jLabBuchung.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabBuchung.setText("Letzte Buchung:");
 
        jLabKontostand.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabKontostand.setText("Kontostand:");
 
        jButton1.setText("Buchen");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1SayHelloClicked(evt);
            }
        });
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
 
        jTextEingabe.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextEingabeActionPerformed(evt);
            }
        });
 
        jLabText.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabText.setText("Buchung:");
 
        jLabKontonummer.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabKontonummer.setText("Kontonummer");
 
        jTextKontonummer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextKontonummerActionPerformed(evt);
            }
        });
 
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(50, 50, 50)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabText)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(30, 30, 30)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabBuchung)
                                    .addComponent(jLabKontostand)))
                            .addComponent(jTextEingabe, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jTextKontonummer, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabKontonummer, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addGap(42, 42, 42)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jLabEingabe, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabAusgabe, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE))
                            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(74, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addComponent(jLabText)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jTextEingabe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabKontonummer)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextKontonummer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 13, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabAusgabe, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabKontostand, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabEingabe, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabBuchung, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(98, 98, 98))
        );
 
        pack();
    }// </editor-fold>
 
    private void jButton1SayHelloClicked(java.awt.event.MouseEvent evt) {
 
        eingabe = jTextEingabe.getText();
        betrag=Double.parseDouble (eingabe);
        Kontonummerstr = jTextKontonummer.getText();
        Kontonummerint = Integer.parseInt(Kontonummerstr);
 
        if (Kontonumer[Kontonummerint]+betrag >=0.0)
        {
        Kontonumer[Kontonummerint] = Kontonumer[Kontonummerint] + betrag;
        ausgabe = Double.toString (Kontonumer[Kontonummerint]);
        jLabEingabe.setText (eingabe);
        jLabAusgabe.setText (ausgabe);
        // TODO add your handling code here:
        }
        else
        {
            JOptionPane.showMessageDialog(null, "Sie würden bei ihrem Konto ins Minus gehen", "Guthaben zu gering", JOptionPane.OK_CANCEL_OPTION);
        }
 
 
    }
 
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
}
 
    private void jTextEingabeActionPerformed(java.awt.event.ActionEvent evt) {
 
 
        // TODO add your handling code here:
}
 
    private void jTextKontonummerActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
}
 
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new StartKonto().setVisible(true);
            }
        });
    }
 
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabAusgabe;
    private javax.swing.JLabel jLabBuchung;
    private javax.swing.JLabel jLabEingabe;
    private javax.swing.JLabel jLabKontonummer;
    private javax.swing.JLabel jLabKontostand;
    private javax.swing.JLabel jLabText;
    private javax.swing.JTextField jTextEingabe;
    private javax.swing.JTextField jTextKontonummer;
    // End of variables declaration
 
}

Ich erkläre jetzt nur noch die dinge welche sich seit dem letzen Kontoprogramm geändert haben oder neu dazu kamen.

  • In der Zeile 27 und 28 erstellt ihr eine int und String Variable für die Kontonummer.
  • In der Zeile 29 erstellt ihr ein double Array welches 100 Variablen groß ist.
  • In der Zeile 156 lest ihr die Zahl aus dem Kontonummern Textfeld ein und speichert sie in der Variablen Kontonummerstr.
  • In der Zeile 157 wandelt ihr die Zahl aus der Variablen Kontonummerstr in ein int um und speichert sie dann in der Variablen Kontonummerint.
  • Sonst hat sich noch geändert das die Variable kontostand weggefallen ist. Überall wo diese stand ist jetzt das Array wie folgt eingetragen Kontonumer[Kontonummerint]. Denn so wird das Gebuchte Ergebnis immer auf das Konto mit der eingeben Kontonummer gebucht und angezeigt.

Hier geht es zu den bisherigen Teilen der Artikelserie Java:

Java 1: Netbeans die ersten Schritte und Hello World

Java 2: Rechnen mit Variablen

Java 3: Werte einlesen, zwischenspeichern und wieder ausgeben

Java 4: Kassen Programm mit if Abfrage

Java 5: Kassen Programm while, do-while Schleife

Java 6: Kassen Programm For Schleife

Java 7: Array – Lotto Programm erstellen

Java 8: Inhalt einer Datei auslesen und filtern

Java 9: Inhalt einer Textdatei in eine andere Datei kopieren

Java 10: Einführung in die Grafische Oberfläche – kleines Kontoprogramm