2008-11-24 112 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