2013-11-20 50 views
0

我做了一個框架效果的嘗試,但它有一些奇怪的行爲,當我將文本框模糊到父,文本框位於不同的地方,請看看:JavaFX模糊效果和佈局X和佈局Y

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

我不明白爲什麼文本字段中的模糊效果後,家長重新..:/ 感謝您的幫助

回答

0

>爲什麼文本框重新定位?
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; 
    } 
} 
+0

謝謝你的解釋,非常清楚:) – PacDroid