简体   繁体   中英

JavaFX, Label only showing after resizing of the window

I new to working with JavaFX and the problem is, that after a change the scene, not all components are diplayed. For example I have a GridPane which will be added to a BorderPane center and a Label will be added to the bottom of the BorderPane. But after I change the scene this Label is not showing, only after a resize it works.

Following is a simple Minimal, Complete, and Verifiable example:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.scene.control.Label;
import javafx.scene.control.Button;
import javafx.scene.Scene;

public class View extends Application implements EventHandler<ActionEvent> {
    private Scene scene1, scene2;
    private Button b1, b2;
    private Stage stage;

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

    @Override
    public void start(Stage stage) throws Exception {
        this.stage = stage;

        this.createScene1();
        this.stage.setScene(this.scene1);

        this.stage.setWidth(200);
        this.stage.setHeight(200);
        this.stage.show();
    }

    public void createScene1() {
        BorderPane border = new BorderPane();
        GridPane root = new GridPane();

        root.add(new Label("Scene 1"), 0, 0);
        this.b1 = new Button("Change to Scene 2");
        this.b1.setOnAction(this);
        root.add(this.b1, 0, 1);

        border.setCenter(root);
        border.setBottom(new Label("Should be visible"));
        this.scene1 = new Scene(border);
    }

    public void createScene2() {
        BorderPane border = new BorderPane();
        GridPane root = new GridPane();

        root.add(new Label("Scene 2"), 0, 0);
        this.b2 = new Button("Change to Scene 1");
        this.b2.setOnAction(this);
        root.add(this.b2, 0, 1);

        border.setCenter(root);
        border.setBottom(new Label("Should be visible"));
        this.scene2 = new Scene(border);
    }

    @Override
    public void handle(ActionEvent e) {
        if (this.b1 != null) {
            if (this.b1 == e.getSource()) {
                this.createScene2();
                this.stage.setScene(this.scene2);
            }
        }
        if (this.b2 != null) {
            if (this.b2 == e.getSource()) {
                this.createScene1();
                this.stage.setScene(this.scene1);
            }
        }
    }
}

I can not reproduce your problem, since your code is working fine for me. But your components might get scaled down so much, that they are not rendered. To avoid this, try using a prefixed min width and height.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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