Also ich habe hier eine Oberfläche aus Spaß (zum lernen ) erzeugt und bin nun auf ein Problem gestoßen...
Also was das ganze darstellen soll ist ein "Geldautomat" . Derzeit habe ich nur für Person 1 die Oberfläche "angefangen" Die Karten Nummer der 1. Person ist "1" und die Geheimzahl "1337" wenn man diese beiden eingibt kommt man "rein".
Mein Problem ist allerdings das die Person nach 3 Versuchen wieder zum Anfangszustand zurück kommt, was schwer zu beschreiben ist da sich alles auf einem Frame abspielt und nur Sichtbar/Unsichtbar gemacht wird.
Mein derzeitiger kompletter Code:
Das ganze funktioniert so wie es momentan ist, nur das man direkt beim 1. Falschen versuch zurück zum "Anfang" kommt.
Ich hatte es vorher bereits mit
versucht allerdings hat das auch nicht funktioniert, warum weiß ich auch nicht aber ich glaube er hat direkt alle durchgänge beim 1. Button klick vollzogen und was anderes fällt mir momentan zur Lösung des Problems nicht ein... Wenn jemand ein Vorschlag hätte wäre das sehr hilfreich
Danke im Vorraus
Also was das ganze darstellen soll ist ein "Geldautomat" . Derzeit habe ich nur für Person 1 die Oberfläche "angefangen" Die Karten Nummer der 1. Person ist "1" und die Geheimzahl "1337" wenn man diese beiden eingibt kommt man "rein".
Mein Problem ist allerdings das die Person nach 3 Versuchen wieder zum Anfangszustand zurück kommt, was schwer zu beschreiben ist da sich alles auf einem Frame abspielt und nur Sichtbar/Unsichtbar gemacht wird.
Mein derzeitiger kompletter Code:
Java:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
/**
*
* Beschreibung
*
* @version 1.0 vom 25.03.2015
* @author
*/
public class Geldautomat extends JFrame {
// Anfang Attribute
private JLabel jLabel1 = new JLabel();
private JLabel jLabel2 = new JLabel();
private JButton jButton1 = new JButton();
private JButton jButton2 = new JButton();
private JTextField jTextField1 = new JTextField();
private JLabel jLabel3 = new JLabel();
private JLabel jLabel4 = new JLabel();
private JLabel jLabel5 = new JLabel();
private JLabel jLabel6 = new JLabel();
private JLabel jLabel7 = new JLabel();
private JButton jButton3 = new JButton();
private JLabel jLabel8 = new JLabel();
private JTextField jTextField2 = new JTextField();
private JButton jButton4 = new JButton();
private JButton jButton5 = new JButton();
private JLabel jLabel9 = new JLabel();
private JLabel jLabel10 = new JLabel();
private JLabel jLabel11 = new JLabel();
private JLabel jLabel12 = new JLabel();
private JLabel jLabel13 = new JLabel();
private JLabel jLabel14 = new JLabel();
private JLabel jLabel15 = new JLabel();
// Ende Attribute
public Geldautomat(String title) {
// Frame-Initialisierung
super(title);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
int frameWidth = 800;
int frameHeight = 600;
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);
setResizable(false);
Container cp = getContentPane();
cp.setLayout(null);
// Anfang Komponenten
jLabel1.setBounds(250, 100, 300, 36);
jLabel1.setText("Herzlich Willkommen");
jLabel1.setFont(new Font("Dialog", Font.BOLD, 30));
cp.add(jLabel1);
jLabel2.setBounds(340, 150, 120, 20);
jLabel2.setText("Bitte Karte einführen");
cp.add(jLabel2);
jButton1.setBounds(340, 240, 120, 25);
jButton1.setText("*Karte einführen*");
jButton1.setMargin(new Insets(2, 2, 2, 2));
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton1_ActionPerformed(evt);
}
});
cp.add(jButton1);
jButton2.setBounds(752, 8, 27, 25);
jButton2.setText("?");
jButton2.setMargin(new Insets(2, 2, 2, 2));
jButton2.setFont(new Font("Dialog", Font.BOLD, 16));
jButton2.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent evt) {
jButton2_MouseEntered(evt);
jLabel4.setText("Verfügbare Kartennummern: 1,2,3,4,5");
}
});
jButton2.addMouseListener(new MouseAdapter() {
public void mouseExited(MouseEvent evt) {
jButton2_MouseExited(evt);
jLabel4.setText("");
}
});
cp.add(jButton2);
jTextField1.setBounds(412, 272, 48, 20);
cp.add(jTextField1);
jLabel3.setBounds(342, 272, 64, 20);
jLabel3.setText("Karten Nr:");
cp.add(jLabel3);
jLabel4.setBounds(528, 8, 222, 25);
jLabel4.setText("");
cp.add(jLabel4);
jLabel5.setBounds(322, 296, 166, 20);
jLabel5.setText("");
cp.add(jLabel5);
jLabel6.setBounds(344, 152, 30, 20);
jLabel6.setText("Herr:");
jLabel6.setVisible(false);
cp.add(jLabel6);
jLabel7.setBounds(376, 152, 110, 20);
jLabel7.setText("");
jLabel7.setVisible(false);
cp.add(jLabel7);
jButton3.setBounds(656, 528, 115, 25);
jButton3.setText("Zurück");
jButton3.setMargin(new Insets(2, 2, 2, 2));
jButton3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton3_ActionPerformed(evt);
}
});
jButton3.setVisible(false);
cp.add(jButton3);
jLabel8.setBounds(295, 192, 210, 20);
jLabel8.setText("Bitte geben Sie Ihre Geheimzahl ein:");
jLabel8.setVisible(false);
cp.add(jLabel8);
jTextField2.setBounds(375, 216, 50, 20);
jTextField2.setVisible(false);
cp.add(jTextField2);
jButton4.setBounds(340, 240, 120, 25);
jButton4.setText("Bestätigen");
jButton4.setMargin(new Insets(2, 2, 2, 2));
jButton4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton4_ActionPerformed(evt);
}
});
jButton4.setVisible(false);
cp.add(jButton4);
jButton5.setBounds(752, 8, 27, 25);
jButton5.setText("?");
jButton5.setMargin(new Insets(2, 2, 2, 2));
jButton5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton5_ActionPerformed(evt);
}
});
jButton5.setFont(new Font("Dialog", Font.BOLD, 16));
jButton5.setVisible(false);
jButton5.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent evt) {
jButton5_MouseEntered(evt);
jLabel9.setText("Geheimzahlen:");
jLabel10.setText("1: 1337");
jLabel11.setText("2: 2121");
jLabel12.setText("3: 2222");
jLabel13.setText("4: 1212");
jLabel14.setText("5: 1234");
}
});
jButton5.addMouseListener(new MouseAdapter() {
public void mouseExited(MouseEvent evt) {
jButton5_MouseExited(evt);
jLabel9.setText("");
jLabel10.setText("");
jLabel11.setText("");
jLabel12.setText("");
jLabel13.setText("");
jLabel14.setText("");
}
});
cp.add(jButton5);
jLabel9.setBounds(640, 8, 110, 20);
jLabel9.setText("");
cp.add(jLabel9);
jLabel10.setBounds(640, 48, 110, 20);
jLabel10.setText("");
cp.add(jLabel10);
jLabel11.setBounds(640, 72, 110, 20);
jLabel11.setText("");
cp.add(jLabel11);
jLabel12.setBounds(640, 96, 110, 20);
jLabel12.setText("");
cp.add(jLabel12);
jLabel13.setBounds(640, 120, 110, 20);
jLabel13.setText("");
cp.add(jLabel13);
jLabel14.setBounds(640, 144, 110, 20);
jLabel14.setText("");
cp.add(jLabel14);
jLabel15.setBounds(326, 272, 148, 20);
jLabel15.setText("");
cp.add(jLabel15);
// Ende Komponenten
setVisible(true);
} // end of public Geldautomat
// Anfang Methoden
public void jButton1_ActionPerformed(ActionEvent evt) {
int x;
x = Integer.parseInt(jTextField1.getText());
if (x==1) {
jLabel7.setText("Person 1");
jButton1.setVisible(false);
jButton5.setVisible(true);
jButton2.setVisible(false);
jTextField1.setVisible(false);
jLabel2.setVisible(false);
jLabel3.setVisible(false);
jLabel4.setVisible(false);
jLabel5.setVisible(false);
jLabel6.setVisible(true);
jLabel7.setVisible(true);
jButton3.setVisible(true);
jLabel8.setVisible(true);
jTextField2.setVisible(true);
jButton4.setVisible(true);
}
else if (x==2) {
jLabel7.setText("Person 2");
jButton1.setVisible(false);
jButton2.setVisible(false);
jButton5.setVisible(true);
jTextField1.setVisible(false);
jLabel2.setVisible(false);
jLabel3.setVisible(false);
jLabel4.setVisible(false);
jLabel5.setVisible(false);
jLabel6.setVisible(true);
jLabel7.setVisible(true);
jButton3.setVisible(true);
}
else if (x==3) {
jLabel7.setText("Person 3");
jButton1.setVisible(false);
jButton5.setVisible(true);
jButton2.setVisible(false);
jTextField1.setVisible(false);
jLabel2.setVisible(false);
jLabel3.setVisible(false);
jLabel4.setVisible(false);
jLabel5.setVisible(false);
jLabel6.setVisible(true);
jLabel7.setVisible(true);
jButton3.setVisible(true);
}
else if (x==4) {
jLabel7.setText("Person 4");
jButton1.setVisible(false);
jButton5.setVisible(true);
jButton2.setVisible(false);
jTextField1.setVisible(false);
jLabel2.setVisible(false);
jLabel3.setVisible(false);
jLabel4.setVisible(false);
jLabel5.setVisible(false);
jLabel6.setVisible(true);
jLabel7.setVisible(true);
jButton3.setVisible(true);
}
else if (x==5) {
jLabel7.setText("Person 5");
jButton1.setVisible(false);
jButton2.setVisible(false);
jButton5.setVisible(true);
jTextField1.setVisible(false);
jLabel2.setVisible(false);
jLabel3.setVisible(false);
jLabel4.setVisible(false);
jLabel5.setVisible(false);
jLabel6.setVisible(true);
jLabel7.setVisible(true);
jButton3.setVisible(true);
}
else {
jLabel5.setText("Unbekannte Kartennummer");
} // end of if-else
}
public void jButton2_MouseEntered(MouseEvent evt) {
// TODO hier Quelltext einfügen
} // end of jButton2_MouseEntered
public void jButton2_MouseExited(MouseEvent evt) {
// TODO hier Quelltext einfügen
} // end of jButton2_MouseExited
public void jButton3_ActionPerformed(ActionEvent evt) {
jTextField1.setText("");
jLabel15.setText("");
jButton1.setVisible(true);
jButton2.setVisible(true);
jTextField1.setVisible(true);
jLabel2.setVisible(true);
jLabel3.setVisible(true);
jLabel4.setVisible(true);
jLabel5.setVisible(true);
jLabel6.setVisible(false);
jLabel7.setVisible(false);
jLabel8.setVisible(false);
jTextField2.setVisible(false);
jButton4.setVisible(false);
jButton3.setVisible(false);// TODO hier Quelltext einfügen
} // end of jButton3_ActionPerformed
public void jButton4_ActionPerformed(ActionEvent evt) {
int x,pin;
x = Integer.parseInt(jTextField1.getText());
pin = Integer.parseInt(jTextField2.getText());
{
if (pin==1337)
{
if (x==1) {
jLabel8.setVisible(false);
jTextField2.setVisible(false);
jButton4.setVisible(false);
jButton3.setText("Karte Ausgeben");
jButton5.setVisible(false);
}
}
else {
jTextField1.setText("");
jLabel15.setText("");
jButton1.setVisible(true);
jButton2.setVisible(true);
jTextField1.setVisible(true);
jLabel2.setVisible(true);
jLabel3.setVisible(true);
jLabel4.setVisible(true);
jLabel5.setVisible(true);
jLabel6.setVisible(false);
jLabel7.setVisible(false);
jLabel8.setVisible(false);
jTextField2.setVisible(false);
jButton4.setVisible(false);
jButton3.setVisible(false);
}
} // end of if
}
public void jButton5_ActionPerformed(ActionEvent evt) {
// TODO hier Quelltext einfügen
} // end of jButton5_ActionPerformed
public void jButton5_MouseEntered(MouseEvent evt) {
// TODO hier Quelltext einfügen
} // end of jButton5_MouseEntered
public void jButton5_MouseExited(MouseEvent evt) {
// TODO hier Quelltext einfügen
} // end of jButton5_MouseExited
// Ende Methoden
public static void main(String[] args) {
new Geldautomat("Geldautomat");
} // end of main
} // end of class Geldautomat
Das ganze funktioniert so wie es momentan ist, nur das man direkt beim 1. Falschen versuch zurück zum "Anfang" kommt.
Ich hatte es vorher bereits mit
Java:
int i = 0;
for (;i < 3;i++ )
{
if (pin==1337)
{
if (x==1) {
jLabel8.setVisible(false);
jTextField2.setVisible(false);
jButton4.setVisible(false);
jButton3.setText("Karte Ausgeben");
jButton5.setVisible(false);
}
}
if (i==2) {
jTextField1.setText("");
jLabel15.setText("");
jButton1.setVisible(true);
jButton2.setVisible(true);
jTextField1.setVisible(true);
jLabel2.setVisible(true);
jLabel3.setVisible(true);
jLabel4.setVisible(true);
jLabel5.setVisible(true);
jLabel6.setVisible(false);
jLabel7.setVisible(false);
jLabel8.setVisible(false);
jTextField2.setVisible(false);
jButton4.setVisible(false);
jButton3.setVisible(false);
} // end of if
versucht allerdings hat das auch nicht funktioniert, warum weiß ich auch nicht aber ich glaube er hat direkt alle durchgänge beim 1. Button klick vollzogen und was anderes fällt mir momentan zur Lösung des Problems nicht ein... Wenn jemand ein Vorschlag hätte wäre das sehr hilfreich
Danke im Vorraus
Zuletzt bearbeitet: