简体   繁体   English

jSlider go 点击 position - 仅在 Macintosh 上存在问题

[英]jSlider go to clicked position - problem only on Macintosh

I have been using the above MetalSliderUI solution for all my JSliders.我一直在为我的所有 JSlider 使用上述 MetalSliderUI 解决方案。 Windows users have been happy with it. Windows 用户对它很满意。 I appreciate finding this solution on stackoverflow.com The solution was discussed here: JSlider question: Position after leftclick我很高兴在 stackoverflow 上找到此解决方案。com 此处讨论了该解决方案: JSlider question: Position after leftclick

Now recently a MAC OSX/64bit user is trying to use my software and he gets null pointer exceptions from the MetalSliderUI references.现在最近一个 MAC OSX/64 位用户正在尝试使用我的软件,他从 MetalSliderUI 引用中得到了 null 指针异常。

Sample of code which I added to JFrame's constructor:我添加到 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 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)

Do you have any suggestions for fixing this problem which only occurs on Macs?您对解决仅在 Mac 上发生的此问题有什么建议吗?

Howard霍华德

It is hard to say without an SSCCE (even harder without a Mac. to test with) but these problems are often caused by failing to update the GUI on the EDT.没有SSCCE很难说(没有 Mac 就更难测试了),但这些问题通常是由于未能在 EDT 上更新 GUI 造成的。 See the Concurrency in Swing lesson of the Java Tutorial for more details.有关详细信息,请参阅 Java 教程的Swing课程中的并发。

The problem is in line 92 of MetalSliderUI , which wants a UI default value named Slider.trackWidth , among others.问题出在MetalSliderUI的第 92 行,它需要一个名为Slider.trackWidth等的 UI 默认值。 The value does not appear in com.apple.laf.AquaSliderUI .该值未出现在com.apple.laf.AquaSliderUI中。

trackWidth = ((Integer)UIManager.get("Slider.trackWidth")).intValue();

A simple alternative is to use BasicSliderUI .一个简单的替代方法是使用BasicSliderUI A more complex approach is to initialize the MetalLookAndFeel and save a reference to your subclass for later use.更复杂的方法是初始化MetalLookAndFeel并保存对子类的引用以供以后使用。

LookAndFeel save = UIManager.getLookAndFeel();
LookAndFeel laf = new MetalLookAndFeel();
UIManager.setLookAndFeel(laf);
SliderUI mySliderUI = new MetalSliderUI() { ... };
UIManager.setLookAndFeel(save);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM