2016-02-25 54 views

回答

1

你不寫一個CSS爲每個視圖,你給每個元素相同的風格類。

Pane pane = new Pane(); 
    pane.getStyleClass().add("bg-black-style"); 

某處你需要將樣式表添加到場景

scene.getStylesheets().add("css-file.css"); 

並在CSS文件

.bg-black-style { 
    -fx-background-color: black; 
} 

這樣每一件事應該看起來一樣有它的風格都在一個地方。

+0

我喜歡這個解決方案在蠻力'.pane {/ * ... * /}'變體。 – dzim

1

你可以在CSS類中使用.pane,它將適用於所有窗格。

.pane{ 
     -fx-background-color: black; 
    } 

相同的作品與.button等

0

您可以將樣式表應用於這樣整個應用:

package hacks; 

import javafx.application.Application; 
import javafx.collections.FXCollections; 
import javafx.collections.ObservableList; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.ListView; 
import javafx.scene.control.TextArea; 
import javafx.scene.layout.FlowPane; 
import javafx.stage.Stage; 

import java.net.URL; 

/** 
* Created by BDay on 7/10/17.<br> 
* <br> 
* CssStyle sets the style for the entire project 
*/ 
public class CssStyle extends Application { 
    private String yourCss = "YourResource.css"; 

    public CssStyle() { 
     try { 
      Application.setUserAgentStylesheet(getCss()); //null sets default style 
     } catch (NullPointerException ex) { 
      System.out.println(yourCss + " resource not found"); 
     } 
    } 

    private Button button = new Button("Button Text"); 
    private TextArea textArea = new TextArea("you text here"); 
    private ObservableList<String> listItems = FXCollections.observableArrayList("one", "two", "three"); 
    private ListView listView = new ListView<String>(listItems); 
    private FlowPane root = new FlowPane(button, textArea, listView); 
    private Scene scene = new Scene(root); 

    @Override 
    public void start(Stage primaryStage) throws Exception { 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

    private String getCss() throws NullPointerException { 
     ClassLoader classLoader = getClass().getClassLoader(); 
     URL resource = classLoader.getResource(yourCss); 
     String asString = resource.toExternalForm(); //throws null 
     return asString; 
    } 
} 
相關問題