[Note] Scene transition with JavaFX

Let's make a scene transition with JavaFX

It's the 22nd day of CSG Advent Calendar. It's the third time, now that the end is visible, let's write it again this time

Introduction

For those who don't like JavaFX. .. .. JavaFX is the standard GUI instead of SWING from JAVA8. Please refer to https://qiita.com/Kei_22/items/f54e6ba7cd4bf71b14c8 because other people have written the details such as introduction in CSG Advent Calendar.

Then the main story

・ First of all, import various things

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;

・ Create a scene


public class Main extends Application{
  //Declaration of the scene
  public static Scene scene1 = null;
  public static Scene scene2 = null;

scene1 is the first scene and scene2 is the scene after the change.

・ In start

@Override
  public void start(Stage stage)throws Exception {

  stage.setTitle("HelloJavaFX");
  stage.setHeight(200);
  stage.setWidth(150);
  //Creating a stage
  initScene1(stage);
  initScene2(stage);

  stage.setScene(scene1);
  stage.show();
  }

Create a GUI for the scene with initScene1 and initScene2. And first, set Scene1. Please like the title screen here

・ Inside initScene1

  public static void initScene1(Stage stage) {
    AnchorPane root = new AnchorPane();
    scene1 = new Scene(root);

    Button btn = new Button("Scene change");
    btn.setPrefWidth(100);
    btn.setPrefHeight(50);
    btn.setOnMouseClicked(event -> setScene(stage,scene2));


    root.getChildren().add(btn);
  }

For the time being, this time I just placed the button because the scene changes when I press the button. Call the setScene method with setOnMouseClicked. The setScene method will be defined later.

・ Inside initScene2

  public static void initScene2(Stage stage) {
    AnchorPane root = new AnchorPane();
    scene2 = new Scene(root);

    Label lbl = new Label("Scene transition completed!");

    root.getChildren().add(lbl);
  }

We have prepared a Label to show that the scene has changed.

・ In setScene

public static void setScene(Stage stage,Scene changeScene) {
    stage.setScene(changeScene);
    stage.show();
  }
}

Set the scene sent as an argument and complete! Don't forget the closing parentheses

What it feels like

It's not bad because it's appropriate to introduce the part. .. .. This is the first screen ↓ スクリーンショット 2018-08-27 21.56.32.png

And this is the screen after pressing the button ↓ スクリーンショット 2018-08-27 21.56.40.png

This is the scene transition. .. .. Well ah ah! !! !! !! !! !! !! !! !! !! !! !! !! !! !!

Overall view

I will show you the script that was divided into small pieces at once

Main.java


import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.application.Application;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;

public class Main extends Application{
  //Declaration of the scene
  public static Scene scene1 = null;
  public static Scene scene2 = null;

  @Override
  public void start(Stage stage)throws Exception {

  stage.setTitle("HelloJavaFX");
  stage.setHeight(200);
  stage.setWidth(150);
  //Creating a stage
  initScene1(stage);
  initScene2(stage);

  stage.setScene(scene1);
  stage.show();
  }

  public static void initScene1(Stage stage) {
    AnchorPane root = new AnchorPane();
    scene1 = new Scene(root);

    Button btn = new Button("Scene change");
    btn.setPrefWidth(100);
    btn.setPrefHeight(50);
    btn.setOnMouseClicked(event -> setScene(stage,scene2));


    root.getChildren().add(btn);
  }
  public static void initScene2(Stage stage) {
    AnchorPane root = new AnchorPane();
    scene2 = new Scene(root);

    Label lbl = new Label("Successful scene change!");

    root.getChildren().add(lbl);
  }
  public static  void setScene(Stage stage,Scene changeScene) {
    stage.setScene(changeScene);
    stage.show();
  }
}

At the end

*** Because there is little information on JavaFX! !! !! Everyone! !! !! Let's write! !! !! !! !! !! !! !! !! !! let's do it! !! !! !! !! !! !! !! !! *** *** This is a memorandum of scene transition.

Recommended Posts

[Note] Scene transition with JavaFX
HelloFX with JavaFX
JavaFX8 screen transition
Merry Christmas with JavaFX !!
[Note] Methods ending with?
Sample to create GUI application with JavaFX + IntelliJ + Scene Builder
Study Java with Progate Note 1
Screen transition with swing, java
Output characters like conversation with JavaFX
Make a slideshow tool with JavaFX
[Java] Points to note with Arrays.asList ()
Hello world with Kotlin and JavaFX
Drag and drop files with JavaFX