Halli hallo, ich hab grade langeweile und Code so vor mich hin, ich habe einen Login Handler geschrieben welcher Benutzereingaben mit einer statischen map mit Accounts abgleicht und daraufhin eine ihm übergebene Anwendung ausführt. ( Ich verwende hier absichtlich keine Datenbank, ich spiel nur ein bischen rum ).
Die Applikation setzt sich bis jetzt folgendermaßen zusammen:
und das funktionale Interface welches die auszuführenden Anwendungen Implementieren müssen :
hier noch der aufruf in der main Methode:
nun passiert aber folgendes , durch umschreiben des Konstruktors
habe ich nun versucht im dem als inneren klasse definierten Listener auf dem Elternelement bei erfolgreichem Login dispose(); aufzurufen
die IDE meckert nicht, lässt sich auch kompilieren,
allerdings wird anstatt das JFrame abzubauen, nur der Inhalt des JPanels abgebaut, kann mir vielleicht einer erklären warum ?!
ich rufe ja
auf dem Elternelement ( JFrame ) auf , normalerweise sollte hier das Frame abgebaut werden und nicht dessen inhalt.
[EDIT]Hat sich erledigt, lag an dem JOptionPane, das durch seine kurze lebensdauer bis zum Buttonklick auch direkt mit abgebaut wurde, funktioniert nun reibungslos ![/EDIT]
Die Applikation setzt sich bis jetzt folgendermaßen zusammen:
Java:
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map.Entry;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Login extends JPanel {
private static final long serialVersionUID = 1L;
final static Dimension PANEL_SIZE = new Dimension(200,100);
final static Dimension FIELD_SIZE = new Dimension(120,25);
final static GridBagConstraints CONSTRAINTS = new GridBagConstraints();
private JTextField nameField;
private JPasswordField passField;
private JFrame parent;
private Application application;
public Login(JFrame parent, Application application) {
this.parent = parent;
this.application = application;
setSize(PANEL_SIZE);
setLayout(new GridBagLayout());
String[] labelingTexts ={"Benutername :","Passwort :","Einloggen",};
JLabel name = new JLabel(labelingTexts[0]);
JLabel pass = new JLabel(labelingTexts[1]);
this.nameField = new JTextField();
this.nameField.setPreferredSize(FIELD_SIZE);
this.passField = new JPasswordField();
this.passField.setPreferredSize(FIELD_SIZE);
JButton btn = new JButton(labelingTexts[2]);
btn.addActionListener(new ButtonListener());
CONSTRAINTS.weightx = 1;
CONSTRAINTS.insets = new Insets(5, 5, 5, 5);
CONSTRAINTS.gridx = 0;
CONSTRAINTS.gridy = 0;
this.add(name, CONSTRAINTS);
CONSTRAINTS.gridx = 1;
CONSTRAINTS.gridy = 0;
this.add(nameField, CONSTRAINTS);
CONSTRAINTS.gridx = 0;
CONSTRAINTS.gridy = 1;
this.add(pass, CONSTRAINTS);
CONSTRAINTS.gridx = 1;
CONSTRAINTS.gridy = 1;
this.add(passField, CONSTRAINTS);
CONSTRAINTS.gridx = 0;
CONSTRAINTS.gridy = 2;
CONSTRAINTS.gridwidth = 2;
this.add(btn, CONSTRAINTS);
}
private boolean checkState(final String name, final String password){
boolean state = false;
for (Entry<String, Account> entry : Account.accounts.entrySet()) {
if (entry.getValue().proof(name, password)) {
state = true;
}
else{
state = false;
}
}
return state;
}
class ButtonListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
if (checkState(nameField.getText(), String.valueOf(passField.getPassword()))) {
JOptionPane.showMessageDialog(null, "Login succsessful !");
application.execute();
parent.dispose();
}
else{
JOptionPane.showMessageDialog(null, "Access denied !");
nameField.setText("");
passField.setText("");
}
}
}
}
Java:
import java.util.HashMap;
import java.util.Map.Entry;
/**
* Represents a single Account
*
* <br></br>
*
* <b>Note :</b>
*
* Contains a static {@link HashMap} with all constructed Account objects
*
*
* @author EscaFlow
* @version 1.0
*
*/
public final class Account {
static HashMap<String, Account> accounts = new HashMap<>();
static int id = 0;
private String name;
private String password;
public Account(final String name, final String password) {
this.name = name;
this.password = password;
id++;
accounts.put(name, this);
}
private String getPass(){
return this.password;
}
public String getName(){
return this.name;
}
public String[] getAccountList(){
String[] arr = new String[accounts.size()];
for (int i = 0; i < accounts.size(); i++) {
for (Entry<String, Account> entry : accounts.entrySet()) {
arr[i] = entry.getKey();
}
}
return arr;
}
public boolean proof(final String name, final String password){
boolean accept = false;
for (Entry<String, Account> entry : accounts.entrySet()) {
if (entry.getKey().equals(name) && entry.getValue().getPass().equals(password)) {
accept = true;
}
}
return accept;
}
}
und das funktionale Interface welches die auszuführenden Anwendungen Implementieren müssen :
Java:
public interface Application {
public void execute();
}
hier noch der aufruf in der main Methode:
Java:
public class LoginTest {
public static void main(String[] args) {
new Account("admin", "admin");
new Account("Peter", "Lustig");
JFrame f = new JFrame("Login");
Login l = new Login(f, new Frame());
f.setDefaultCloseOperation(2);
f.add(l);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
nun passiert aber folgendes , durch umschreiben des Konstruktors
Java:
public Login(JFrame parent, Application application){..}
habe ich nun versucht im dem als inneren klasse definierten Listener auf dem Elternelement bei erfolgreichem Login dispose(); aufzurufen
Java:
if (checkState(nameField.getText(), String.valueOf(passField.getPassword()))) {
JOptionPane.showMessageDialog(null, "Login succsessful !");
application.execute();
parent.dispose(); // <<---
}
die IDE meckert nicht, lässt sich auch kompilieren,
allerdings wird anstatt das JFrame abzubauen, nur der Inhalt des JPanels abgebaut, kann mir vielleicht einer erklären warum ?!
ich rufe ja
Code:
dispose();
[EDIT]Hat sich erledigt, lag an dem JOptionPane, das durch seine kurze lebensdauer bis zum Buttonklick auch direkt mit abgebaut wurde, funktioniert nun reibungslos ![/EDIT]
Zuletzt bearbeitet: