簡體   English   中英

JavaFX 場景 2 按鈕操作不起作用

[英]JavaFX Scene 2 button on actions not working

我有2個場景。 其中一個場景是登錄,另一個是注冊。 當我單擊登錄屏幕上的注冊按鈕時,它會將我切換到注冊場景。 我在 controller 中為按鈕所做的操作不起作用。 我是否需要在主要或 controller 中做一些事情才能讓 Register.fxml 工作?

模型控制器:

package application;

import javafx.event.ActionEvent;
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.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import java.sql.*;

public class ModelController extends Main{

@FXML
private Label LblStatus;

@FXML
private TextField UserEmail;

@FXML
private TextField UserPassword;

@FXML
private TextField RegisterEmail;

@FXML
private TextField RegisterPassword;

@FXML
private TextField RegisterFirst;

@FXML
private TextField RegisterLast;

@FXML
private TextField RegisterSSN;

@FXML
private TextField RegisterAddress;

@FXML
private TextField RegisterCity;

@FXML
private TextField RegisterState;

@FXML
private TextField RegisterZip;

@FXML
private Button LoginRegister;
    


public void Login(ActionEvent event) throws Exception {
    
    
    if(UserEmail.getText().equals("User") && UserPassword.getText().equals("pass")) {
        LblStatus.setText("Login Success");
        
        Stage primaryStage = new Stage();
        Parent root = FXMLLoader.load(getClass().getResource("/application/Main.fxml"));
        Scene scene = new Scene(root,400,400);
        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    else {
        LblStatus.setText("Login Failed");
    }
    
}

public void handleButtonAction(ActionEvent event) {
    try {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Register.FXML"));
        Parent root1 = (Parent) fxmlLoader.load();
        Scene Register = new Scene(root1);
        Stage window = (Stage)((Node)event.getSource()).getScene().getWindow();
        
        window.setScene(Register);
        window.show();

    }
    catch(Exception e) {
        
    }
}

public void CompleteRegistration(ActionEvent event) {
    
}

主要的:

package application;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.fxml.FXMLLoader;


public class Main extends Application {
@Override
public void start(Stage primaryStage) {
    try {
        Parent root = FXMLLoader.load(getClass().getResource("/application/Login.fxml"));
        Scene scene = new Scene(root,400,400);
        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.show();
    } catch(Exception e) {
        e.printStackTrace();
    }
}


public static void main(String[] args) {
    launch(args);
}
}

第二個場景沒有拉入 controller。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM