2015-10-30 29 views
4

以下應用:矩形的樣式可以顯示邊框嗎?

public class Temp extends Application { 

    @Override 
    public void start(Stage primaryStage) { 
     StackPane root = new StackPane(); 

     Rectangle rect = new Rectangle(10.0,10.0); 
     rect.setStyle("-fx-fill: red; -fx-border-style: solid; -fx-border-width: 5; -fx-border-color: black;"); 
     root.getChildren().add(rect); 

     Scene scene = new Scene(root, 100, 100); 

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

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     launch(args); 
    } 
} 

生成以下窗口:

enter image description here

爲什麼沒有我的長方形有厚厚的黑色邊框?

+1

請參閱[CSS參考](https://docs.oracle.com/javafx/2/api/javafx/scene/doc-files/cssref.html#shape)(一個'Rectangle'是一個'Shape' )。邊界被稱爲_stroke_ – Vertex

+0

@Vertex有沒有辦法讓筆畫只在矩形的一些邊而不是全部都可見? (我猜不是,因爲一個'形狀'並不真正知道「邊」。) – Museful

+0

不,它不是。你不得不手動添加線條,如果你不這樣做。 – Vertex

回答

4

編號:Rectangle不支持-fx-border等:只有Region和子類。

所以儘量

public class Temp extends Application { 

    @Override 
    public void start(Stage primaryStage) { 
     StackPane root = new StackPane(); 

     Region rect = new Region(); 
     rect.setStyle("-fx-background-color: red; -fx-border-style: solid; -fx-border-width: 5; -fx-border-color: black; -fx-min-width: 20; -fx-min-height:20; -fx-max-width:20; -fx-max-height: 20;"); 
     root.getChildren().add(rect); 

     Scene scene = new Scene(root, 100, 100); 

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

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     launch(args); 
    } 
} 

或者,您也可以使用 「嵌套的背景」 實現你的邊界:

rect.setStyle("-fx-background-color: black, red; -fx-background-insets: 0, 5; -fx-min-width: 20; -fx-min-height:20; -fx-max-width:20; -fx-max-height: 20;"); 

而且你只能添加邊框一些雙方

rect.setStyle("-fx-background-color: black, red; -fx-background-insets: 0, 5 5 0 0; -fx-min-width: 20; -fx-min-height:20; -fx-max-width:20; -fx-max-height: 20;"); 
3

試試這種風格:

rect.setStyle("-fx-fill: red; -fx-stroke: black; -fx-stroke-width: 5;"); 

這是全部在JavaFX CSS Reference Guide的第一部分。