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



我正在尝试合成器风格,我遇到了以下问题,经过相当多的研究还没有找到答案。我有以下 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
任何

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

你可以在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包裹中的地方。

我希望这能帮助你。

Main Calss :

SynthLookAndFeel

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

合成器.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"/>

最新更新