繁体   English   中英

如何在FXML Controller中设置新场景

[英]How do I set a new scene from within FXML Controller

我是编程新手,仅在过去两周才开始学习,因此对任何多余或草率的代码感到抱歉...

我有2个场景,在我的主班上。 但是我正在使用FXML开发每个场景,并且所有代码都已放置在第一个场景的FXML Controller中。 我准备开始构建第二个场景,但是不知道如何正确启动它。

我的问题是,如何设置舞台以显示第二个场景(mainCallWindow),特别是从第一个FXML文件的控制器类中显示。 如果有更好的方法,请告诉我。

主类:

package supportTool;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.*;
import javafx.stage.Stage;

public class Main extends Application {

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

    public String versionNumber = "v2.1";

    @Override
    public void start(Stage primaryStage) throws Exception {
        // SETTING UP THE STAGE
        Stage window;
        window = primaryStage;
        window.setTitle("Support Tool " + versionNumber);
        // SETTING UP THE SCENES
        Parent newCallDetailsFXML = FXMLLoader.load(getClass().getResource("newCallDetails.fxml"));
        Parent mainCallWindowFXML = FXMLLoader.load(getClass().getResource("mainCallWindow.fxml"));
        Scene newCallDetails = new Scene (newCallDetailsFXML, 800, 600);
        Scene mainCallWindow = new Scene (mainCallWindowFXML, 800, 600);
        // CHOOSING THE SCENE AND SHOWING THE STAGE
        window.setScene(newCallDetails);
        window.show();
    }
}

场景1 FXML控制器:

package supportTool;

import javafx.scene.control.*;
import javafx.scene.image.*;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class newCallController {

    private int maxChar;
    public ChoiceBox choiceAccount;
    public ImageView btnCall;
    public TextField tfCallbackNumber;
    public TextField tfCallerName;
    public TextField tfStoreNumber;

    // ACTION COMPLETED WHEN CALL BUTTON IS PRESSED
    public void btnCall() {
        Caller newCaller = new Caller();
        newCaller.setCallerName(tfCallerName.getText());
        newCaller.setCallbackNumber(tfCallbackNumber.getText());
        newCaller.setAccount(String.valueOf(choiceAccount.getValue()));
        newCaller.setStoreNumber(tfStoreNumber.getText());
        try {
            FileOutputStream fos = new FileOutputStream("caller.bin");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(newCaller);
            oos.close();
            fos.close();
        } catch(IOException e){
            e.printStackTrace();
        }           
        // RIGHT HERE IS WHERE I WANT TO SET THE SCENE TO "mainCallWindow"
    }

    // CHECKS TO SEE IF THE TEXT CONTAINS ONLY LETTERS
    private boolean isNumberCheckEvent(String message) {
        if (message.matches("[0-9]+")) {
            return true;
        } else {
            return false;
        }
    }

    // SETS THE MAX CHARACTERS FOR ALL TEXTFIELDS
    public void maxCharEvent() {
        // CALLER NAME MAX CHARACTERS
        tfCallerName.setOnKeyTyped(maxCharEvent -> {
            maxChar = 20;
            if(tfCallerName.getText().length() >= maxChar) {
                maxCharEvent.consume();
            }
        });
        // CALLBACK NUMBER MAX CHARACTERS
        tfCallbackNumber.setOnKeyTyped(maxCharEvent -> {
            maxChar = 10;
            if(tfCallbackNumber.getText().length() >= maxChar) {
                maxCharEvent.consume();
            }
        });
        // STORE NUMBER MAX CHARACTERS
        tfStoreNumber.setOnKeyTyped(maxCharEvent -> {
            maxChar = 5;
            if (String.valueOf(choiceAccount.getValue()).equals("6 Digit Account")) {
                maxChar = 6;
            }
            if (tfStoreNumber.getText().length() >= maxChar) {
                maxCharEvent.consume();
            }
        });
    }

    // CHANGES TEXT TO ONLY LETTERS BASED ON isNumberCheckEvent
    public void numberValidationEvent() {
        tfCallbackNumber.setOnKeyReleased(numberValidationEvent -> {
            maxCharEvent();
            if(tfCallbackNumber.getText().length() > 0) {
                if (!isNumberCheckEvent(tfCallbackNumber.getText())) {
                    tfCallbackNumber.setText(tfCallbackNumber.getText().substring(0, tfCallbackNumber.getText().length() - 1));
                    tfCallbackNumber.positionCaret(10);
                    numberValidationEvent.consume();
                }
            }
        });
        tfStoreNumber.setOnKeyReleased(numberValidationEvent -> {
            maxCharEvent();
            if(tfStoreNumber.getText().length() > 0) {
                if (!isNumberCheckEvent(tfStoreNumber.getText())) {
                    tfStoreNumber.setText(tfStoreNumber.getText().substring(0, tfStoreNumber.getText().length() - 1));
                    tfStoreNumber.positionCaret(10);
                    numberValidationEvent.consume();
                }
            }
        });
    }
}

您可以通过多种方式更改场景。 在当前情况下,您可以尝试以下操作。 首先,您需要引用FXMLLoader,场景和舞台以从控制器更改场景。 而不是在主类中加载,而是在您的控制器类中加载。

   FXMLLoader loader = new FXMLLoader(getClass().getResource("mainCallWindow.fxml"));
   Parent mainCallWindowFXML = loader.load();

   //use one of components on your scene to get a reference to your scene object.

   Stage stage = (Stage)tfCallerName.getScene.getWindow();//or use any other component in your controller
   Scene mainCallWindow = new Scene (mainCallWindowFXML, 800, 600);
   stage.setScene(newCallDetails);
   stage.show(); //this line may be unnecessary since you are using the same stage.
}

这不是实现此目的的唯一方法。 您可以使用同一场景加载不同的FXML文件。 我建议更改场景的根节点,而不是完全更改场景。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM