How to use gridpane JavaFX to make buttons span multiple columns / rows?

I am new to JavaFX and am trying to use gridpane for simple button design

I can't figure out how to make buttons span multiple columns / rows without pushing other buttons away I've been using it H@R_613_2419 @And V@R_613_2419 @Group other buttons together I tried setrowspan on the button, but it didn't seem to work

How it is looking

How i want it to look

This is my code:

import javafx.stage.*;
import javafx.application.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;

public class SimpleGUI extends Application {
 public void start(Stage primaryStage) throws Exception {
  GridPane root = new GridPane();
  Scene scene = new Scene(root,200,200);

   //Buttons
   Button b1 = new Button("KNAPP 1");
   Button b2 = new Button("KNAPP 2");
   Button b3 = new Button("KNAPP 3");
   Button b4 = new Button("KNAPP 4");
   Button b5 = new Button("KNAPP 5");
   Button b6 = new Button("KNAPP 6");

   //Horizontal @R_613_2419@
   H@R_613_2419@ topButtons = new H@R_613_2419@();
   topButtons.getChildren().add(b1);
   topButtons.getChildren().add(b2);
   topButtons.getChildren().add(b3);

   //Vertical @R_613_2419@
   V@R_613_2419@ leftButtons = new V@R_613_2419@();
   leftButtons.getChildren().add(b4);
   leftButtons.getChildren().add(b5);

   //Placement
   GridPane.setConstraints(topButtons,0);
   GridPane.setConstraints(leftButtons,1);
   GridPane.setConstraints(b6,1,1);

   //Length (3 Columns,2 Rows)
   GridPane.setColumnSpan(topButtons,3);
   GridPane.setRowSpan(leftButtons,2);

   //Add them to the stage
   root.getChildren().add(topButtons);
   root.getChildren().add(leftButtons);
   root.getChildren().add(b6);

   primaryStage.setScene(scene);
   primaryStage.show();
 }

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

Solution

Because you are using a grid pane, you do not need to H@R_613_2419 @And V@R_613_2419 @:

import javafx.stage.*;
import javafx.application.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;

public class SimpleGUI extends Application {
 public void start(Stage primaryStage) throws Exception {
  GridPane root = new GridPane();
  Scene scene = new Scene(root);

   //Buttons
   Button b1 = new Button("KNAPP 1");
   Button b2 = new Button("KNAPP 2");
   Button b3 = new Button("KNAPP 3");
   Button b4 = new Button("KNAPP 4");
   Button b5 = new Button("KNAPP 5");
   Button b6 = new Button("KNAPP 6");

   root.add(b1,0);
   root.add(b2,0);
   root.add(b3,2,0);

   root.add(b4,1);
   root.add(b5,2);

   // node,columnIndex,rowIndex,columnSpan,rowSpan:
   root.add(b6,2);

   // allow button to grow:
   b6.setMaxSize(Double.MAX_VALUE,Double.MAX_VALUE);

   primaryStage.setScene(scene);
   primaryStage.show();
 }

 public static void main(String[] args){
  launch(args);
 }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>