想重绘JSlider
的标记或拇指,而不是标准的灰色。我怎样才能做到这一点?
最佳答案
有三种方法:
Preferred of Ways
使用Synth的示例
SynthSliderTest.java
import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.synth.*;
public class SynthSliderTest {
private JFrame f = new JFrame();
public SynthSliderTest() {
try {
SynthLookAndFeel laf = new SynthLookAndFeel();
laf.load(SynthSliderTest.class.getResourceAsStream("yourPathTo/demo.xml"), SynthSliderTest.class);
UIManager.setLookAndFeel(laf);
} catch (Exception e) {
e.printStackTrace();
}
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public JComponent makeUI() {
JSlider slider = new JSlider(0, 100);
JPanel p = new JPanel();
p.add(slider);
return p;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
SynthSliderTest synthSliderTest = new SynthSliderTest();
}
});
}
}
demo.xml
文件<?xml version="1.0" encoding="UTF-8"?>
<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="RED"/>
</state>
<state value="PRESSED">
<color type="BACKGROUND" value="GREEN"/>
</state>
<!-- state value="MOUSE_OVER">
<color type="BACKGROUND" value="BLUE"/>
</state -->
</style>
<bind style="SliderThumbStyle" type="region" key="SliderThumb" />
</synth>