Advertisement
sergAccount

Untitled

Apr 11th, 2021
677
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.85 KB | None | 0 0
  1. package com.mycompany.mavenproject3;
  2.  
  3. import java.util.Optional;
  4. import javafx.application.Application;
  5. import javafx.application.Platform;
  6. import javafx.event.ActionEvent;
  7. import javafx.geometry.Insets;
  8. import javafx.scene.Scene;
  9. import javafx.scene.control.Alert;
  10. import javafx.scene.control.Alert.AlertType;
  11. import javafx.scene.control.Button;
  12. import javafx.scene.control.ButtonType;
  13. import javafx.scene.layout.FlowPane;
  14. import javafx.scene.layout.Pane;
  15. import javafx.scene.layout.VBox;
  16. import javafx.stage.Stage;
  17.  
  18. public class App extends Application {
  19.  
  20.     private static final int W_WIDTH = 1024;
  21.     private static final int W_HEIGHT = 680;
  22.     private static final int W_POS_X = 10;
  23.     private static final int W_POS_Y = 10;
  24.     // ссылка на главное окно !!!
  25.     private Stage pStage;
  26.  
  27.     @Override
  28.     public void start(Stage stage) {
  29.         //
  30.         System.out.println("App.start>>");
  31.         this.pStage = stage;
  32.         // создем сцену        
  33.         var scene = new Scene(createPane(), W_WIDTH, W_HEIGHT);
  34.         stage.setTitle("JavaFX Controls!!!");
  35.         stage.setScene(scene);
  36.         // установка позиции окна (x,y) - левый верхний угол
  37.         stage.setX(W_POS_X);
  38.         stage.setY(W_POS_Y);
  39.         // запретить менять размер!!!
  40.         stage.setResizable(false);
  41.  
  42.         stage.show();
  43.     }
  44.  
  45.     // определяем и настраиваем компонеты
  46.     private Pane createPane() {
  47.         // панель - вертик расположение элементов
  48.         final VBox pane = new VBox();
  49.         pane.setSpacing(10);
  50.         pane.setPadding(new Insets(10, 10, 10, 10));
  51.         // панель - потоковое расположение, вертик и горизонтальные отступы!!!
  52.         final FlowPane buttonsPane = new FlowPane();
  53.         buttonsPane.setVgap(0);
  54.         buttonsPane.setHgap(10);
  55.         // создаем элементы управления !!!
  56.         Button btn1 = new Button("Обновить");
  57.         btn1.setOnAction(this::onRefresh);
  58.         Button btn2 = new Button("Очистить");
  59.         btn2.setOnAction(this::onClear);
  60.         Button btn3 = new Button("FullScreen MODE");
  61.         btn3.setOnAction(this::onFullScreen);
  62.         Button btn4 = new Button("Центрировать");
  63.         btn4.setOnAction(this::onCenter);
  64.         Button btn6 = new Button("Выход");
  65.         btn6.setOnAction(this::onExit);
  66.  
  67.         buttonsPane.getChildren().addAll(btn1, btn2, btn3, btn4, btn6/*, btn5, btn6*/);
  68.         // добавляем панель в pane
  69.         pane.getChildren().addAll(buttonsPane);
  70.         return pane;
  71.     }
  72.      //
  73.     private void onExit(ActionEvent t) {
  74.         //
  75.         System.out.println("onExit>>");
  76.         final Alert alert = new Alert(AlertType.CONFIRMATION);
  77.         alert.setTitle("Выход");
  78.         alert.setHeaderText("Завершить работу?");
  79.         alert.setContentText(":)");
  80.         // показывает диалог на экране и ждет действий пол-ля
  81.         Optional<ButtonType> option = alert.showAndWait();
  82.         if(option.get() == ButtonType.OK){
  83.             // завершаем работу JavaFX APP
  84.             Platform.exit();
  85.         }        
  86.     }
  87.  
  88.     //
  89.     private void onRefresh(ActionEvent t) {
  90.         System.out.println("onRefresh!!!");
  91.     }
  92.  
  93.     private void onClear(ActionEvent t) {
  94.     }
  95.  
  96.     private void onFullScreen(ActionEvent t) {
  97.         pStage.setMaximized(true);
  98.         pStage.setFullScreen(true);
  99.     }
  100.  
  101.     private void onCenter(ActionEvent t) {
  102.         // centerOnScreen
  103.         pStage.centerOnScreen();
  104.     }
  105.  
  106.  
  107.     public static void main(String[] args) {
  108.         launch();
  109.     }
  110.  
  111.  
  112. }
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement