我正在设计一个需要登录页面的应用程序,然后如果登录成功,它应该加载一个新的屏幕。这是代码:
import java.util.ArrayList;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class LoginPage extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Sign in here");
GridPane grid = new GridPane();
grid.setVgap(20);
grid.setPadding(new Insets(25, 10, 10, 25));
//Welcome title
Text scenetitle = new Text("Welcome");
scenetitle.setFont(Font.font("Tahoma", FontWeight.BOLD, 20));
grid.add(scenetitle, 0, 0, 2, 1);
//Enter username and label
Label userName = new Label("Enter your Username:");
grid.add(userName, 0, 1);
TextField userTextField = new TextField();
grid.add(userTextField, 1, 1);
//Enter Password
Label pw = new Label("Enter your password:");
grid.add(pw, 0, 2);
PasswordField pwBox = new PasswordField();
grid.add(pwBox, 1, 2);
//Button to login
Button btn = new Button("Login");
HBox hbBtn = new HBox(10);
hbBtn.setAlignment(Pos.BOTTOM_RIGHT);
hbBtn.getChildren().add(btn);
grid.add(hbBtn, 1, 4);
final Text actiontarget = new Text();
grid.add(actiontarget, 1, 6);
//Set usernames and passwords
ArrayList<String> userNames = new ArrayList<String>();
ArrayList<String> passWord = new ArrayList<String>();
//Add them in here
userNames.add("A");
passWord.add("A");
//display login success when button is pressed
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
String username = userTextField.getText();
String password = pwBox.getText();
//Check if correct here
if(userNames.contains(username)&&passWord.contains(password)){
actiontarget.setFill(Color.GREEN);
actiontarget.setText("Login Sucessful");
}else{
actiontarget.setFill(Color.RED);
actiontarget.setText("Login Unsucessful");
}
}
});
//Display
Scene scene = new Scene(grid, 300, 275);
primaryStage.setScene(scene);
primaryStage.show();
}
}
如果登录成功,则应加载到另一个屏幕。这个屏幕将被称为HomePage - 我还没有写,但我该怎么做?会不会是
if(userNames.contains(username)&&passWord.contains(password)){
actiontarget.setFill(Color.GREEN);
actiontarget.setText("Login Sucessful");
HomePage homePage = new HomePage();
}else{
actiontarget.setFill(Color.RED);
actiontarget.setText("Login Unsucessful");
这会有用还是有另一种方法可以做到这一点? 感谢。
答案 0 :(得分:0)
提取代码并创建一个方法,为您显示登录屏幕...
public boolean displayLoginPage(final Stage owner) {
Stage stage = new Stage();
stage.initOwner(owner);
stage.setTitle("Sign in here");
GridPane grid = new GridPane();
grid.setVgap(20);
grid.setHgap(5);
grid.setPadding(new Insets(25, 10, 10, 25));
//Welcome title
Text sceneTitle = new Text("Welcome");
sceneTitle.setFont(Font.font("Tahoma", FontWeight.BOLD, 20));
grid.add(sceneTitle, 0, 0, 2, 1);
//Enter username and label
Label userName = new Label("Enter your Username:");
grid.add(userName, 0, 1);
TextField userTextField = new TextField();
grid.add(userTextField, 1, 1);
//Enter Password
Label pw = new Label("Enter your password:");
grid.add(pw, 0, 2);
PasswordField pwBox = new PasswordField();
grid.add(pwBox, 1, 2);
//Button to login
Button btn = new Button("Login");
HBox hbBtn = new HBox(10);
hbBtn.setAlignment(Pos.BOTTOM_RIGHT);
hbBtn.getChildren().add(btn);
grid.add(hbBtn, 1, 4);
final Text actiontarget = new Text();
grid.add(actiontarget, 1, 6);
//Set usernames and passwords
List<String> userNames = new ArrayList<String>();
List<String> passWord = new ArrayList<String>();
//Add them in here
userNames.add("A");
passWord.add("A");
final AtomicBoolean loginSuccessful = new AtomicBoolean();
//display login success when button is pressed
btn.setOnAction((ActionEvent e) -> {
String username = userTextField.getText();
String password = pwBox.getText();
//Check if correct here
if (userNames.contains(username) && passWord.contains(password)) {
actiontarget.setFill(Color.GREEN);
actiontarget.setText("Login Successful");
loginSuccessful.set(true);
stage.close();
} else {
actiontarget.setFill(Color.RED);
actiontarget.setText("Login UnSuccessful");
loginSuccessful.set(false);
stage.close();
}
});
//Display
Scene scene = new Scene(grid, 400, 300);
stage.setScene(scene);
stage.showAndWait();
return loginSuccessful.get();
}
然后在你的开始方法......
@Override
public void start(Stage primaryStage) throws Exception {
BorderPane root = new BorderPane();
VBox vBox = new VBox(5, new Label("Hello World"));
root.setCenter(vBox);
Scene scene = new Scene(root, 400, 300);
boolean loginSuccessful = displayLoginPage(primaryStage);
if (loginSuccessful) {
primaryStage.setScene(scene);
primaryStage.show();
}
}
显然这并不完美,但它会让你开始
答案 1 :(得分:0)
我建议你从这开始: 你的主要场景:
public class MainApp extends Application {
private static Stage primaryStage;
public MainApp() {
}
@Override
public void start(Stage primaryStage) {
MainApp.primaryStage = primaryStage;
MainApp.primaryStage.setTitle("test");
loginScene();
//方法loginSuccessful()
if (loginSuccessful()) {
HomeScene();
}
}
private void loginScene() {
try {
FXMLLoader loader = new FXMLLoader();
//path to your login fxml file
loader.setLocation(MainApp.class.getResource("/fxml/LoginScene.fxml"));
rootLayout = loader.load();
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
//
public void HomeScene() throws IOException {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("/fxml/HomeScene.fxml"));
AnchorPane homeScenePane = loader.load();
rootLayout.setCenter(homeScenePane );
HomeSceneController controller = loader.getController();
controller.setMainApp(this);
} catch (IOException e) {
e.printStackTrace();
}
}
}
同时创建控制器HomeSceneController
等等......
希望它可以帮助你开始