簡體   English   中英

JavaFX模糊效果以及layoutX和layoutY

[英]JavaFX blur effect and layoutX and layoutY

我嘗試了框架的效果,但是當我將文本字段模糊化為Parent時,它具有一些奇怪的行為,該文本字段位於另一個位置,請看一下:

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);
    }

}

和我的自定義textview:

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;
    }
}

我不明白為什么文本字段在模糊效果后會在父級中重新定位。.:/謝謝您的幫助

> 為什么要重新定位文本字段?
GaussianBlur的默認半徑值為10。當將此效果應用於節點時,該節點的局部邊界會額外擴大這些模糊半徑,但節點的寬度和高度保持不變。 Parent不應用CSS樣式,也不布局其子級,但是,如您的示例所示,它考慮了局部范圍並重新放置了節點。

> 為什么文本字段的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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM