2012-07-19 63 views
2

我使用如何在xml文件中使用SynthLookAndFeel,其中xml文件路徑將使用getResourceAsStream方法加載文件?

SynthLookAndFeel laf = new SynthLookAndFeel(); 
laf.load(MyJSlider.class.getResourceAsStream("/ui/demo.xml"), MyJSlider.class);  
//laf.load(new URL("file:///var/tmp/demo.xml")); 

凡我demo.xml有以下:

<style id="SliderTrackStyle"> 
    <opaque value="TRUE"/> 
    <state> 
     <imagePainter method="SliderTrackBackground" path="/var/tmp/JavaUnitTest/src/image/menu/bg.jpg" sourceInsets="0 0 0 0" /> 
    </state> 
</style> 
<bind style="SliderTrackStyle" type="region" key="SliderTrack" /> 

導致錯誤:

run-single: 
Uncaught error fetching image: 
java.lang.NullPointerException 
    at sun.awt.image.URLImageSource.getConnection(URLImageSource.java:115) 
    at sun.awt.image.URLImageSource.getDecoder(URLImageSource.java:125) 
    at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:263) 
    at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:205) 
    at sun.awt.image.ImageFetcher.run(ImageFetcher.java:169) 
BUILD STOPPED (total time: 9 seconds) 

爲了避免和有較少的風險。

我如何使用它像path=Menu.class.getResourceAsStream("/image/menu/bg.jpg")我想避免使用示例中顯示的靜態路徑。

追問:

enter image description here

+1

請確實看看[如何添加圖像到您的項目](http://stackoverflow.com/a/9866659/1057230)和這[回答](http:// stackoverflow。 com/a/11372350/1057230)我的 – 2012-07-19 18:02:19

回答

3

可以在Advanced Synth找到一個很好的例子。

你可以看到的例子是:

SynthLookAndFeel synth = new SynthLookAndFeel(); 
synth.load(SynthFrame.class.getResourceAsStream("demo.xml"), SynthFrame.class); 
UIManager.setLookAndFeel(synth); 

而且demo.xml生活demo.synth包,同樣SynthFrame類,在此XML包含:

<imageIcon id="check_off" path="images/checkbox_off.png"/> 

checkbox_off.png生活demo.synth.images包。

+0

謝謝。不錯的文章。我也修改了我的後續部分,以獲得更好的照片。 – YumYumYum 2012-07-23 18:04:37

3
  1. to check,特別是@trashgod回答顯示了基本的東西,但有很大的代碼解決方法BasicXxxUI

  2. 我會使用Nimbus Look and Feel而史前和滑動Synth

  3. Nimbus有similair問題與GUI,aaaa和一些錯誤此外,

  4. Nimbus(基於普通Synt)可能是最簡單的方式和我們周圍是一個偉大的饑荒早期預警系統的黑客

1

您還可以通過傳遞文件

的整個路徑例如添加XML文件

SynthLookAndFeel laf = new SynthLookAndFeel(); 
laf.load(new URL("file:///D:/Demo/src/example1.xml")); 
UIManager.setLookAndFeel(laf); 

它適用於我。嘗試它爲您的應用程序

相關問題