jSlider转到单击的位置-仅在Macintosh上存在问题 [英] jSlider go to clicked position - problem only on Macintosh
问题描述
我一直在为我的所有JSlider使用上面的MetalSliderUI解决方案. Windows用户对此感到满意.我非常感谢在stackoverflow.com上找到此解决方案 解决方案在这里进行了讨论: JSlider问题:左击后的位置
I have been using the above MetalSliderUI solution for all my JSliders. Windows users have been happy with it. I appreciate finding this solution on stackoverflow.com The solution was discussed here: JSlider question: Position after leftclick
现在,一个MAC OSX/64位用户正在尝试使用我的软件,他从MetalSliderUI引用中获取了空指针异常.
Now recently a MAC OSX/64bit user is trying to use my software and he gets null pointer exceptions from the MetalSliderUI references.
我添加到JFrame的构造函数中的代码示例:
Sample of code which I added to JFrame's constructor:
// 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 which does not quote the exact line in my code:
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上出现的此问题有任何建议吗?
Do you have any suggestions for fixing this problem which only occurs on Macs ?
霍华德
推荐答案
The problem is in line 92 of MetalSliderUI
, which wants a UI default value named Slider.trackWidth
, among others. The value does not appear in com.apple.laf.AquaSliderUI
.
trackWidth = ((Integer)UIManager.get("Slider.trackWidth")).intValue();
一种简单的替代方法是使用 BasicSliderUI
.一种更复杂的方法是初始化MetalLookAndFeel
并保存对子类的引用以供以后使用.
A simple alternative is to use BasicSliderUI
. A more complex approach is to initialize the MetalLookAndFeel
and save a reference to your subclass for later use.
LookAndFeel save = UIManager.getLookAndFeel();
LookAndFeel laf = new MetalLookAndFeel();
UIManager.setLookAndFeel(laf);
SliderUI mySliderUI = new MetalSliderUI() { ... };
UIManager.setLookAndFeel(save);
这篇关于jSlider转到单击的位置-仅在Macintosh上存在问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!