2012-05-21 113 views
9

所以在這個應用程序中,用戶點擊一個按鈕,然後該按鈕啓動一個程序。該按鈕將具有應用程序的標題和圖標。我只需要知道如何讓圖標,有點像這個:Windows http://goo.gl/5WjdT如何獲取其他應用程序的圖標?

所以,我想知道的是這樣的:

  1. 有任何真正的辦法做到這一點在Java中
  2. 如果所以,你會怎麼做?

在此先感謝!

回答

9

您是否必須將圖標關聯到應用程序的exe文件?如果是這樣的話,可以在java中使用,請看this tutorial,它解釋了從Java應用程序的可執行二進制文件中提取應用程序圖標的幾種方法。

看看這個代碼:

String s = "c:/windows/regedit.exe"; 
File file = new File(s); 

// Get metadata and create an icon 
sun.awt.shell.ShellFolder sf = 
     sun.awt.shell.ShellFolder.getShellFolder(file); 
Icon icon = new ImageIcon(sf.getIcon(true)); 
+0

謝謝!你有一個教程,你舉了一個例子!如果我能的話+1! – mattbdean

+3

注意:sun。*中的類通常不是公開的API,並且可能隨每個JDK版本而改變。 – Puce

7

您可以簡單地使用的FileSystemView用於這一目的:

public static void main(String[] args) { 
    Icon icon = FileSystemView.getFileSystemView() 
     .getSystemIcon(new File("C:\\Windows\\regedit.exe")); 
    JFrame frame = new JFrame(); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.add(new JLabel(icon)); 
    frame.pack(); 
    frame.setVisible(true); 
} 
+0

謝謝你的回答! – mattbdean

相關問題