Java Swing getToolkit 和自定义光标奇怪的行为
我正在我的 Netbeans Java Swing 应用程序中创建自定义光标。例如,当“cursorImage”是磁盘上的图像文件时,我会像这样创建默认光标:
Cursor defaultCursor = toolkit.createCustomCursor(cursorImage, hotspot, name);
一旦我开始使用自定义光标,我的应用程序就开始出现非常奇怪的错误,我什至无法解释。通过排查,我发现问题出在下面的代码上。此代码与我的应用程序(Netbeans SingleFrameApplication)相关。
Toolkit toolkit = MyApp.getApplication().getMainView().getRootPane()
.getToolkit().getDefaultToolkit();
该代码存在于一个名为 CursorController 的简单非 GUI POJO 类中。
我用这段代码替换了它:
Component c = new JButton("getToolkit");
Toolkit toolkit = c.getToolkit();
我什至不在任何地方显示该按钮。但现在 bug 已经消失了。
问题是我不明白这个问题或者为什么它现在(显然)得到解决。我不太有信心我已经以正确的方式解决了这个问题。我不应该仅仅为了获取工具包而创建一个按钮,对吧?
I am creating custom cursors in my Netbeans Java Swing app. For example, where 'cursorImage' is an image file on disk, I create my default cursor like so:
Cursor defaultCursor = toolkit.createCustomCursor(cursorImage, hotspot, name);
Once I starting using custom cursors, my application started exhibiting very strange bugs that I cannot even explain. Through troubleshooting, I found the following code to be the problem. This code is related to my app being a Netbeans SingleFrameApplication.
Toolkit toolkit = MyApp.getApplication().getMainView().getRootPane()
.getToolkit().getDefaultToolkit();
The code exists in a simple non-GUI POJO class called CursorController.
I replaced it by this code:
Component c = new JButton("getToolkit");
Toolkit toolkit = c.getToolkit();
I don't even display the button anywhere. But the bugs are gone now.
The problem is that I do not understand the problem or why it is (apparently) resolved now. I'm not very confident that I have solved it the right way. I shouldn't really create a button just to get the toolkit, right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论