【问题标题】:JavaFX blur effect and layoutX and layoutYJavaFX 模糊效果与布局和布局
【发布时间】:2013-12-04 13:13:05
【问题描述】:

我尝试了框架的效果,但是当我将文本字段模糊为父级时,它有一些奇怪的行为,文本字段位于不同的位置,请看一下:

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextField;
import javafx.scene.effect.GaussianBlur;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;


public class BlurTest extends Application {
    CTextView subPane = new CTextView(100,100);
    @Override
    public void start(Stage stage) throws Exception {

        VBox myBox = new VBox();

        CheckBox chkBlur = new CheckBox("Show");

        chkBlur.selectedProperty().addListener(new ChangeListener<Boolean>(){

            @Override
            public void changed(ObservableValue<? extends Boolean> v,
                    Boolean oldValue, Boolean newValue) {
                if(oldValue)
                    subPane.getTxt().setEffect(new GaussianBlur());
                else
                    subPane.getTxt().setEffect(null);
            }

        });

        myBox.getChildren().addAll(new TextField("Not blur"), subPane, new TextField("Not blur"), chkBlur);
        myBox.setPrefSize(250, 500);

        Scene scene = new Scene(myBox);

        stage.setScene(scene);
        stage.show();
    }

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

}

还有我的自定义文本视图:

import javafx.scene.Parent;
import javafx.scene.control.TextField;


public class CTextView extends Parent {

    private TextField txt;

    public CTextView(double w, double h) {
        super();
        this.txt = new TextField("Default");

        this.txt.setLayoutX(20);
        this.txt.setLayoutY(20);

        this.getChildren().add(this.txt);
    }

    public TextField getTxt() {
        return txt;
    }
}

我不明白为什么在模糊效果之后文本字段在父级中重新定位..:/ 感谢您的帮助

【问题讨论】:

    标签: layout javafx blur effect


    【解决方案1】:

    > 为什么要重新定位文本字段?
    GaussianBlur 的默认半径值为 10。当此效果应用于节点时,该节点的局部边界会扩大这些模糊半径,但节点的宽度和高度保持不变。 Parent 不应用 CSS 样式并且不布局其子级,但是如您的示例所示,它考虑了本地边界并重新定位了节点。

    > 为什么 textfield 的 setLayoutX 和 setLayoutY 不起作用?
    Parent 确实考虑了其子元素的本地边界,但它没有根据子元素的布局值对其进行布局。使用 Region(或其子类)来处理其子布局值。

    public class CTextView extends Region {
    
        private TextField txt;
    
        public CTextView(double w, double h) {
            super();
            this.txt = new TextField("Default");
    
            this.txt.setLayoutX(20);
            this.txt.setLayoutY(20);
    
            this.getChildren().add(this.txt);
        }
    
        public TextField getTxt() {
            return txt;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-11-15
      • 2015-04-15
      • 2011-02-05
      • 2015-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多