首页 文章

我们可以在5秒后不使用按钮更改Java Scene Builder 2.0中的场景

提问于
浏览
0

基本上我使用JavaFX场景buidler 2.0我想将场景从一个更改为另一个而不使用任何按钮 . Main File公共类OurFirstProject扩展Application {

public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
    stage.setFullScreenExitHint("");
    //stage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
    Rectangle2D screenBounds = Screen.getPrimary().getVisualBounds();
    Scene scene = new Scene(root, screenBounds.getWidth(), screenBounds.getHeight());
    stage.setScene(scene);
    stage.setFullScreen(true);
    stage.show();

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

}

公共类FXMLDocumentController实现Initializable {

@FXML
private void change(ActionEvent event) throws IOException {
    Parent sceneChange = FXMLLoader.load(getClass().getResource("Change.fxml"));
    Rectangle2D screenBounds = Screen.getPrimary().getVisualBounds();
    Scene changeScene = new Scene(sceneChange, screenBounds.getWidth(), screenBounds.getHeight());
    Stage Window = (Stage) ((Node) event.getSource()).getScene().getWindow();
    Window.setScene(changeScene);
    Window.setFullScreen(true);
    Window.show();
}

int a = 0;
@FXML
public Button helloButton;
@FXML
private Label ourLabel;

@FXML
private void printHello(ActionEvent e) {
    a++;
    if (a % 2 == 0) {
        ourLabel.setText("Hello World! Kyun" + a);
    } else {
        ourLabel.setText("Hello Dunia" + a);
    }
}

@Override
public void initialize(URL url, ResourceBundle rb) {

在此处输入代码 }

在FXML文件名中提到的场景“更改”,我想在没有使用按钮的情况下运行此Scen我想在FIrst场景的五秒延迟时运行它 .

1 回答

  • 0

    你可以使用Timer()之类的,

    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
            @Override
            public void run() {
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {// After this you can add your change.fxml load code
                        Parent root = null;
                        try {
                            root = fxmlLoader.load();
                        }catch(Exception e)
                        {
                            //Exception catch code here
                        }
                        primaryStage.show();//Here you can write your show code like window.show()
                    }
                });
            }
        },5000);// 5000- time delay in milliseconds
    

相关问题