Hey,
ich versuche grade eine List mit einem Objekt zu befüllen. Die Dinge die in das Objekt gehören sollen über ein Interface mit Hilfe von JTextfield eingegeben werden. Funktioniert auch alles soweit außer das die Dinge anscheinend nicht richtig im Objekt gespeichert werden, da wenn ich die einzelnen Dinge mit getter aus dem Objekt hervorrufe wird nur "null" ausgegeben... Kann mir da jemand helfen?
Hier mein Code:
ich versuche grade eine List mit einem Objekt zu befüllen. Die Dinge die in das Objekt gehören sollen über ein Interface mit Hilfe von JTextfield eingegeben werden. Funktioniert auch alles soweit außer das die Dinge anscheinend nicht richtig im Objekt gespeichert werden, da wenn ich die einzelnen Dinge mit getter aus dem Objekt hervorrufe wird nur "null" ausgegeben... Kann mir da jemand helfen?
Hier mein Code:
Java:
package com.company;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.*;
import java.util.LinkedList;
public class Main implements ActionListener {
JPanel Schalter, buttonPanel, Kontoerstellung;
JButton erstellenButton, greifenButton, verlassenButton, ErstellenButton;
JLabel Namensfrage, Jahresfrage, PINfrage;
JTextField Namenseingabe, Jahreseingabe, PINeingabe;
JFrame frame;
List<Konto> KontenListe = new LinkedList<Konto>();
Scanner scanner = new Scanner(System.in);
static String KontoInhaber;
int ErstellungsDatum;
int PIN;
int KontoNummer;
int KontoStand;
int ii = 1;
public static void main(String[] args) { new Main(); }
public Main() {
begruessung();
}
public void begruessung() { createAndShowWindow(); }
public void createAndShowWindow() {
frame = new JFrame("Schalter");
frame.setContentPane(this.createContentPane());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(435, 300);
frame.setVisible(true);
}
public JPanel createContentPane() {
Schalter = new JPanel();
Schalter.setLayout(null);
buttonPanel = new JPanel();
buttonPanel.setLayout(null);
buttonPanel.setLocation(10, 80);
buttonPanel.setSize(435, 300);
Schalter.add(buttonPanel);
erstellenButton = new JButton("Neues Konto erstellen");
erstellenButton.setLocation(0, 0);
erstellenButton.setSize(200,30);
erstellenButton.addActionListener(this);
buttonPanel.add(erstellenButton);
greifenButton = new JButton("Auf Konto zugreifen!");
greifenButton.setLocation(200, 0);
greifenButton.setSize(200, 30);
greifenButton.addActionListener(this);
buttonPanel.add(greifenButton);
verlassenButton = new JButton("Verlassen");
verlassenButton.setLocation(100, 40);
verlassenButton.setSize(200, 30);
verlassenButton.addActionListener(this);
buttonPanel.add(verlassenButton);
Schalter.setOpaque(true);
return Schalter;
}
public void actionPerformed(ActionEvent e) {
closeFrame();
if (e.getSource() == erstellenButton) {
erstellen();
} else if (e.getSource() == greifenButton) {
greifen();
} else if (e.getSource() == verlassenButton) {
// verlassen();}
}
}
private void closeFrame() {
frame.setVisible(false);
frame.dispose();
}
public void erstellen() {
kontoerstellung();
}
public void kontoerstellung() { createAndShowKontoerstellung(); }
public void createAndShowKontoerstellung() {
frame = new JFrame("Kontoerstellung");
frame.setContentPane(this.createKontoerstellung());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(470, 600);
frame.setVisible(true);
}
public JPanel createKontoerstellung() {
Kontoerstellung = new JPanel();
Kontoerstellung.setLayout(null);
Namensfrage = new JLabel("Wie lautet ihr Name?");
Namensfrage.setLocation(10, 0);
Namensfrage.setSize(435, 50);
Namensfrage.setHorizontalAlignment(0);
Kontoerstellung.add(Namensfrage);
Namenseingabe = new JTextField();
Namenseingabe.setLocation(10,55);
Namenseingabe.setSize(435,35);
Namenseingabe.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
KontoInhaber = Namenseingabe.getText();
}
});
Kontoerstellung.add(Namenseingabe);
Jahresfrage = new JLabel("Welches Jahr haben wir?");
Jahresfrage.setLocation(10, 100);
Jahresfrage.setSize(435,50);
Jahresfrage.setHorizontalAlignment(0);
Kontoerstellung.add(Jahresfrage);
Jahreseingabe = new JTextField();
Jahreseingabe.setLocation(10,155);
Jahreseingabe.setSize(435,35);
Jahreseingabe.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ErstellungsDatum = Integer.parseInt(Jahreseingabe.getText());
}
});
Kontoerstellung.add(Jahreseingabe);
PINfrage = new JLabel("Wie soll Ihr PIN lauten?");
PINfrage.setLocation(10, 200);
PINfrage.setSize(435,50);
PINfrage.setHorizontalAlignment(0);
Kontoerstellung.add(PINfrage);
PINeingabe = new JTextField();
PINeingabe.setLocation(10, 255);
PINeingabe.setSize(435, 35);
PINeingabe.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
PIN = Integer.parseInt(PINeingabe.getText());
}
});
Kontoerstellung.add(PINeingabe);
ErstellenButton = new JButton("Erstellen");
ErstellenButton.setLocation(165,355);
ErstellenButton.setSize(120,35);
ErstellenButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
KontoNummer = ii;
KontoStand = 100;
KontenListe.add(new Konto(KontoInhaber, KontoStand, KontoNummer,ErstellungsDatum, PIN));
ii++;
closeFrame();
begruessung();
}
});
Kontoerstellung.add(ErstellenButton);
Kontoerstellung.setOpaque(true);
return Kontoerstellung;
}
public void greifen() {
System.out.print("Wie lautet Ihre Kontonummer? ");
KontoNummer = scanner.nextInt();
System.out.print(KontenListe.get(KontoNummer-1).getKontoStand());
}
}
Zuletzt bearbeitet von einem Moderator: