Hallo, habe einen Code gemacht der ein Fenster erstellt wo man sich einloggen muss. Wenn man sich eingeloggt hat mit den richtigen Passwort dann kommt ein neues Fenster mit Login Succeful .Aber ich weiß nicht wie ich dort ein Textfeld reinbekomme wo man was reinschreiben kann und speichern kann, was dann beim nächsten mal beim Starten , dass da der selbe Text immer noch ist den man reingeschrieben hat, also quasi eine Notiz die man auch wieder weiterschreiben und verändern kann .
package LoginMarvin;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MarvinEins extends JFrame implements ActionListener {
Icon x = new ImageIcon(getClass().getResource("HalloHallo.png"));
Container container = getContentPane();
JLabel userLabel = new JLabel("USERNAME");
JLabel passwordLabel = new JLabel("PASSWORD");
JTextField userTextField = new JTextField();
JPasswordField passwordField = new JPasswordField();
JButton loginButton = new JButton("LOGIN");
JButton resetButton = new JButton("RESET");
JButton hallo = new JButton("", x);
JCheckBox showPassword = new JCheckBox("Show Password");
MarvinEins() {
setLayoutManager();
setLocationAndSize();
addComponentsToContainer();
addActionEvent();
}
public void setLayoutManager() {
container.setLayout(null);
}
public void setLocationAndSize() {
userLabel.setBounds(50, 150, 100, 30);
passwordLabel.setBounds(50, 220, 100, 30);
userTextField.setBounds(150, 150, 150, 30);
passwordField.setBounds(150, 220, 150, 30);
showPassword.setBounds(150, 250, 150, 30);
loginButton.setBounds(50, 300, 100, 30);
resetButton.setBounds(200, 300, 100, 30);
hallo.setBounds(20, 350, 300, 200);
}
public void addComponentsToContainer() {
container.add(userLabel);
container.add(passwordLabel);
container.add(userTextField);
container.add(passwordField);
container.add(showPassword);
container.add(loginButton);
container.add(resetButton);
container.add(hallo);
}
public void addActionEvent() {
loginButton.addActionListener(this);
resetButton.addActionListener(this);
showPassword.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
//Coding Part of LOGIN button
if (e.getSource() == loginButton) {
String userText;
String pwdText;
userText = userTextField.getText();
pwdText = passwordField.getText();
if (userText.equalsIgnoreCase("Super") && pwdText.equalsIgnoreCase("Hallo")) {
JOptionPane.showMessageDialog(this, "Login Successful");
} else {
JOptionPane.showMessageDialog(this, "Invalid Username or Password");
}
}
//Coding Part of RESET button
if (e.getSource() == resetButton) {
userTextField.setText("");
passwordField.setText("");
}
//Coding Part of showPassword JCheckBox
if (e.getSource() == showPassword) {
if (showPassword.isSelected()) {
passwordField.setEchoChar((char) 0);
} else {
passwordField.setEchoChar('*');
}
}
}
}
nächste Klasse :
package LoginMarvin;
import javax.swing.JFrame;
public class MarvinZwei {
public static void main(String[] a) {
MarvinEins frame = new MarvinEins();
frame.setTitle("Login Form");
frame.setVisible(true);
frame.setBounds(10, 10, 370, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
}
}
package LoginMarvin;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MarvinEins extends JFrame implements ActionListener {
Icon x = new ImageIcon(getClass().getResource("HalloHallo.png"));
Container container = getContentPane();
JLabel userLabel = new JLabel("USERNAME");
JLabel passwordLabel = new JLabel("PASSWORD");
JTextField userTextField = new JTextField();
JPasswordField passwordField = new JPasswordField();
JButton loginButton = new JButton("LOGIN");
JButton resetButton = new JButton("RESET");
JButton hallo = new JButton("", x);
JCheckBox showPassword = new JCheckBox("Show Password");
MarvinEins() {
setLayoutManager();
setLocationAndSize();
addComponentsToContainer();
addActionEvent();
}
public void setLayoutManager() {
container.setLayout(null);
}
public void setLocationAndSize() {
userLabel.setBounds(50, 150, 100, 30);
passwordLabel.setBounds(50, 220, 100, 30);
userTextField.setBounds(150, 150, 150, 30);
passwordField.setBounds(150, 220, 150, 30);
showPassword.setBounds(150, 250, 150, 30);
loginButton.setBounds(50, 300, 100, 30);
resetButton.setBounds(200, 300, 100, 30);
hallo.setBounds(20, 350, 300, 200);
}
public void addComponentsToContainer() {
container.add(userLabel);
container.add(passwordLabel);
container.add(userTextField);
container.add(passwordField);
container.add(showPassword);
container.add(loginButton);
container.add(resetButton);
container.add(hallo);
}
public void addActionEvent() {
loginButton.addActionListener(this);
resetButton.addActionListener(this);
showPassword.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
//Coding Part of LOGIN button
if (e.getSource() == loginButton) {
String userText;
String pwdText;
userText = userTextField.getText();
pwdText = passwordField.getText();
if (userText.equalsIgnoreCase("Super") && pwdText.equalsIgnoreCase("Hallo")) {
JOptionPane.showMessageDialog(this, "Login Successful");
} else {
JOptionPane.showMessageDialog(this, "Invalid Username or Password");
}
}
//Coding Part of RESET button
if (e.getSource() == resetButton) {
userTextField.setText("");
passwordField.setText("");
}
//Coding Part of showPassword JCheckBox
if (e.getSource() == showPassword) {
if (showPassword.isSelected()) {
passwordField.setEchoChar((char) 0);
} else {
passwordField.setEchoChar('*');
}
}
}
}
nächste Klasse :
package LoginMarvin;
import javax.swing.JFrame;
public class MarvinZwei {
public static void main(String[] a) {
MarvinEins frame = new MarvinEins();
frame.setTitle("Login Form");
frame.setVisible(true);
frame.setBounds(10, 10, 370, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
}
}