2010-10-02 144 views
1

創建自定義有底代碼光標不同:爪哇 - 自定義光標是在不同的操作系統

Toolkit toolkit = Toolkit.getDefaultToolkit(); 
Image image = toolkit.getImage("C:/Users/Administrator/Desktop/gaea/core/ui/gaeawindow/src/si/xlab/gaea/core/ui/gaeawindow/HandCursor.gif"); 

// Somewhere in mouse pressed action 

public void mousePressed(MouseEvent e) 
    { 
     Cursor cursor = toolkit.createCustomCursor(imageClose, new Point(12,12), "Hand"); 
     e.getComponent().setCursor(cursor); 
    } 

光標是在Mac上顯示像它應該是,但在模擬的Windows 7事實並非如此。它顯示增加,而且很醜。

我應該在我的代碼中應用什麼修復/技巧來解決這個問題?這是常見問題嗎?

+0

「Emulated」?你的意思是並行桌面還是什麼? – 2010-10-02 22:28:32

+0

是的,我的意思是在Parallels中。 – Ivansek 2010-10-03 08:41:17

回答

0

可能模擬Windows 7無法找到圖像文件。 您應該將圖像文件移動到java路徑文件旁邊,以便您可以使用getClass()。getResource()加載此文件。

它應該在模擬的Windows 7和Mac上工作。

public class CursorTest extends JFrame { 
    public CursorTest() { 

     Toolkit toolkit = Toolkit.getDefaultToolkit(); 
     URL url = getClass().getResource("/si/xlab/gaea/core/ui/gaeawindow/HandCursor.gif"); 

     Image image = null; 
     try { 
      image = ImageIO.read(url.openStream()); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     Cursor cursor = toolkit.createCustomCursor(image, new Point(12, 12), 
       "Hand"); 
     setCursor(cursor); 

     setSize(new Dimension(200, 200)); 
     setVisible(true); 
    } 
} 
1

問題是Windows需要32x32光標,如果不是,則會縮放圖像。 Mac更靈活。

最簡單的解決方案是用透明像素填充現有的16x16光標到32x32;這將在兩個平臺上運行。

您可以使用

Toolkit.getDefaultToolkit().getBestCursorSize(w,h) 

,看是否支持給定大小。

欲瞭解更多信息,請參閱: http://forums.sun.com/thread.jspa?threadID=5424409 其中也有鏈接到MS站點。