hallo !!
ich hab problem und zwar, in projekt sollte wsich kd nach registrierung in datei UserIndex.dat speichern und das klappt, aber wenn ich 2 kunde anlegen moechte dann tut sich nicht ...
package gui;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.ibjectInputStream;
import java.ibjectOutputStream;
import java.io.Serializable;
public class UserSpeicher {
// Speichern von Bedienern
public static void save(){
try{
FileOutputStream fos = new FileOutputStream("UserIndex.dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
for(int i=0;i<Person.kList.size();i++){
oos.writeObject(Person.kList.elementAt(i));
}
oos.close();
fos.close();
}
catch(IOException e) {
System.err.println("");
}
}
//Laden von Bedienern
public static void load(){
try{
FileInputStream fis = new FileInputStream("UserIndex.dat");
ObjectInputStream ois = new ObjectInputStream(fis);
//falls schon einmal die Daten geladen wurden, werden diese erstmal gelöscht
Person.kList.removeAllElements();
while(true) {
try{
Person person = (Person) ois.readObject();
Person.kList.addElement(person);
}
catch(EOFException e) {break;}
}
}
catch(FileNotFoundException e){System.err.println(e.toString());}
catch(IOException e){System.err.println(e.toString());}
catch(ClassNotFoundException e){System.err.println(e.toString());}
}
}
package gui;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JProgressBar;
import javax.swing.JInternalFrame;
import javax.swing.JTextPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JDesktopPane;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextField;
import javax.swing.JEditorPane;
import javax.swing.JButton;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JSlider;
import javax.swing.JList;
import javax.swing.JLabel;
import javax.swing.JToolBar;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JPasswordField;
public class Regist extends JFrame implements ActionListener{
private JPanel jContentPane = null;
private JLabel jLabel = null;
private JLabel jLabel1 = null;
private JLabel jLabel11 = null;
private JLabel jLabel111 = null;
private JLabel jLabel1111 = null;
private JButton jButton = null;
private JTextField jTextField = null;
private JTextField jTextField1 = null;
private JTextField jTextField11 = null;
private JButton jButton1 = null;
private JPasswordField jPasswordField = null;
//konstruktor
public Regist(){
super();
initialize();
}
private void initialize(){
this.setSize(530, 380);
this.setContentPane(getJContentPane());
this.setContentPane(getJContentPane());
this.setTitle("REGISTRIERUNG");
}
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel1111 = new JLabel();
jLabel1111.setBounds(new Rectangle(49, 229, 89, 27));
jLabel1111.setText("PASSWORT");
jLabel111 = new JLabel();
jLabel111.setBounds(new Rectangle(49, 178, 89, 27));
jLabel111.setText("LOGIN");
jLabel11 = new JLabel();
jLabel11.setBounds(new Rectangle(50, 130, 89, 27));
jLabel11.setText("NACHNAME");
jLabel1 = new JLabel();
jLabel1.setBounds(new Rectangle(50, 80, 89, 27));
jLabel1.setText("VORNAME");
jLabel = new JLabel();
jLabel.setText(" REGISTRIEREN");
jLabel.setBounds(new Rectangle(203, 27, 104, 30));
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(jLabel, null);
jContentPane.add(jLabel1, null);
jContentPane.add(jLabel11, null);
jContentPane.add(jLabel111, null);
jContentPane.add(jLabel1111, null);
jContentPane.add(getJButton(), null);
jContentPane.add(getJTextField(), null);
jContentPane.add(getJTextField1(), null);
jContentPane.add(getJTextField11(), null);
jContentPane.add(getJButton1(), null);
jContentPane.add(getJPasswordField(), null);
}
return jContentPane;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(382, 300, 107, 27));
jButton.setText("Speichern");
jButton.addActionListener(this);
}
return jButton;
}
/**
* This method initializes jTextField
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
jTextField.setBounds(new Rectangle(180, 80, 216, 27));
jTextField.addActionListener(this);
}
return jTextField;
}
/**
* This method initializes jTextField1
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField1() {
if (jTextField1 == null) {
jTextField1 = new JTextField();
jTextField1.setBounds(new Rectangle(180, 130, 216, 27));
jTextField1.addActionListener(this);
}
return jTextField1;
}
/**
* This method initializes jTextField11
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField11() {
if (jTextField11 == null) {
jTextField11 = new JTextField();
jTextField11.setBounds(new Rectangle(180, 180, 216, 27));
jTextField11.addActionListener(this);
}
return jTextField11;
}
private JPasswordField getJPasswordField() {
if (jPasswordField == null) {
jPasswordField = new JPasswordField();
jPasswordField.setBounds(new Rectangle(180, 230, 216, 27));
jPasswordField.addActionListener(this);
}
return jPasswordField;
}
private JButton getJButton1() {
if (jButton1 == null) {
jButton1 = new JButton();
jButton1.setBounds(new Rectangle(30, 300, 107, 27));
jButton1.setText("Abbrechen");
jButton1.addActionListener(this);
}
return jButton1;
}
public void actionPerformed(ActionEvent e) {
Fkt y=new Fkt();
if(e.getActionCommand().equals("Abbrechen")){
//event();
//input_windows.setVisible(false);
if(y.Yes_No_Message("Möchten Sie wirklich abbrechen? ") == 0){
this.setVisible(false);
this.dispose();
}}
if(e.getActionCommand().equals("Speichern")){
if(jTextField1.getText().equals("")==true || jTextField1.getText().length()<3) {
if(jTextField1.getText().equals("")==true) y.Message("Kein Nachname eingegeben!\nEr muss mindestens aus 3 Zeichen bestehen");
else y.Message("Nachname zu kurz!\nEr muss mindestens aus 3 Zeichen bestehen");
}
else {
if(jTextField.getText().equals("")==true || jTextField.getText().length()<2) {
if(jTextField.getText().equals("")==true) y.Message("Kein Vorname eingegeben!\nEr muss mindestens aus 3 Zeichen bestehen");
else y.Message("Vorname zu kurz!\nEr muss aus mindestens 2 Zeichen bestehen!");
}
else {
if(jTextField11.getText().equals("")==true || jTextField11.getText().length()<4){
if(jTextField11.getText().equals("")==true) y.Message("Kein Login eingegeben!\nEr muss mindestens aus 3 Zeichen bestehen");
else y.Message("Login zu kurz!\nEr muss aus mindestens 2 Zeichen bestehen!");
}
else {
UserSpeicher.load();
boolean test = false;
for(int i=0;i<Person.kList.size();i++){
Person person = new Person();
person = (Person)Person.kList.elementAt(i);
if(person.getLogin().equals(jTextField11.getText()))test=true;}
Verwalter admin=new Verwalter();
if (test==true|| (jTextField11.getText().equalsIgnoreCase(admin.getVerwalterLogin())==true)){
y.Message("Login bereits vorhanden!");
}
else {
char[] pass1 = jPasswordField.getPassword();
int newlength = 0;
if (pass1.length<5) {
if(pass1.length==0) y.Message("Kein Passwort eingetragen!");
else if(pass1.length<5) y.Message("Passwort zu Kurz! Es muss mindestens 5 Zeichen haben!");
}
else {
if(y.Yes_No_Message("Wiklich speichern?")==0){
Person person=new Person();
person.setSurname(jTextField1.getText());
person.setFirstname(jTextField1.getText());
person.setLogin(jTextField11.getText());
person.setPasswort(jPasswordField.getPassword());
person.setSperre(false);
Person.kList.addElement(person);
UserSpeicher.save();
y.Message("Daten gespeichert!");
this.setVisible(false);
}
}
}
}
}
}
}
}
}
ich hab problem und zwar, in projekt sollte wsich kd nach registrierung in datei UserIndex.dat speichern und das klappt, aber wenn ich 2 kunde anlegen moechte dann tut sich nicht ...
package gui;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.ibjectInputStream;
import java.ibjectOutputStream;
import java.io.Serializable;
public class UserSpeicher {
// Speichern von Bedienern
public static void save(){
try{
FileOutputStream fos = new FileOutputStream("UserIndex.dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
for(int i=0;i<Person.kList.size();i++){
oos.writeObject(Person.kList.elementAt(i));
}
oos.close();
fos.close();
}
catch(IOException e) {
System.err.println("");
}
}
//Laden von Bedienern
public static void load(){
try{
FileInputStream fis = new FileInputStream("UserIndex.dat");
ObjectInputStream ois = new ObjectInputStream(fis);
//falls schon einmal die Daten geladen wurden, werden diese erstmal gelöscht
Person.kList.removeAllElements();
while(true) {
try{
Person person = (Person) ois.readObject();
Person.kList.addElement(person);
}
catch(EOFException e) {break;}
}
}
catch(FileNotFoundException e){System.err.println(e.toString());}
catch(IOException e){System.err.println(e.toString());}
catch(ClassNotFoundException e){System.err.println(e.toString());}
}
}
package gui;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JProgressBar;
import javax.swing.JInternalFrame;
import javax.swing.JTextPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JDesktopPane;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextField;
import javax.swing.JEditorPane;
import javax.swing.JButton;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JSlider;
import javax.swing.JList;
import javax.swing.JLabel;
import javax.swing.JToolBar;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JPasswordField;
public class Regist extends JFrame implements ActionListener{
private JPanel jContentPane = null;
private JLabel jLabel = null;
private JLabel jLabel1 = null;
private JLabel jLabel11 = null;
private JLabel jLabel111 = null;
private JLabel jLabel1111 = null;
private JButton jButton = null;
private JTextField jTextField = null;
private JTextField jTextField1 = null;
private JTextField jTextField11 = null;
private JButton jButton1 = null;
private JPasswordField jPasswordField = null;
//konstruktor
public Regist(){
super();
initialize();
}
private void initialize(){
this.setSize(530, 380);
this.setContentPane(getJContentPane());
this.setContentPane(getJContentPane());
this.setTitle("REGISTRIERUNG");
}
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel1111 = new JLabel();
jLabel1111.setBounds(new Rectangle(49, 229, 89, 27));
jLabel1111.setText("PASSWORT");
jLabel111 = new JLabel();
jLabel111.setBounds(new Rectangle(49, 178, 89, 27));
jLabel111.setText("LOGIN");
jLabel11 = new JLabel();
jLabel11.setBounds(new Rectangle(50, 130, 89, 27));
jLabel11.setText("NACHNAME");
jLabel1 = new JLabel();
jLabel1.setBounds(new Rectangle(50, 80, 89, 27));
jLabel1.setText("VORNAME");
jLabel = new JLabel();
jLabel.setText(" REGISTRIEREN");
jLabel.setBounds(new Rectangle(203, 27, 104, 30));
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(jLabel, null);
jContentPane.add(jLabel1, null);
jContentPane.add(jLabel11, null);
jContentPane.add(jLabel111, null);
jContentPane.add(jLabel1111, null);
jContentPane.add(getJButton(), null);
jContentPane.add(getJTextField(), null);
jContentPane.add(getJTextField1(), null);
jContentPane.add(getJTextField11(), null);
jContentPane.add(getJButton1(), null);
jContentPane.add(getJPasswordField(), null);
}
return jContentPane;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(382, 300, 107, 27));
jButton.setText("Speichern");
jButton.addActionListener(this);
}
return jButton;
}
/**
* This method initializes jTextField
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
jTextField.setBounds(new Rectangle(180, 80, 216, 27));
jTextField.addActionListener(this);
}
return jTextField;
}
/**
* This method initializes jTextField1
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField1() {
if (jTextField1 == null) {
jTextField1 = new JTextField();
jTextField1.setBounds(new Rectangle(180, 130, 216, 27));
jTextField1.addActionListener(this);
}
return jTextField1;
}
/**
* This method initializes jTextField11
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField11() {
if (jTextField11 == null) {
jTextField11 = new JTextField();
jTextField11.setBounds(new Rectangle(180, 180, 216, 27));
jTextField11.addActionListener(this);
}
return jTextField11;
}
private JPasswordField getJPasswordField() {
if (jPasswordField == null) {
jPasswordField = new JPasswordField();
jPasswordField.setBounds(new Rectangle(180, 230, 216, 27));
jPasswordField.addActionListener(this);
}
return jPasswordField;
}
private JButton getJButton1() {
if (jButton1 == null) {
jButton1 = new JButton();
jButton1.setBounds(new Rectangle(30, 300, 107, 27));
jButton1.setText("Abbrechen");
jButton1.addActionListener(this);
}
return jButton1;
}
public void actionPerformed(ActionEvent e) {
Fkt y=new Fkt();
if(e.getActionCommand().equals("Abbrechen")){
//event();
//input_windows.setVisible(false);
if(y.Yes_No_Message("Möchten Sie wirklich abbrechen? ") == 0){
this.setVisible(false);
this.dispose();
}}
if(e.getActionCommand().equals("Speichern")){
if(jTextField1.getText().equals("")==true || jTextField1.getText().length()<3) {
if(jTextField1.getText().equals("")==true) y.Message("Kein Nachname eingegeben!\nEr muss mindestens aus 3 Zeichen bestehen");
else y.Message("Nachname zu kurz!\nEr muss mindestens aus 3 Zeichen bestehen");
}
else {
if(jTextField.getText().equals("")==true || jTextField.getText().length()<2) {
if(jTextField.getText().equals("")==true) y.Message("Kein Vorname eingegeben!\nEr muss mindestens aus 3 Zeichen bestehen");
else y.Message("Vorname zu kurz!\nEr muss aus mindestens 2 Zeichen bestehen!");
}
else {
if(jTextField11.getText().equals("")==true || jTextField11.getText().length()<4){
if(jTextField11.getText().equals("")==true) y.Message("Kein Login eingegeben!\nEr muss mindestens aus 3 Zeichen bestehen");
else y.Message("Login zu kurz!\nEr muss aus mindestens 2 Zeichen bestehen!");
}
else {
UserSpeicher.load();
boolean test = false;
for(int i=0;i<Person.kList.size();i++){
Person person = new Person();
person = (Person)Person.kList.elementAt(i);
if(person.getLogin().equals(jTextField11.getText()))test=true;}
Verwalter admin=new Verwalter();
if (test==true|| (jTextField11.getText().equalsIgnoreCase(admin.getVerwalterLogin())==true)){
y.Message("Login bereits vorhanden!");
}
else {
char[] pass1 = jPasswordField.getPassword();
int newlength = 0;
if (pass1.length<5) {
if(pass1.length==0) y.Message("Kein Passwort eingetragen!");
else if(pass1.length<5) y.Message("Passwort zu Kurz! Es muss mindestens 5 Zeichen haben!");
}
else {
if(y.Yes_No_Message("Wiklich speichern?")==0){
Person person=new Person();
person.setSurname(jTextField1.getText());
person.setFirstname(jTextField1.getText());
person.setLogin(jTextField11.getText());
person.setPasswort(jPasswordField.getPassword());
person.setSperre(false);
Person.kList.addElement(person);
UserSpeicher.save();
y.Message("Daten gespeichert!");
this.setVisible(false);
}
}
}
}
}
}
}
}
}