Today I started learning JavaFx but I've run into a problem. For some reason when I use a GridPane to put a button and label on the screen, if I resize the window the button moves on the screen. The button and the label is staying in one place, and not moving alongside the window. How do I make it so that the button and label stay in their spot when resizing the window? I've read multiple pages but still can't find anything. Heres my code so far:
package application.alertboxes;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.*;
import javafx.stage.Modality;
import javafx.stage.Stage;
public class infoBox {
public static void display() {
Stage window = new Stage();
window.initModality(Modality.APPLICATION_MODAL);
window.setTitle("Information");
Label label = new Label("Test");
Button closeButton = new Button("Close");
closeButton.setOnAction(e -> window.close());
GridPane layout = new GridPane();
layout.setPadding(new Insets(10,10,10,10));
layout.setVgap(8);
layout.setHgap(10);
GridPane.setConstraints(closeButton, 105, 80);
GridPane.setConstraints(label, 75, 50);
layout.getChildren().addAll(closeButton, label);
Scene scene = new Scene(layout, 1200, 720);
window.setScene(scene);
window.showAndWait();
}
}
Add the appropriate constrains to all GripPane
cells:
import javafx.application.Application;
import javafx.geometry.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
public class FxMain extends Application{
private static final int ROWS = 10, COLS = 10;
@Override
public void start(Stage window) {
Label label = new Label("Test");
Button closeButton = new Button("Close");
closeButton.setOnAction(e -> window.close());
GridPane layout = new GridPane();
layout.setPadding(new Insets(10,10,10,10));
layout.setVgap(8);
layout.setHgap(10);
GridPane.setConstraints(closeButton, 8, 8);
GridPane.setConstraints(label, 4, 4);
//set vgrow to all rows to always
for (int row = 0; row < ROWS; row++) {
layout.getRowConstraints()
.add(new RowConstraints(-1, -1, -1, Priority.ALWAYS, VPos.CENTER, false));
}
//set hgrow to all columns to always
for (int col = 0; col <COLS; col++) {
layout.getColumnConstraints()
.add(new ColumnConstraints(-1, -1, -1, Priority.ALWAYS, HPos.CENTER, false));
}
layout.getChildren().addAll(closeButton, label);
Scene scene = new Scene(layout, 320, 200);
window.setScene(scene);
window.show();
}
public static void main(String[] args) {
launch(args);
}
}
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.