JavaFX HBox set alignment

Description

JavaFX HBox set alignment

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class Main extends Application {
   protected Text text = new Text(50, 50, "JavaFX Programming");

   protected BorderPane getPane() {
      HBox paneForButtons = new HBox(20);
      Button btLeft = new Button("Left", new ImageView("icon.png"));
      Button btRight = new Button("Right", new ImageView("icon.png"));
      paneForButtons.getChildren().addAll(btLeft, btRight);
      paneForButtons.setAlignment(Pos.CENTER);
      paneForButtons.setStyle("-fx-border-color: green");

      BorderPane pane = new BorderPane();
      pane.setBottom(paneForButtons);//  w  w w  .  j av a  2s.  co  m

      Pane paneForText = new Pane();
      paneForText.getChildren().add(text);
      pane.setCenter(paneForText);

      btLeft.setOnAction(e -> text.setX(text.getX() - 10));
      btRight.setOnAction(e -> text.setX(text.getX() + 10));

      return pane;
   }

   @Override
   public void start(Stage primaryStage) {

      Scene scene = new Scene(getPane(), 450, 200);
      primaryStage.setTitle("ButtonDemo");
      primaryStage.setScene(scene);
      primaryStage.show();
   }

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



PreviousNext

Related