我一直在为所有JSliders使用上面的MetalSliderUI解决方案。 Windows用户对此很满意。我很高兴在stackoverflow.com上找到这个解决方案 这里讨论了解决方案: JSlider question: Position after leftclick
最近,MAC OSX / 64位用户正在尝试使用我的软件,他从MetalSliderUI引用中获取空指针异常。
我添加到JFrame构造函数的代码示例:
// Radio Window Elecraft K3 RFPWR Slider - when click on slider
// go to the value instead of going up/down one tick.
jSliderElecraftK3RFPWR.setUI(
new MetalSliderUI() {
protected void scrollDueToClickInTrack(int direction) {
int value = jSliderElecraftK3RFPWR.getValue();
if (jSliderElecraftK3RFPWR.getOrientation() == JSlider.HORIZONTAL) {
value = this.valueForXPosition(jSliderElecraftK3RFPWR.getMousePosition().x);
} else if (jSliderElecraftK3RFPWR.getOrientation() == JSlider.VERTICAL) {
value = this.valueForYPosition(jSliderElecraftK3RFPWR.getMousePosition().y);
}
jSliderElecraftK3RFPWR.setValue(value);
}
}
);
在我的代码中没有引用确切行的异常:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at
javax.swing.plaf.metal.MetalSliderUI.installUI(MetalSliderUI.java:92) at
javax.swing.JComponent.setUI(JComponent.java:662) at
javax.swing.JSlider.setUI(JSlider.java:300) at
HamRadioIntegrator_N3ZH.JFrameRadio.(JFrameRadio.java) at
HamRadioIntegrator_N3ZH.JFrameRadio$550.run(JFrameRadio.java) at
java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209) at
java.awt.EventQueue.dispatchEventImpl(EventQueue.java:678) at
java.awt.EventQueue.access$000(EventQueue.java:86) at
java.awt.EventQueue$1.run(EventQueue.java:639) at
java.awt.EventQueue$1.run(EventQueue.java:637) at
java.security.AccessController.doPrivileged(Native Method) at
java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:648) at
java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296) at
java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211) at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201) at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196) at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188) at
java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
您是否有任何解决此问题的建议仅在Mac上发生?
霍华德
答案 0 :(得分:1)
很难说没有SSCCE(没有Mac就更难以测试),但这些问题通常是由于未能在EDT上更新GUI而引起的。有关详细信息,请参阅Java教程的Concurrency in Swing课程。
答案 1 :(得分:1)
问题出在MetalSliderUI
的第92行,其中包含名为Slider.trackWidth
的UI默认值等。该值未显示在com.apple.laf.AquaSliderUI
中。
trackWidth = ((Integer)UIManager.get("Slider.trackWidth")).intValue();
一个简单的替代方法是使用BasicSliderUI
。更复杂的方法是初始化MetalLookAndFeel
并保存对子类的引用以供以后使用。
LookAndFeel save = UIManager.getLookAndFeel();
LookAndFeel laf = new MetalLookAndFeel();
UIManager.setLookAndFeel(laf);
SliderUI mySliderUI = new MetalSliderUI() { ... };
UIManager.setLookAndFeel(save);