Swing JDialog中的自定义光标 [英] Custom Cursor in a Swing JDialog
问题描述
我有一个Java Swing应用程序,使用Java 1.5在Mac OS X 10.5上开发。
I have a Java Swing application, developed on Mac OS X 10.5 using Java 1.5.
我试图在用户移动时显示自定义光标将鼠标悬停在对话框中的某些文本上但是,光标永远不会改变。
I'm trying to make a custom cursor appear when the user moves the mouse over some text in a dialog. The cursor never changes, though.
当我不使用JFrame而不是JDialog时,光标会发生变化。但是我必须自己编写所有对话框代码。
When I don't use a JFrame instead of a JDialog, the cursor does change. But then I'll have to write all the dialog code myself.
如何让光标出现?
这是我可以创建的最简单的代码来演示问题:
Here's the simplest code I could create to demonstrate the problem:
import javax.swing.*;
import java.awt.*;
public class CursorTest {
public static void main(String[] args) {
JLabel label = new JLabel("Move mouse here for hand cursor");
label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
JOptionPane pane = new JOptionPane(label);
pane.setOptions(new Object[]{"OK"});
JDialog dialog = pane.createDialog(null, "Test Dialog");
dialog.setVisible(true);
}
}
推荐答案
看起来就像它是Java 1.5中的一个错误:我首先尝试使用Java 1.6.0_07,它按预期工作(在Windows XP上)。然后我用Java 1.5.0_06重新编译,确实光标仍处于默认状态。
Looks like it is a bug in Java 1.5: I first tried with Java 1.6.0_07 and it worked as expected (on Windows XP). Then I recompiled with Java 1.5.0_06 and indeed the cursor remains in default state.
了解MacOS上Java 1.6的困难,我发现很难解决这个问题。 ...
Knowing the difficulties of Java 1.6 on MacOS, I see it will be hard to fix that...
错误ID:5079694 JDialog不尊重setCursor
他们给出了解决方法......
Bug ID: 5079694 JDialog doesn't respect setCursor
They give a workaround...
经过测试的解决方法:
Tested workaround:
public class CursorTest extends JFrame
{
private CursorTest()
{
}
private void ShowDialog()
{
JLabel label = new JLabel("Move mouse here for hand cursor");
label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
JOptionPane pane = new JOptionPane(label);
pane.setOptions(new Object[] { "OK" } );
JDialog dialog = pane.createDialog(this, "Test Dialog");
dialog.setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
CursorTest testFrame = new CursorTest();
testFrame.setTitle("Test GUI");
testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
testFrame.setSize(500, 300);
testFrame.setVisible(true);
testFrame.ShowDialog();
}
});
}
}
我的JDK&系统。
Works fine with my JDK & system.
这篇关于Swing JDialog中的自定义光标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!