2013-07-27 164 views
0

我测试这个代码:无法加载CSS

LayoutSample.java

import javafx.application.Application; 
import javafx.geometry.Insets; 
import javafx.scene.Scene; 
import javafx.scene.layout.FlowPane; 
import javafx.scene.paint.Color; 
import javafx.scene.shape.Rectangle; 
import javafx.stage.Stage; 

public class LayoutSample extends Application 
{ 

    public static void main(String[] args) 
    { 
     launch(LayoutSample.class, args); 
    } 

    @Override 
    public void start(Stage stage) 
    { 

     FlowPane flow = new FlowPane(); 
     flow.setPadding(new Insets(5, 5, 5, 5)); 
     flow.setVgap(5); 
     flow.setHgap(5); 
     flow.setPrefWrapLength(170); // preferred width allows for two columns 
     flow.setStyle("-fx-background-color: white;"); 

     //ImageView pages[] = new ImageView[8]; 
     for (int i = 0; i < 28; i++) 
     { 
//   pages[i] = new ImageView(
//     new Image(LayoutSample.class.getResourceAsStream(
//     "graphics/chart_" + (i + 1) + ".png"))); 
      flow.getChildren().add(generateRectangle()); 
     } 

     Scene scene = new Scene(flow); 
     ///// 
     String cssURL = "ButtonsDemo.css"; 
     String css = this.getClass().getResource(cssURL).toExternalForm(); 
     scene.getStylesheets().add(css); 
     //// 
     stage.setScene(scene); 
     stage.setTitle("Layout Sample"); 
     stage.show(); 
    } 

    public Rectangle generateRectangle() 
    { 

     Rectangle rect2 = new Rectangle(10, 10, 10, 10); 
     rect2.setId("app"); 
     rect2.setArcHeight(8); 
     rect2.setArcWidth(8); 
     rect2.setFill(Color.AZURE); 
     //rect2.setX(10); 
     //rect2.setY(160); 
     rect2.setStrokeWidth(1); 
     rect2.setStroke(Color.BLACK); 
     rect2.setWidth(220); 
     rect2.setHeight(180); 

     return rect2; 
    } 
} 

ButtonsDemo.css

#app { 
    -fx-background-color: 
    linear-gradient(to bottom, #f2f2f2, #d4d4d4); 
} 

但CSS代码不在Rectangle上呈现。你能告诉我这是正确的Java代码。我怀疑我不是设置矩形标识。

回答

1

你的CSS装入正确,但你的风格的角色忽略,因为它并不适用于你正在尝试将其应用到节点类型。

要使用-fx-background-color,节点必须从Region派生。

一个Rectangle不是一个区域。

请参阅CSS reference on RegionsRectangle了解适用于这两种类型的属性的定义。另外,如果您使用-fx-fill为您的Rectangle设置渐变填充,我猜测它是您实际想要做的,那么您不应该在源代码中设置填充。