Инструменты пользователя

Инструменты сайта


develop:java:javafx

Различия

Показаны различия между двумя версиями страницы.

Ссылка на это сравнение

Предыдущая версия справа и слева Предыдущая версия
Следующая версия
Предыдущая версия
develop:java:javafx [2023/05/21 08:32]
admin [Сведения]
develop:java:javafx [2023/06/20 15:34] (текущий)
admin
Строка 4: Строка 4:
  
 ===== Сведения ===== ===== Сведения =====
- 
 <details> <details>
 <summary>:!: Сведения </summary> <summary>:!: Сведения </summary>
Строка 14: Строка 13:
  
  
 +<code java>
 +</code>
 +</details>
  
  
  
 +===== Использование =====
 +Есть как минимум пару вариантов использования, которые несколько разнятся\\
 +<details>
 +<summary>:!: Интерфейс в fxml файлах</summary>
 +
 +Простая форма fxml\\
 +(myApp/src/main/resources/maket/rootWindow.fxml)
 +<code xml>
 +<?xml version="1.0" encoding="UTF-8"?>
 +
 +<?import javafx.geometry.*?>
 +<?import javafx.scene.control.*?>
 +<?import javafx.scene.layout.*?>
 +
 +<VBox alignment="CENTER" prefHeight="300.0" prefWidth="500.0" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.sakeeper.sakeeper.controller.BaseController">
 +    <padding>
 +        <Insets bottom="10" top="5" />
 +    </padding>
 +    <Label alignment="TOP_CENTER" text="Название торговой точки" />
 +    <HBox alignment="BOTTOM_CENTER" spacing="5">
 +        <padding>
 +            <Insets bottom="5" top="5" />
 +        </padding>
 +        <Button onAction="#product1" text="Product 1" />
 +        <Button onAction="#product2" text="Product 2" />
 +    </HBox>
 +   <HBox id="newHBox" prefHeight="100.0" prefWidth="200.0" />
 +</VBox>
 +</code>
 +
 +Контроллер\\
 +(myApp/src/main/java/com/myApp/myApp/controller/BaseController.java)\\
 <code java> <code java>
 +package com.sakeeper.sakeeper.controller;
  
 +import com.sakeeper.sakeeper.Sakeeper;
 +import com.sakeeper.sakeeper.model.Product;
 +import javafx.fxml.FXML;
 +import javafx.scene.control.Button;
 +
 +public class BaseController {
 +    private Sakeeper sakeeper;
 +    @FXML
 +    private Button product1;
 +    @FXML
 +    private Button product2;
 +    public void setSakeeper(Sakeeper sakeeper){
 +        this.sakeeper = sakeeper;
 +    }
 +
 +    @FXML
 +    private void product1(){
 +        sakeeper.showVolumeWindow(new Product("product_1"));
 +    }
 +    @FXML
 +    private void product2(){
 +        sakeeper.showVolumeWindow(new Product("product_2"));
 +    }
 +}
 +</code>
 +
 +Основной код, главный файл
 +<code java>
 +package com.sakeeper.sakeeper;
 +
 +import com.sakeeper.sakeeper.controller.BaseController;
 +import com.sakeeper.sakeeper.controller.VolumeController;
 +import com.sakeeper.sakeeper.model.Product;
 +import javafx.application.Application;
 +import javafx.event.ActionEvent;
 +import javafx.fxml.FXMLLoader;
 +import javafx.scene.Scene;
 +import javafx.scene.control.Alert;
 +import javafx.scene.control.Button;
 +import javafx.scene.layout.VBox;
 +import javafx.stage.Modality;
 +import javafx.stage.Stage;
 +
 +import javafx.event.EventHandler;
 +
 +import java.io.IOException;
 +
 +public class Sakeeper extends Application {
 +    private Stage primaryStage;
 +    private VBox rootLayout;
 +
 +    public Sakeeper(){
 +        // list add's
 +    }
 +
 +    public static void main(String[] args) {
 +        launch();
 +    }
 +
 +    @Override
 +    public void start(Stage primaryStage) throws IOException {
 +        this.primaryStage = primaryStage;
 +        this.primaryStage.setTitle("saKeeper");
 +        showBaseWindow();
 +    }
 +
 +    // Это основное окно
 +    public void showBaseWindow(){
 +        try{
 +            FXMLLoader loader = new FXMLLoader();
 +            loader.setLocation(Sakeeper.class.getResource("/maket/rootWindow.fxml"));
 +            rootLayout = loader.load();
 +
 +            Scene scene = new Scene(rootLayout);
 +            primaryStage.setScene(scene);
 +
 +            BaseController controller = loader.getController();
 +            controller.setSakeeper(this);
 +            primaryStage.show();
 +
 +        } catch (IOException e) {
 +            e.printStackTrace();
 +        }
 +    }
 +
 +    // Это уже второе окно, в примере его нет
 +    public void showVolumeWindow(Product selectedProduct){
 +        try{
 +            FXMLLoader loader = new FXMLLoader();
 +            loader.setLocation(Sakeeper.class.getResource("/maket/volumeWindow.fxml"));
 +            VBox page = loader.load();
 +
 +            Stage dialogStage = new Stage();
 +            dialogStage.setTitle("Выберите объем");
 +            dialogStage.initModality(Modality.WINDOW_MODAL);
 +            dialogStage.initOwner(primaryStage);
 +            dialogStage.setScene(new Scene(page));
 +
 +            VolumeController volumeController = loader.getController();
 +            volumeController.setDialogStage(dialogStage);
 +            volumeController.setProduct(selectedProduct);
 +            dialogStage.showAndWait();
 +
 +        } catch (IOException e) {
 +            e.printStackTrace();
 +        }
 +    }
 +}
 </code> </code>
 </details> </details>
Строка 24: Строка 167:
  
  
-=====  =====+<details> 
 +<summary>:!: Интерфейс в коде</summary> 
 +Элементы так же могут создаваться в коде, размещаться на форме и задаваться свойства\\ 
 +<code java> 
 +    public void showBaseWindow(){ 
 +        try{ 
 +            (..)  
 +            Button newButt new Button("This dynamic button"); 
 +            /*newButt.setOnAction(event -> { 
 +                Alert message new Alert(Alert.AlertType.INFORMATION); 
 +                message.setContentText("this message from dynamic button "); 
 +                message.show(); 
 +            });*/ 
 + 
 + 
 +            /*EventHandler<MouseEvent> rightClickHandler event -> { 
 +                if (MouseButton.SECONDARY.equals(event.getButton())) { 
 +                    button.setFont(new Font(button.getFont().getSize() + 1)); 
 +                } 
 +            };*/ 
 + 
 +            //Create the EventHandler 
 +            EventHandler<ActionEvent> hnd new EventHandler<ActionEvent>() { 
 +                @Override 
 +                //handle method 
 +                public void handle(ActionEvent ev) { 
 +                    Alert message new Alert(Alert.AlertType.INFORMATION); 
 +                    message.setContentText("this message from dynamic button "); 
 +                    message.show(); 
 +                } 
 +            }; 
 +            newButt.setOnAction(hnd); 
 +            rootLayout.getChildren().add(newButt); 
 +        (...) 
 +        } catch (IOException e) { 
 +            e.printStackTrace(); 
 +        } 
 +    } 
 +</code> 
 +</details> 
 + 
 + 
 + 
 +===== Оформление ===== 
 +Хорошее разделение должно придерживаться пути:\\ 
 +  * логика в java (модели/контроллеры) 
 +  * элементы формы в FXML описании 
 +  * а оформление в CSS 
 + 
 +В JavaFX не совсем тот самый общеизвестный CSS, имеет собственные свойства\\ 
 +Выбор компонентов либо селектором (название типа компонента, с точкой вначале, либо ИД с решеткой вначале) аналогично CSS\\
  
 <details> <details>
-<summary> </summary>+<summary>:!: Примеры </summary> 
 +<code java> 
 +.button { 
 +    -fx-font-size: 15px; 
 +
 + 
 +.label { 
 +    // Some properties 
 +
 + 
 +#my-component { 
 +  ... 
 +
 +</code> 
  
 <code java> <code java>
 +<Label styleClass="my-label,other-class">I am a simple label</Label>
 +   # или в java
 +Label label = new Label("I am a simple label");
 +label.getStyleClass().addAll("my-label", "other-class");
  
 +
 +<Label fx:id="foo">I am a simple label</Label>
 +  #
 +Label label = new Label("I am a simple label");
 +label.setId("foo");
 +</code>
 +
 +Подключение
 +<code java>
 +<BorderPane xmlns="http://javafx.com/javafx"
 +            xmlns:fx="http://javafx.com/fxml"
 +            stylesheets="styles.css"
 +            ...
 +            >
 +  ...
 +</BorderPane>
 +
 +  #
 +String stylesheet = getClass().getResource("/styles.css").toExternalForm();
 +scene.getStylesheets().add(stylesheet);
 +
 +  #
 +<HBox stylesheets="styles.css">
 +    ...
 +</HBox>
 +
 +  #
 +HBox box = new HBox();
 +String stylesheet = getClass().getResource("/styles.css").toExternalForm();
 +box.getStylesheets().add(stylesheet);
 </code> </code>
 </details> </details>
 +
 +
  
develop/java/javafx.1684657925.txt.gz · Последнее изменение: 2023/05/21 08:32 — admin