java - 如何在新的 fxml javaFX0 上重定向用户
问题描述
我正在尝试在用户连接到数据库后重定向用户。
这是我使用登录方法的控制器:
package packages.home;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.TextField;
import javafx.scene.control.Label;
import javafx.scene.control.Button;
import static javax.swing.JOptionPane.showMessageDialog;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Controller extends Thread {
@FXML
private Button btnClose;
@FXML
private TextField txtUsername;
@FXML
private TextField txtPassword;
@FXML
private Label lblErrors;
@FXML
private Button btnConnect;
/// --
Connection laConnection = GestionConnection.getLaConnection();
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
public void handleButtonAction(MouseEvent event) throws IOException{
if (event.getSource() == btnClose) {
System.exit(0);
System.out.println("Fermeture de l'application.");
}
if (event.getSource() == btnConnect) {
//login here
if (logIn().equals("Success")) {
try {
//add you loading or delays - ;-)
Node node = (Node) event.getSource();
Stage stage = (Stage) node.getScene().getWindow();
//stage.setMaximized(true);
stage.close();
Scene scene = new Scene(FXMLLoader.load(getClass().getResource("Login.fxml")));
stage.setScene(scene);
stage.show();
} catch (IOException ex) {
System.err.println(ex.getMessage());
}
}
}
}
private String logIn() {
String status = "Success";
String username = txtUsername.getText();
String password = txtPassword.getText();
if(username.isEmpty() || password.isEmpty()) {
setLblErrors(Color.RED, "Champs vides.");
status = "Error";
} else {
//query
String sql = "SELECT * FROM admins Where username = ? and password = ?";
try {
// On test la requête
System.out.println("Hello");
preparedStatement = laConnection.prepareStatement(sql);
preparedStatement.setString(1, username);
preparedStatement.setString(2, password);
resultSet = preparedStatement.executeQuery();
if (!resultSet.next()) {
setLblErrors(Color.TOMATO, "Nom d'utilisateur ou mot de passe incorrect");
status = "Error";
} else {
setLblErrors(Color.GREEN, "Authentification réussite, redirection en cours ...");
showMessageDialog(null, "Vous êtes bien connecté");
}
} catch (SQLException ex) {
System.err.println(ex.getMessage());
status = "Exception";
}
}
return status;
}
private void setLblErrors(Color color, String text) {
lblErrors.setTextFill(color);
lblErrors.setText(text);
System.out.println(text);
}
}
这是我的新 fxml 控制器:
package packages.home;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.layout.AnchorPane;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
public class DashboardController implements Initializable {
@FXML
private AnchorPane rootPane;
public DashboardController(AnchorPane rootPane) {
this.rootPane = rootPane;
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
}
@FXML
private void loadSecond(ActionEvent event) throws IOException{
AnchorPane pane = FXMLLoader.load(getClass().getResource("dashboard.fxml"));
rootPane.getChildren().setAll(pane);
}
}
我确实知道如何在我的第一个控制器和我的新控制器之间建立链接。
我是 javaFX 的新手,我很乐意学习有关它的新东西。
在此先感谢您的帮助。
解决方案
静态方式
例如:
public class Main {
public static void main(String[] args) { ... }
public static Scene scene;
@Override
public void start (Stage stage) {
...
scene = new Scene(FXMLoader.load(...)); //Controller fxml
stage.setScene(scene);
...
}
public static void setRoot(AnchorPane pane, Parent parent) {
pane.getChildren().clear();
scene.setRoot(parent);
}
}
在Controller.java
创建@FXML
对正在使用的引用AnchorPane
并将其传递到函数中。
if (connected) {
Main.setRoot(pane, FXMLoader.load(...)); //new screen fxml
}
编辑:获取参考
public void setRoot(AnchorPane pane, Parent parent) {
pane.getChildren().clear();
pane.getScene().setRoot(parent);
}
不要为场景调用控制器getScene()
的initialize
方法将null
.
if (connected) {
setRoot(pane, FXMLoader.load(...)); //new screen fxml
}
推荐阅读
- python - 使用带有 IsolationForest 的 GridSearchCV 来查找异常值
- java - Python 和 Java 中的 AES-128 加密但溢出错误,无符号字节整数小于最小值
- sql - Count(*) 与内部连接和分组依据
- javascript - React-Native - 错误:`StackViewTransitionConfigs` 已被删除
- azure - 在自托管 Web 应用上具有托管服务标识的 Azure Key Vault
- pointers - 在 Go 中使用从 Gorm 查询的指针来操作结构?
- powerbi - power bi 中的多重搜索
- teradata - 我对此日期格式更改有疑问
- liferay - 带有可选参数的 Liferay 友好 URL
- string - 如何将字符串“[1,2,3]”转换为 Vec
?