JavaFX StackPane extends to create custom control

Description

JavaFX StackPane extends to create custom control


import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Main extends Application {
  @Override/*w ww.  jav  a2s. co  m*/
  public void start(Stage primaryStage) {
    // Create a border pane 
    BorderPane pane = new BorderPane();

    // Place nodes in the pane
    pane.setTop(new CustomPane("Top")); 
    pane.setRight(new CustomPane("Right"));
    pane.setBottom(new CustomPane("Bottom"));
    pane.setLeft(new CustomPane("Left"));
    pane.setCenter(new CustomPane("Center")); 
    

    Scene scene = new Scene(pane);
    primaryStage.setTitle("Main");
    primaryStage.setScene(scene);
    primaryStage.show();
  }
} 

// Define a custom pane to hold a label in the center of the pane
class CustomPane extends StackPane {
  public CustomPane(String title) {
    getChildren().add(new Label(title));
    setStyle("-fx-border-color: red");
    setPadding(new Insets(11.5, 12.5, 13.5, 14.5));
  }
}


import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class Main extends Application {
  @Override//from  w ww  .j  a  va 2 s .  c o m
  public void start(Stage primaryStage) {
    FanGrid pane = new FanGrid(2, 75);
    pane.setAlignment(Pos.CENTER);
    pane.setPadding(new Insets(5));
    pane.setVgap(5);
    pane.setHgap(5);

    Scene scene = new Scene(pane);
    primaryStage.setTitle("java2s.com");
    primaryStage.setScene(scene);
    primaryStage.setResizable(false);
    primaryStage.show();
  }



  public static void main(String[] args) {
    Application.launch(args);
  }
}
  class FanGrid extends GridPane {
    FanGrid(int n, double radius) {
      for (int col = 0; col < n; col++) {
        for (int row = 0; row < n; row++) {
          FanPane fanPane = new FanPane(radius);
          add(fanPane, col, row);
        }
      }
    }
  }
class FanPane extends StackPane {
  FanPane(double radius) {
    Circle circle = new Circle(radius);
    circle.setStroke(Color.BLACK);
    circle.setFill(Color.WHITE);
    getChildren().add(circle);

    Pane pane = new Pane();
    for (int i = 0, j = 25; i < 4; i++, j += 90) {
      Arc arc = new Arc(radius, radius, radius * 0.9, radius * 0.9, j, 40);
      arc.setFill(Color.BLACK);
      arc.setType(ArcType.ROUND);
      pane.getChildren().add(arc);
    }
    getChildren().add(pane);
  }
}



PreviousNext

Related