2008-11-24 60 views
4

我有一个Java Swing应用程序,使用Java 1.5在Mac OS X 10.5上开发。Swing中的自定义光标JDialog

我想让用户在对话框中的某些文本上移动鼠标时出现自定义光标。但是光标不会改变。

当我不使用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); 
    } 
} 

回答

4

看起来它是Java 1.5中的错误:我第一次与Java 1.6.0_07试着和它的工作如预期(在Windows XP )。然后我用Java 1.5.0_06重新编译,实际上游标保持默认状态。

知道在MacOS的Java 1.6的困难,我看这将是很难修复......

Bug ID: 5079694 JDialog doesn't respect 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 &系统。

2

谢谢PhiLho,那个Sun 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); 
    } 
} 
+0

non null?无论如何你给了一个null的变量? 〜对不起,我试图理解你的线程...... :( – gumuruh 2011-08-15 10:03:22