【Java FX】简单的UML生成器

该文章展示了如何使用JavaFX库创建一个简单的应用程序,通过UML类结构来组织Text组件的属性(如文本、坐标等)和方法(如创建、设置属性)。
摘要由CSDN通过智能技术生成

实现效果

在这里插入图片描述

代码

package com.boulete.fxdemo;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;

import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;

public class UMLApplication extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        // 使用实例:
        // 创建UML实例类
        UMLClass umlClassText = new UMLClass("javafx.scene.text.Text");
        // 添加属性
        umlClassText.addProperties(
                new Property("text", "StringProperty", "定义显示的文本"),
                new Property("x", "DoubleProperty", "定义文本的x坐标(默认:0)"),
                new Property("y", "DoubleProperty", "定义文本的y坐标(默认:0)"),
                new Property("underline", "BooleanProperty", "定义是否每行文本下面有下划线(默认:false)"),
                new Property("strikethrough", "BooleanProperty", "定义是否每行文本中间有删除线(默认:false)"),
                new Property("font", "ObjectProperty<Font>", "定义文本的字体")
        );
        // 添加方法
        umlClassText.addMethods(
                new Method("Text", "创建一个空的Text"),
                new Method("Text", "使用给定的文本创建一个Text",
                        new Property("text", "String")),
                new Method("Text", "使用给定的x、y坐标以及文本创建一个Text",
                        new Property("x", "Double"),
                        new Property("y", "Double"),
                        new Property("text", "String"))
        );

        // 创建场景并展示
        Scene scene = new Scene(getPaneByUMLClass(umlClassText));  // 创建场景
        primaryStage.setTitle("UML");  // 标题
        primaryStage.setScene(scene);  // 场景设置
        primaryStage.show();  // 显示窗体
    }

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

    public GridPane getPaneByUMLClass(UMLClass umlClass){
        final double paddingOfWholePane = 50;  // 整个面板的内边距
        final double paddingOfInnerPane = 15;  // UML边框的内边距
        final double paddingOfTitle = 5;  // 类名的上下内边距
        final double hGap = 2;  // 水平间距
        final double vGap = 5;  // 行间距
        final Font fontTitle = Font.font("Times New Roman", FontWeight.BOLD, FontPosture.REGULAR, 14); // 类名字体

        GridPane pane = new GridPane();  // 背景面板
        StackPane paneClassName = new StackPane();  // 类名面板
        VBox vBoxProperties = new VBox(vGap);  // 属性面板
        VBox vBoxPropertiesDesc = new VBox(vGap);  // 属性描述面板
        VBox vBoxMethods = new VBox(vGap);  // 方法面板
        VBox vBoxMethodsDesc = new VBox(vGap);  // 方法描述面板

        pane.setStyle("-fx-background-color: white");  // 整个窗口的背景颜色
        paneClassName.setStyle("-fx-border-color: black; -fx-background-color: rgb(110,110,110)");  // 类名面板边界及背景色
        vBoxProperties.setStyle("-fx-border-color: black; -fx-background-color: rgb(215,215,215)");  // 属性面板面板边界及背景色
        vBoxMethods.setStyle("-fx-border-color: black; -fx-background-color: rgb(215,215,215)");  // 方法面板边界及背景色
        vBoxPropertiesDesc.setStyle("-fx-border-color: black");  // 属性描述面板背景色
        vBoxMethodsDesc.setStyle("-fx-border-color: black");  // 方法描述面板背景色

        // 主面板设置
        pane.setAlignment(Pos.CENTER);  // 居中
        pane.setPadding(new Insets(paddingOfWholePane,paddingOfWholePane,paddingOfWholePane,paddingOfWholePane));  // 内边距
        pane.setHgap(hGap);  // 内行距

        // 类名面板设置
        Label labelClassName = new Label(umlClass.name);  // 类名标签
        labelClassName.setFont(fontTitle);  // 字体
        paneClassName.getChildren().add(labelClassName);  // 面板添加标签
        paneClassName.setPadding(new Insets(paddingOfTitle, 0, paddingOfTitle, 0));  // 上下内边距

        // UML面板内边距设置
        vBoxProperties.setPadding(new Insets(paddingOfInnerPane,paddingOfInnerPane,paddingOfInnerPane,paddingOfInnerPane));
        vBoxPropertiesDesc.setPadding(new Insets(paddingOfInnerPane,paddingOfInnerPane,paddingOfInnerPane,paddingOfInnerPane));
        vBoxMethods.setPadding(new Insets(paddingOfInnerPane,paddingOfInnerPane,paddingOfInnerPane,paddingOfInnerPane));
        vBoxMethodsDesc.setPadding(new Insets(paddingOfInnerPane,paddingOfInnerPane,paddingOfInnerPane,paddingOfInnerPane));

        // 遍历属性及描述
        for (Property property : umlClass.properties){
            vBoxProperties.getChildren().add(new Label("-" + property));
            vBoxPropertiesDesc.getChildren().add(new Label(property.desc));
        }

        // 遍历方法及描述
        for (Method method : umlClass.methods){
            vBoxMethods.getChildren().add(new Label(method.toString()));
            vBoxMethodsDesc.getChildren().add(new Label(method.desc));
        }

        // 添加到主面板
        pane.add(paneClassName, 0, 0);
        pane.addRow(1, vBoxProperties, vBoxPropertiesDesc);
        pane.addRow(2, vBoxMethods, vBoxMethodsDesc);

        return pane;
    }

    public static class Property{
        String name;
        String type;
        String desc;
        /**
         * 属性 / 输入参数<br>
         * <table>
         *     <tr><th> n </th><th>-</th><th> 名称 </th></tr>
         *     <tr><th> t </th><th>-</th><th> 类型 </th></tr>
         *     <tr><th> d </th><th>-</th><th> 描述 </th></tr>
         * </table>
         * */
        public Property(String n, String t, String d){
            name = n;
            type = t;
            desc = d;
        }

        public Property(String n, String t){
            this(n, t, "");
        }

        @Override
        public String toString() {
            return name + ": " + type;
        }
    }

    public static class Method{
        String name;
        String desc;
        String methodReturn = "";
        LinkedList<Property> properties = new LinkedList<>();

        /**
         * 方法<br>
         * <table>
         *     <tr><th> n </th><th>-</th><th> 名称 </th></tr>
         *     <tr><th> d </th><th>-</th><th> 描述 </th></tr>
         * </table>
         * */
        public Method(String n, String d){
            name = n;
            desc = d;
        }

        public Method(String n){
            this(n, "");
        }

        /**
         * 方法<br>
         * <table>
         *     <tr><th> n </th><th>-</th><th> 名称 </th></tr>
         *     <tr><th> d </th><th>-</th><th> 描述 </th></tr>
         *     <tr><th> r </th><th>-</th><th> 返回类型 </th></tr>
         * </table>
         * */
        public Method(String n, String d, String r){
            this(n, d);
            methodReturn = r;
        }

        /**
         * 方法<br>
         * <table>
         *     <tr><th> n </th><th>-</th><th> 名称 </th></tr>
         *     <tr><th> d </th><th>-</th><th> 描述 </th></tr>
         *     <tr><th> ps </th><th>-</th><th> 输入参数 </th></tr>
         * </table>
         * */
        public Method(String n, String d, Property...ps){
            this(n, d);
            this.addProperties(ps);
        }

        /**
         * 方法<br>
         * <table>
         *     <tr><th> n </th><th>-</th><th> 名称 </th></tr>
         *     <tr><th> d </th><th>-</th><th> 描述 </th></tr>
         *     <tr><th> r </th><th>-</th><th> 返回类型 </th></tr>
         *     <tr><th> ps </th><th>-</th><th> 输入参数 </th></tr>
         * </table>
         * */
        public Method(String n, String d, String r, Property...ps){
            this(n, d, r);
            this.addProperties(ps);
        }

        public void addProperty(Property p){
            properties.add(p);
        }

        public void addProperties(Property...ps){
            properties.addAll(Arrays.asList(ps));
        }

        @Override
        public String toString() {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("+").append(name).append('(');
            Iterator<Property> iterator = properties.iterator();
            while (iterator.hasNext()){
                stringBuilder.append(iterator.next());
                if (iterator.hasNext())stringBuilder.append(", ");
            }
            stringBuilder.append(')');
            if (!methodReturn.equals("")) stringBuilder.append(": ").append(methodReturn);
            return stringBuilder.toString();
        }
    }

    public static class UMLClass{
        String name;
        LinkedList<Property> properties = new LinkedList<>();
        LinkedList<Method> methods = new LinkedList<>();

        /**
         * 类<br>
         * <table>
         *     <tr><th> n </th><th>-</th><th> 名称 </th></tr>
         * </table>
         * */
        public UMLClass(String n){
            name = n;
        }

        public void addProperties(Property...ps){
            properties.addAll(Arrays.asList(ps));
        }

        public void addMethods(Method...ms){
            methods.addAll(Arrays.asList(ms));
        }
    }
}


  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值