2014-07-25 3 views
0

JavaFx 애플리케이션 클래스에 CSS 리소스에 대한 경로를 포함 시키면 프로젝트가 완벽하게 실행됩니다.JavaFx Spring 애플리케이션에 CSS 파일을 포함하는 방법

public class SampleApp extends Application { 

    public static void main(String[] args) { 
     launch(args); 
    } 

    @Override 
    public void start(Stage stage) throws Exception { 
     AnnotationConfigApplicationContext context 
       = new AnnotationConfigApplicationContext(SampleAppFactory.class); 

     SampleController sampleController = context.getBean(SampleController.class); 
     Scene scene = new Scene((Parent) sampleController.getView(), 320, 240); 
     scene.getStylesheets().add("/resources/css/fxmlapp.css"); 

     stage.setScene(scene); 
     stage.setTitle("JFX2.0 Sprung"); 
     stage.show(); 
    } 
} 

그러나 FXML 파일에 CSS 경로를 포함 시키면 오류가 발생합니다.

<?xml version="1.0" encoding="UTF-8"?> 

<?import java.lang.*?> 
<?import java.net.*?> 
<?import java.util.*?> 
<?import javafx.scene.*?> 
<?import javafx.scene.control.*?> 
<?import javafx.scene.layout.*?> 

<StackPane fx:id="view" prefHeight="98.0" prefWidth="160.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="com.rev.SampleController"> 
    <children> 
    <Button fx:id="printBtn" onAction="#print" text="Click Me" /> 
    </children> 
    <stylesheets> 
    <URL value="@../css/fxmlapp.css" /> 
    </stylesheets> 
</StackPane> 

주 라인 <URL value="@../css/fxmlapp.css" />에 inclussion.

FXML에 포함 된 CSS로 작업하고 싶습니다. FXML을 CSS로 디자인하는 것이 훨씬 쉽습니다. 특정 CSS 속성이 페이지의 모양과 느낌에 어떤 영향을 주는지 확인할 수 있습니다. 어떻게 작동시킬 수 있습니까? 다들 감사 해요. 다음과 같이

다른 클래스

은 다음과 같습니다

@Configuration 
public class SampleAppFactory { 

    @Bean 
    public Person person() { 
     return new Person("Richard"); 
    } 

    @Bean 
    public SampleController sampleController() throws IOException { 
     return (SampleController) loadController("/resources/fxml/Sample.fxml"); 
    } 

    protected Object loadController(String url) throws IOException { 
     InputStream fxmlStream = null; 
     try { 
      fxmlStream = getClass().getResourceAsStream(url); 
      FXMLLoader loader = new FXMLLoader(); 
      loader.load(fxmlStream); 
      return loader.getController(); 
     } finally { 
      if (fxmlStream != null) { 
       fxmlStream.close(); 
      } 
     } 
    } 
} 

public class SampleController { 

    @FXML 
    private Node view; 
    @Autowired 
    private Person person; 

    public Node getView() { 
     return view; 
    } 

    public Person getPerson() { 
     return person; 
    } 

    public void print(ActionEvent event) { 
     System.out.println("Well done, " + person.getFirstName() + "!"); 
    } 
} 

파일 : fxmlapp.css


.root { 
    -fx-background-color: linear-gradient(from 0% 0% to 0% 100%, #cbd0d7 0%, white 100%); 
} 

#printBtn { 
    -fx-text-fill: #e4f3fc; 
    -fx-font: 20pt "Tahoma Bold"; 
    -fx-padding: 10; 
    -fx-base: #2d4b8e 
} 

#printBtn:hover{ 
    -fx-base: #395bae; 
} 

답변

1

사용 InputStream의 대신 java.net.URL, 예를

protected Object loadController(String url) throws IOException { 
    try { 
     FXMLLoader loader = new FXMLLoader(getClass().getResource(url)); 
     loader.load(); 
     return loader.getController(); 
    } 
    catch(...) {...} 
} 

이렇게하면 FXML 메커니즘이 상대 URI를 확인할 수 있습니다.

+0

감사합니다. @Nikos Paraskevopoulos. 완벽하게 작동합니다. –

관련 문제