JavaFX Layout Management Program

 

Program to demonstrate JavaFX Layout Management


// JavaFX Controls

// Layout management

 

import javafx.application.*;

import javafx.scene.*;

import javafx.stage.*;

import javafx.event.*;

import javafx.scene.layout.*;

import javafx.scene.control.*;

import javafx.scene.input.*;

import javafx.geometry.*;

 

public class Layout_Demo extends Application

{

     public static void main(String []as)

     {

          launch(as);       

     }

    

     public void init(){}

     public void stop(){}

    

     public void start(Stage myStage)

     {

          myStage.setTitle("Layout Demo");

         

          Button B1 = new Button("Top");

          Button B2 = new Button("Right");

          Button B3 = new Button("Bottom");

          Button B4 = new Button("Left");

          Button B5 = new Button("Center");

         

          // Flow Layout Horizontal Orientation

          FlowPane layout = new FlowPane(Orientation.HORIZONTAL, 25, 35);

          layout.setAlignment(Pos.CENTER);

          layout.getChildren().addAll(B1,B2,B3,B4,B5);

          layout.setPadding(new Insets(10,10,10,10));

         

          Scene myScene = new Scene(layout, 250, 250);

          myStage.setScene(myScene);

          myStage.show();

     }

}

 

// Flow Layout Vertical Orientation                

          FlowPane layout = new FlowPane(Orientation.VERTICAL, 25, 35);

          layout.setAlignment(Pos.CENTER);

          layout.getChildren().addAll(B1,B2,B3,B4,B5);

          layout.setPadding(new Insets(10,10,10,10));

 

// HBox Layout               

          HBox layout = new HBox(25);

          layout.setAlignment(Pos.CENTER);

          layout.getChildren().addAll(B1,B2,B3,B4,B5);

            layout.setPadding(new Insets(10,10,10,10));

 

// VBox Layout

          VBox layout = new VBox(15);

          layout.setAlignment(Pos.CENTER);

          layout.getChildren().addAll(B1,B2,B3,B4,B5);

          layout.setPadding(new Insets(10,10,10,10));

 

 


 

// stack layout   

          B1.setPrefSize(200,200);

          B2.setPrefSize(175,175);

          B3.setPrefSize(150,150);

          B4.setPrefSize(125,125);

          B5.setPrefSize(100,100);

                  

          StackPane layout = new StackPane();

          layout.setAlignment(Pos.CENTER);

          layout.getChildren().addAll(B1,B2,B3,B4,B5);

          layout.setPadding(new Insets(10,10,10,10));

 

// Border Layout

          BorderPane layout = new BorderPane();

                  

          // Adding controls to each location

          layout.setTop(B1);

          layout.setRight(B2);

          layout.setBottom(B3);

          layout.setLeft(B4);

          layout.setCenter(B5);

         

          // Set alignment in each location

          BorderPane.setAlignment(B1,Pos.TOP_CENTER);

          BorderPane.setAlignment(B2,Pos.CENTER_RIGHT);

          BorderPane.setAlignment(B3,Pos.BOTTOM_CENTER);

          BorderPane.setAlignment(B4,Pos.CENTER_LEFT);

          BorderPane.setAlignment(B5,Pos.CENTER);

 

 

 

         


 

// Grid layout

          GridPane layout = new GridPane();

          layout.setAlignment(Pos.CENTER);

          layout.setHgap(50);

          layout.setVgap(50);

          layout.setPadding(new Insets(25,25,25,25));

                  

          // Adding controls to each location (col x row)

          layout.add(B1,0,0);

         layout.add(B2,0,1);

          layout.add(B3,1,0);

          layout.add(B4,1,1);

          // Column span (2,1)

          layout.add(B5,0,2,2,1);

          GridPane.setHalignment(B5, HPos.CENTER);

 

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu