我在互联网上找到了关于如何使用xml来自定义Java中的Slider的示例,一切正常:
import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.swing.plaf.synth.SynthLookAndFeel;
public class Main {
public static void main(String[] args) {
try {
SynthLookAndFeel laf = new SynthLookAndFeel();
File initialFile = new File(
"/pathTo/jslider.xml");
InputStream targetStream = new FileInputStream(initialFile);
// laf.load(Test.class.getResourceAsStream("demo.xml"), Test.class);
laf.load(targetStream, Main.class);
UIManager.setLookAndFeel(laf);
} catch (Exception e) {
e.printStackTrace();
}
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JSlider jslider = new JSlider() {
{
setVisible(true);
}
};
jslider.putClientProperty("JSlider.isFilled", Boolean.TRUE);
f.add(jslider);
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
使用xml文件:
<synth>
<style id="backingStyle">
<opaque value="TRUE"/>
<font name="Dialog" size="12"/>
<state>
<color value="WHITE" type="BACKGROUND"/>
<color value="BLACK" type="FOREGROUND"/>
</state>
</style>
<bind style="backingStyle" type="region" key=".*"/>
<style id="SliderTrackStyle">
<opaque value="TRUE"/>
<state>
<color type="BACKGROUND" value="ORANGE"/>
</state>
</style>
<bind style="SliderTrackStyle" type="region" key="SliderTrack" />
<style id="SliderThumbStyle">
<opaque value="TRUE"/>
<state>
<color type="BACKGROUND" value="GRAY"/>
</state>
<state value="PRESSED">
<color type="BACKGROUND" value="BLACK"/>
</state>
</style>
<bind style="SliderThumbStyle" type="region" key="SliderThumb" />
</synth>
基本上我有两个问题:
1)现在您可以看到我使用过:
jslider.putClientProperty("JSlider.isFilled", Boolean.TRUE);
因为我希望看到左侧部分已填满。但这不起作用。
2)除此之外,我想定义用于填充该部分的颜色,但我无法在互联网上找到xml标签来定义该颜色。
有什么建议吗? 先感谢您。