自定义光标在一个Swing的JDialog
-
10-07-2019 - |
题
我有一个Java Swing应用程序,使用Java 1.5在Mac OS X 10.5开发的。
我试图让当用户在一个对话框中一些文字上移动鼠标的自定义光标出现。光标永远不会改变,虽然。
当我不使用一个JFrame代替一个JDialog的,光标不变化。但后来我不得不写所有的对话框代码自己。
我怎样才能光标出现?
下面是最简单的代码,我可以创建演示问题:
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和确实光标保持在默认状态。
知道在MacOS的Java 1.6的困难,我看这将是很难解决这个问题...
错误ID:5079694的JDialog不尊重的setCursor结果 他们给一个解决方法......
[编辑]测试解决方法:
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&系统。
其他提示
由于PhiLho,孙bug报告给我的解决方案。雇主(父帧)必须非空和展示。为了记录在案,这里这么的我的示例代码修改后的版本不的显示手形光标。
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"});
JFrame parent = new JFrame();
parent.setVisible(true);
JDialog dialog = pane.createDialog(parent, "Test Dialog");
dialog.setModal(false);
dialog.setVisible(true);
}
}
不隶属于 StackOverflow