Poznając Jave stwierdziłem, że dobrym pomysłem byłoby również liźnięcie JavaFX. Na start zacząłem od małego projektu, okienka logowania ze sprawdzeniem danych, które mieszczą się w .txt
Klasa MainWindow: (ew. pastebin)
package sas.kacper.itemsapp;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.xml.bind.DatatypeConverter;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.control.PasswordField;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ButtonType;
import javafx.scene.layout.VBox;
import javafx.scene.layout.HBox;
import javafx.scene.Node;
import javafx.scene.text.Text;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import sas.kacper.itemsapp.LoginAuthenticator;
public class MainWindow extends Application {
@Override
public void start(Stage primaryStage) {
VBox mainPane = new VBox(10);
Image iLogo = new Image("file:java.png", true);
ImageView ivLogo = new ImageView();
ivLogo.setImage(iLogo);
Text lText = new Text("Nazwa użytkownika");
Text pText = new Text("Hasło");
TextField lField = new TextField();
PasswordField pField = new PasswordField();
Button bLogin = new Button("Log In!");
bLogin.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
String userLogin="", userHash="";
userLogin = lField.getText();
try {
MessageDigest crypt = MessageDigest.getInstance("MD5");
crypt.update((pField.getText()).getBytes());
userHash = (DatatypeConverter.printHexBinary(crypt.digest())).toLowerCase();
} catch(NoSuchAlgorithmException exc) {
exc.printStackTrace();
}
Alert aAlert = new Alert(AlertType.INFORMATION);
((Stage)aAlert.getDialogPane().getScene().getWindow()).getIcons().add(new Image("file:java.png"));
aAlert.setContentText("Wszystko ok!");
aAlert.setTitle("Zalogowany!");
if(!(LoginAuthenticator.checkLogin(userLogin, userHash))) {
aAlert.setAlertType(AlertType.ERROR);
aAlert.setContentText("Coś poszło nie tak!");
aAlert.setTitle("Błąd");
}
aAlert.setHeaderText(null);
aAlert.showAndWait();
}
});
lField.setMaxWidth(250);
pField.setMaxWidth(250);
lField.setAlignment(Pos.CENTER);
pField.setAlignment(Pos.CENTER);
mainPane.setAlignment(Pos.CENTER);
mainPane.setStyle("-fx-background-color: #FFFFFF;");
mainPane.getChildren().addAll(ivLogo, new Text(), new Text(), lText, lField, pText, pField, new Text(), bLogin);
Scene mainScene = new Scene(mainPane, 450, 600);
primaryStage.setTitle("Log In!");
primaryStage.setResizable(false);
primaryStage.setScene(mainScene);
primaryStage.getIcons().add(new Image("file:java.png"));
primaryStage.show();
}
public static void main(String ... args) {
launch(args);
}
}
Klasa LoginAuthenticator: (ew. pastebin)
package sas.kacper.itemsapp;
import java.lang.String;
import java.io.FileReader;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
public class LoginAuthenticator {
public static Boolean checkLogin(String Login, String hashPass) {
BufferedReader strFile = null;
try {
strFile = new BufferedReader(new FileReader("input.txt"));
} catch(FileNotFoundException e) {
e.printStackTrace();
}
String sLine = null;
try {
while((sLine = strFile.readLine()) != null) {
String[] data = sLine.split(":");
if(data[0].equals(Login)) {
if(data[1].equals(hashPass)) {
return true;
}
}
}
} catch(IOException e) {
e.printStackTrace();
}
return false;
}
}
Mam parę wątpliwości. Czy nie lepiej byłoby pozostawić w klasie MainWindow tylko i wyłącznie kod związany z widokiem? Może zamiast działać na klasie LoginAuthenticator to utworzyć obiekt, konstruktorem podesłać dane, potem wywołać metodę checkLogin i również z niej informować o rezultacie?
Jak to na początki przystało, MainWindow razi po oczach bałaganem. Jakieś wskazówki? Na co zwracać uwagę, czego nie robić? Za wszystkie podpowiedzi byłbym wdzięczny. Pozdrawiam.