【问题标题】:Javafx - I want to redirect the user to a new scene if the user entered password and username correctlyJavafx - 如果用户正确输入密码和用户名,我想将用户重定向到新场景
【发布时间】:2021-09-23 07:47:48
【问题描述】:

我正在使用 javaFX 制作登录系统。当用户输入正确的用户名和密码时,我想将他重定向到“home.fxml”我该怎么做,目前当用户输入正确的数据时我正在打印一条消息。我可以单独创建一个方法来链接到一个新场景,但是当我在 if 语句中调用它时,它会要求参数。

package sample;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
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.stage.Stage;
import javafx.event.ActionEvent;

import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.ResourceBundle;

public class Controller {
    @FXML
    private Button cancelbutton;
    @FXML
    private Label loginmessagelabel;
    @FXML
    private PasswordField password1;
    @FXML
    private TextField username1;
    PreparedStatement pst;
    ResultSet rs;
    @FXML
    private Stage stage;
    private Scene scene;
    private Parent root;


    public void validatelogin() throws SQLException, ClassNotFoundException, IOException {
        String jdbcURL = "jdbc:mysql://localhost/medibase";
        String username = "root";
        String password = "0852";
        Connection connection = DriverManager.getConnection(jdbcURL,username,password);
        Class.forName("com.mysql.jdbc.Driver");
        String uname = username1.getText();
        String psd = password1.getText();
        pst=connection.prepareStatement("SELECT * FROM user_account WHERE username=? and password=?");
        pst.setString(1, uname);
        pst.setString(2, psd);
        rs = pst.executeQuery();
        if(rs.next()){
        loginmessagelabel.setText("Congratulations");
   


           

        } else{
            loginmessagelabel.setText("Login failed");
        }

    }

我可以创建一个像下面这样的方法,但是当我在 if 语句中调用它时它要求参数

public void switchtoHome(ActionEvent event)throws IOException {
    Parent root = FXMLLoader.load(getClass().getResource("home.fxml"));
    stage = (Stage)((Node)event.getSource()).getScene().getWindow();
    scene=new Scene(root);
    stage.setScene(scene);
    stage.show();
}

【问题讨论】:

标签: java mysql javafx


【解决方案1】:

Question about remembering the logged user上查看我的回答

具体是changeToMainWindow()方法,登录成功时调用该方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-06
    • 2016-02-04
    • 2020-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-04
    相关资源
    最近更新 更多