Advertisement
sergAccount

Untitled

Apr 11th, 2021
673
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.37 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.collections.FXCollections;
  7. import javafx.collections.ObservableList;
  8. import javafx.event.ActionEvent;
  9. import javafx.geometry.Insets;
  10. import javafx.scene.Scene;
  11. import javafx.scene.control.Alert;
  12. import javafx.scene.control.Alert.AlertType;
  13. import javafx.scene.control.Button;
  14. import javafx.scene.control.ButtonType;
  15. import javafx.scene.control.Label;
  16. import javafx.scene.control.TableColumn;
  17. import javafx.scene.control.TableView;
  18. import javafx.scene.control.TextField;
  19. import javafx.scene.control.cell.PropertyValueFactory;
  20. import javafx.scene.layout.FlowPane;
  21. import javafx.scene.layout.Pane;
  22. import javafx.scene.layout.VBox;
  23. import javafx.stage.Stage;
  24.  
  25. public class App extends Application {
  26.  
  27.     private static final int W_WIDTH = 1024;
  28.     private static final int W_HEIGHT = 680;
  29.     private static final int W_POS_X = 10;
  30.     private static final int W_POS_Y = 10;
  31.     // ссылка на главное окно !!!
  32.     private Stage pStage;
  33.     // таблица для показа объектов типа Person
  34.     private TableView<Person> tableView;
  35.     //
  36.     private ObservableList<Person> data = FXCollections.observableArrayList();
  37.  
  38.     @Override
  39.     public void start(Stage stage) {
  40.         //
  41.         System.out.println("App.start>>");
  42.         this.pStage = stage;
  43.         // создем сцену        
  44.         var scene = new Scene(createPane(), W_WIDTH, W_HEIGHT);
  45.         stage.setTitle("JavaFX Controls!!!");
  46.         stage.setScene(scene);
  47.         // установка позиции окна (x,y) - левый верхний угол
  48.         stage.setX(W_POS_X);
  49.         stage.setY(W_POS_Y);
  50.         // запретить менять размер!!!
  51.         stage.setResizable(false);
  52.  
  53.         stage.show();
  54.     }
  55.  
  56.     // определяем и настраиваем компонеты
  57.     private Pane createPane() {
  58.         // панель - вертик расположение элементов
  59.         final VBox pane = new VBox();
  60.         pane.setSpacing(10);
  61.         pane.setPadding(new Insets(10, 10, 10, 10));
  62.         // панель - потоковое расположение, вертик и горизонтальные отступы!!!
  63.         final FlowPane buttonsPane = new FlowPane();
  64.         buttonsPane.setVgap(0);
  65.         buttonsPane.setHgap(10);
  66.         // создаем элементы управления !!!
  67.         Button btn1 = new Button("Обновить");
  68.         btn1.setOnAction(this::onRefresh);
  69.         Button btn2 = new Button("Очистить");
  70.         btn2.setOnAction(this::onClear);
  71.         Button btn3 = new Button("FullScreen MODE");
  72.         btn3.setOnAction(this::onFullScreen);
  73.         Button btn4 = new Button("Центрировать");
  74.         btn4.setOnAction(this::onCenter);
  75.         Button btn6 = new Button("Выход");
  76.         btn6.setOnAction(this::onExit);        
  77.         Button btn5 = new Button("Окно");
  78.         btn5.setOnAction(this::onOpenWindow);
  79.        
  80.         TextField textField = new TextField();
  81.         textField.setText("TEST");
  82.         buttonsPane.getChildren().addAll(btn1, btn2, btn3, btn4, btn5, btn6, textField);
  83.        
  84.         // создаем таблицу!!!
  85.         data = createData();
  86.         tableView = createTableView(data);
  87.         // добавляем панель в pane
  88.         pane.getChildren().addAll(buttonsPane, tableView);
  89.         return pane;
  90.     }
  91.     // метод воз-ет для таблицы
  92.     private static ObservableList<Person> createData() {
  93.        //
  94.        final ObservableList<Person> data = FXCollections.observableArrayList();
  95.        data.add(new Person("fName1", "lastName1"));
  96.        data.add(new Person("fName2", "lastName2"));
  97.        data.add(new Person("fName3", "lastName3"));
  98.        return data;
  99.     }
  100.     private static TableView<Person> createTableView(final ObservableList<Person> data) {
  101.        
  102.         TableView<Person> tView = new TableView<>();
  103.         // определяем колонки для таблицы
  104.         TableColumn<Person, String> column1 = new TableColumn<>("Имя");
  105.         column1.setCellValueFactory(new PropertyValueFactory<>("firstName"));        
  106.         // задаем колонку для таблицу
  107.         tView.getColumns().add(column1);        
  108.         // устанавливаем данные для таблицы
  109.         tView.setItems(data);
  110.         return tView;        
  111.     }
  112.     //
  113.     private void onOpenWindow(ActionEvent t) {        
  114.         final Label secondLabel = new Label("I'm a Label on new Window!");
  115.         final VBox pane = new VBox();
  116.         pane.getChildren().add(secondLabel);
  117.         // создаем сцену
  118.         Scene secondScene = new Scene(pane, 230, 100);
  119.         // New window (Stage)
  120.         Stage newWindow = new Stage();
  121.         newWindow.setTitle("Second Stage");
  122.         newWindow.setScene(secondScene);
  123.         // Set position of second window, related to primary window.
  124.         newWindow.setX(pStage.getX() + 200);
  125.         newWindow.setY(pStage.getY() + 100);
  126.         newWindow.show();
  127.        
  128.                
  129.     }
  130.    
  131.      //
  132.     private void onExit(ActionEvent t) {
  133.         //
  134.         System.out.println("onExit>>");
  135.         final Alert alert = new Alert(AlertType.CONFIRMATION);
  136.         alert.setTitle("Выход");
  137.         alert.setHeaderText("Завершить работу?");
  138.         alert.setContentText(":)");
  139.         // показывает диалог на экране и ждет действий пол-ля
  140.         Optional<ButtonType> option = alert.showAndWait();
  141.         if(option.get() == ButtonType.OK){
  142.             // завершаем работу JavaFX APP
  143.             Platform.exit();
  144.         }        
  145.     }
  146.  
  147.     //
  148.     private void onRefresh(ActionEvent t) {
  149.         System.out.println("onRefresh!!!");
  150.     }
  151.  
  152.     private void onClear(ActionEvent t) {
  153.     }
  154.  
  155.     private void onFullScreen(ActionEvent t) {
  156.         pStage.setMaximized(true);
  157.         pStage.setFullScreen(true);
  158.     }
  159.  
  160.     private void onCenter(ActionEvent t) {
  161.         // centerOnScreen
  162.         pStage.centerOnScreen();
  163.     }
  164.  
  165.  
  166.     public static void main(String[] args) {
  167.         launch();
  168.     }
  169. }
  170.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement