有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在JavaFX中扩展窗格

使用我在查看this answer时学到的东西,我想实现一个自定义窗格。此窗格名为BackgroundEffectPane,其主要目的是产生效果并仅将其应用于背景。这将使我能够以更干净的方式实现链接答案中的半透明背景窗格

到目前为止,我已经阅读了PaneNode的文档。到目前为止,我还没有看到任何明显的方法可以覆盖以尽可能干净地完成这项工作。我觉得唯一相关的是窗格中的getChildren()方法

这样做对吗

Pane是子类的正确类吗

TLDR:试图创建自定义窗格,我应该覆盖哪些方法。我想做的就是给背景添加效果


共 (1) 个答案

  1. # 1 楼答案

    我不会放弃任何方法。如果要创建提供此功能的StackPane子类,只需在构造函数中调用getChildren().addAll(...)

    public class BackgroundEffectPane extends StackPane {
    
        public BackgroundEffectPane(Node content) {
            getChildren().addAll(createBackground(), freeze(...), content);
        }
    
        // code refactored from other answer...
    }
    

    当然,现在你真的不再需要子类了:

    public class BackgroundEffectPane {
    
        private final Node content ;
    
        private final Parent effectPane ;
    
        public BackgroundEffectPane(Node content) {
            this.content = content ;
            this.effectPane = new StackPane(createBackground(), freeze(...), content);
        }
    
        public Parent getEffectPane() {
            return effectPane ;
        }
    
        // other code...
    }
    

    通过不公开窗格的底层实现(即API不公开您正在使用的StackPane),可以更好地封装类