JTextfield

Liondary

Mitglied
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:
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:
Moin,

add 1) nutze bitte die Java-tags, so bekommt man ja Augenkrebs !!
add 2) wo genau funktioniert was nicht??
etwas genauere Angaben wären nett !

Gruß Klaus
 
Hey Klaus,
1) tut mir Leid bin ganz neu hier... was sind Java-tags? Hoffe ich hab diesmal richtig gemacht.

2)

Java:
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);

Ich will das wenn man diesen Button klickt KontoInhaber, KontoStand, KontoNummer, ErstellungsDatum und PIN an ein neues Konto gegeben werden und dieses Konto dann in die Liste geschrieben wird. Was KontoInhaber, ErstellungsDatum und PIN sind, steht in den JTextfields dadrüber.

Wenn ich dann aber mit get auf das Konto was ich erstellt habe zugreife, existiert das Konto zwar aber KontoInhaber, KontoStand, ErstellungDatum und PIN sind alle 0.

Gruß Leo
 
Zuletzt bearbeitet von einem Moderator:
Das hier ist das Objekt "Konto" :

Java:
package com.company;


public class Konto {

    private String KontoInhaber;
    private int KontoStand;
    private int KontoNummer;
    private int NeuerKontoStand;
    private int ErstellungsDatum;
    private int Jahr;
    private int PIN;

    public Konto (String _KontoInhaber, int _KontoStand, int _KontoNummer, int _ErstellungsDatum, int _PIN) {

        KontoInhaber = _KontoInhaber;
        KontoStand = _KontoStand;
        KontoNummer = _KontoNummer;
        ErstellungsDatum = _ErstellungsDatum;
        PIN = _PIN;

    }

    public String getKontoInhaber() {

        return KontoInhaber;
    }

    public String setKontoInhaber(String NeuerKontoInhaber) {

        KontoInhaber = NeuerKontoInhaber;

        return KontoInhaber;
    }

    public int getKontoStand() {

        return KontoStand;
    }

    public int setKontoStand(int KontoStand, int Einzahlung) {

        NeuerKontoStand = KontoStand + Einzahlung;

        return NeuerKontoStand;

    }

    public int getKontoNummer() {

        return KontoNummer;
    }

    public int setKontoNummer(int NeueKontoNummer) {

        KontoNummer = NeueKontoNummer;

        return KontoNummer;

    }

    public int getErstellungsDatum() {

        return ErstellungsDatum;
    }

    public int getPIN() {

        return PIN;
    }


}

Edit: KontoNummer und KontoStand werden korrekt angezeigt. Nur KontoInhaber, ErstellungsDatum und PIN, also die Einträge die ich in den darüber liegenden JTextfields eingebe, werden als 0 ausgegeben.
 
Es macht ja auch keinen Sinn, dass Du die Variablen KontoInhaber etc nur setzt wenn das Textfield eine Action auslöst. U.U. tut sie das nämlich gar nicht. Lies die Werte aus den Textfeldern mit getText() aus.
 
Java:
     Jahreseingabe.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
         ErstellungsDatum = Integer.parseInt(Jahreseingabe.getText());
       }
     });
     PINeingabe.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
         PIN = Integer.parseInt(PINeingabe.getText());
       }
     });
     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();
       }
     });
Edit: KontoNummer und KontoStand werden korrekt angezeigt. Nur KontoInhaber, ErstellungsDatum und PIN, also die Einträge die ich in den darüber liegenden JTextfields eingebe, werden als 0 ausgegeben.

Wann glaubst du werden die ActionListener vom JTextField denn ausgeführt? Hast du kontrolliert ob die jemals ausgeführt werden? Was würde passieren wenn diese nie ausgeführt werden?

Anmerkung zu deinem Code:
Instanzvariablen und Parameter werden immer in lowerCamelCase geschrieben, nicht in UpperCamelCase!!
Eine setter Methode setzt einen Wert daher sollte man hier keinen Wert zurückgeben. Dazu hat man ja den entsprechenden Getter!
 
Perfekt! Ich danke euch allen! Es funktioniert jetzt 🙂

Java:
 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);

        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);

        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);

        Kontoerstellung.add(PINeingabe);

        ErstellenButton = new JButton("Erstellen");
        ErstellenButton.setLocation(165,355);
        ErstellenButton.setSize(120,35);
        ErstellenButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                KontoInhaber = Namenseingabe.getText();
                PIN = Integer.parseInt(PINeingabe.getText());
                ErstellungsDatum = Integer.parseInt(Jahreseingabe.getText());
                KontoNummer = ii;
                KontoStand = 100;
                KontenListe.add(new Konto(KontoInhaber, KontoStand, KontoNummer,ErstellungsDatum, PIN));
                ii++;
                closeFrame();
                begruessung();
            }
        });
        Kontoerstellung.add(ErstellenButton);

        Kontoerstellung.setOpaque(true);

        return Kontoerstellung;
    }

@Joose Danke für den Hinweis. Ich wird in Zukunft drauf achten 🙂
 

Neue Themen


Zurück
Oben