如何在java中加载合成器样式

时间:2012-11-26 21:38:53

标签: java xml load styles synth

我正在尝试合成器风格,我遇到了以下问题,经过相当多的研究,一个避难所尚未找到答案。我有以下xml文件

<?xml version="1.0" encoding="UTF-8"?>
<synth>
    <style id="button">
        <state>
            <color value="blue" type="BACKGROUND"/>
            <color value="yellow" type="FOREGROUND"/>
        </state>
    </style>
    <bind style="button" type="region" key="Button"/>
</synth>

和以下java代码将该xml文件加载为SynthLookAndFeel对象

private void initializeStyle() {
    SynthLookAndFeel laf = new SynthLookAndFeel();
    try {
        laf.load(this.getClass().getResourceAsStream("Style.xml"), this.getClass());
        UIManager.setLookAndFeel(laf);
    } catch (ParseException | UnsupportedLookAndFeelException e) {
        e.printStackTrace();
    }
}

当我尝试运行此代码时,我得到以下异常

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: You must supply an InputStream, StyleFactory and Class or URL

有关如何解决此问题的任何建议都非常受欢迎。

2 个答案:

答案 0 :(得分:2)

您可以在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包中。

我希望这可以帮到你。

答案 1 :(得分:0)

Main Calss:

  

SynthLookAndFeel lookAndFeel = new SynthLookAndFeel();   lookAndFeel.load(TestRApp.class.getResourceAsStream(synthFile),   TestRApp.class); UIManager.setLookAndFeel(LookAndFeel中);

synth.xml文件    

<style id="panelStyle">
    <imagePainter method="panelBackground" path="images/main-bg.png" sourceInsets="0 0 0 0" stretch="true"/>
</style>
<bind style="panelStyle" type="region" key="Panel"/>