Cześć,
mam problem z implementacją kodu odnośnie logowania (logowanie, rejestracja, przypomnienie danych) w Java Swing. Napisałem kod jeżeli chodzi o program konsolowy, ale nie potrafię przenieść tego na program okienkowy.
Czy jest ktoś w stanie mi z tym pomóc / nakierować mnie?
Pozdrawiam :)
Mój kod - konsolowy:
import java.io.*;
import java.util.Scanner;
public class Menu {
static void login() throws IOException {
String login, loginInput, password, passwordInput;
String fileName = "Users.txt";
Scanner scan = new Scanner(System.in);
System.out.println("Your Login: ");
loginInput = scan.nextLine();
System.out.println("Your Password: ");
passwordInput = scan.nextLine();
int i = 0;
boolean foundLogin = false;
boolean loggedIn = false;
BufferedReader br = new BufferedReader(new FileReader(fileName));
try {
int counter =0;
while ((login = br.readLine()) != null ) {
if (counter++ %2 != 0){
continue;
}
if (loginInput.equals(login)){
foundLogin = true;
if ((password = br.readLine()).equals(passwordInput)){
loggedIn = true;
menuAfterLogin();
} else{
if (foundLogin && !loggedIn){
System.out.println("Wrong password, try again.");
}
}
}
}
if (!foundLogin){
System.out.println("The username " +loginInput +" does not exist, try again.");
}
} finally {
br.close();
}
}
static void register () throws IOException {
Scanner scanner = new Scanner(System.in);
String login, password;
FileWriter file = new FileWriter("Users.txt", true);
BufferedWriter out = new BufferedWriter(file);
System.out.println("Your Login: ");
login = scanner.nextLine();
out.write(login + "\n");
System.out.println("Your Password: ");
password = scanner.nextLine();
out.write(password + "\n");
out.close();
System.out.println("Welcome " + login + " in 1001 world games!");
login();
}
static void forgot() throws IOException {
String login, loginInput, password;
String fileName = "Users.txt";
Scanner scan = new Scanner(System.in);
System.out.println("Your Login: ");
loginInput = scan.nextLine();
int i = 0;
boolean foundLogin = false;
BufferedReader br = new BufferedReader(new FileReader(fileName));
try {
int counter = 0;
while ((login = br.readLine()) != null) {
if (counter++ % 2 != 0) {
continue;
}
if (loginInput.equals(login)) {
foundLogin = true;
password = br.readLine();
System.out.print("Your Password is: ");
System.out.println(password);
}
}
if((!foundLogin)) {
System.out.println("The username " + loginInput + " does not exist, try again.");
}
} finally {
br.close();
}
}
}
Kod okienkowy:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Login extends JFrame {
public Login() {
setTitle("S N A K E - LOGIN");
setSize(400,300);
setResizable(false);
setLocationRelativeTo(null);
initComponents();
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
JPanel panel = new JPanel();
public void initComponents() {
JButton enter = new JButton("Enter");
enter.setSize(90, 30);
enter.setLocation(getWidth() - 310, getHeight() - 100);
JButton close = new JButton("Close");
close.setSize(90, 30);
close.setLocation(getWidth() - 190, getHeight() - 100);
JLabel text = new JLabel("HELLO, PLAYER!");
text.setBounds(150, -20, 100, 100);
JLabel login = new JLabel("Login:");
login.setSize(100, 30);
login.setLocation(getWidth() - 300, getHeight() - 220);
JLabel password = new JLabel("Password:");
password.setSize(100, 30);
password.setLocation(getWidth() - 300, getHeight() - 180);
JTextField userLogin = new JTextField(6);
userLogin.setSize(100, 30);
userLogin.setLocation(getWidth() - 200, getHeight() - 220);
String lg = userLogin.getText().toString();
JPasswordField userPassword = new JPasswordField(6);
userPassword.setSize(100, 30);
userPassword.setLocation(getWidth() - 200, getHeight() - 180);
char[] pw = userPassword.getName().toCharArray();
JButton register = new JButton("Register");
register.setSize(90, 30);
register.setLocation(getWidth() - 310, getHeight() - 75);
JButton forgot = new JButton("Forgot");
forgot.setSize(90, 30);
forgot.setLocation(getWidth() - 190, getHeight() - 75);
panel.setBackground(Color.GRAY);
;
panel.setLayout(null);
panel.add(text);
panel.add(login);
panel.add(password);
panel.add(userLogin);
panel.add(userPassword);
panel.add(close);
panel.add(enter);
panel.add(register);
panel.add(forgot);
this.getContentPane().add(panel);
enter.addActionListener(new bEnter());
close.addActionListener(new bClose());
register.addActionListener(new bRegister());
forgot.addActionListener(new bForgot());
}
class bClose implements ActionListener {
public void actionPerformed(ActionEvent e) {
dispose();
}
}
static class bEnter implements ActionListener {
public void actionPerformed(ActionEvent e) {
// jezeli user zal to rozpoczyna grę
// a jak nie to próbuje ponownie się zalogować
}
}
static class bRegister implements ActionListener {
public void actionPerformed(ActionEvent e) {
//rejestracja
}
}
static class bForgot implements ActionListener {
public void actionPerformed(ActionEvent e) {
// przypomnienie hasła
}
}
}