if (dot != -1) {
String extension = file_name.substring(dot);
Program program = Program.findProgram(extension);
if (program != null){
String typeString = program.getName();
ImageData imageData = program.getImageData();
if (imageData != null){
Image iconImage = new Image(null, imageData, imageData.getTransparencyMask());
}
}
}
这么获取的图标只是系统识别程序和文件的,请问系统文件夹图标、系统不识别文件的图标如何获取?谢谢大侠!
5 个解决方案
#1
自己設置一個呗
#2
设置一个倒是可以,但我还是想知道如何获取系统提供的~
#3
Program 是什么类 我在api里查不到
#4
给你写了一个提取图标的,你可以参考看看
用法如下,输入文件的扩展名(不加),然后点按钮就能看到效果
import java.io.File;
import java.io.IOException;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.BoxLayout;
import javax.swing.filechooser.FileSystemView;
import sun.awt.shell.ShellFolder;
public class FileIconExtractor extends JFrame implements ActionListener{
private JButton getIconBtn = new JButton("get Icon");
private JPanel iconPanel = new JPanel();
private JTextField extField = new JTextField();
private JLabel smallIconLabel = new JLabel("small Icon here");
private JLabel bigIconLabel = new JLabel("big Icon here");
public FileIconExtractor() {
this.setSize(200, 150);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
getIconBtn.setActionCommand("GETICON");
getIconBtn.addActionListener(this);
iconPanel.setLayout(new BoxLayout(iconPanel, BoxLayout.Y_AXIS));
iconPanel.add(smallIconLabel);
iconPanel.add(bigIconLabel);
this.add(extField, BorderLayout.NORTH);
this.add(iconPanel, BorderLayout.CENTER);
this.add(getIconBtn, BorderLayout.SOUTH);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("GETICON")) {
String ext = extField.getText();
File file;
try
{
file = File.createTempFile("icon", "." + ext);
FileSystemView view = FileSystemView.getFileSystemView();
Icon smallIcon = view.getSystemIcon(file);
ShellFolder shellFolder = ShellFolder.getShellFolder(file);
Icon bigIcon = new ImageIcon(shellFolder.getIcon(true));
setIconLabel(smallIcon, bigIcon);
file.delete();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
private void setIconLabel(Icon smallIcon, Icon bigIcon) {
smallIconLabel.setIcon(smallIcon);
bigIconLabel.setIcon(bigIcon);
}
public static void main(String[] args) {
FileIconExtractor fie = new FileIconExtractor();
}
}
用法如下,输入文件的扩展名(不加),然后点按钮就能看到效果
import java.io.File;
import java.io.IOException;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.BoxLayout;
import javax.swing.filechooser.FileSystemView;
import sun.awt.shell.ShellFolder;
public class FileIconExtractor extends JFrame implements ActionListener{
private JButton getIconBtn = new JButton("get Icon");
private JPanel iconPanel = new JPanel();
private JTextField extField = new JTextField();
private JLabel smallIconLabel = new JLabel("small Icon here");
private JLabel bigIconLabel = new JLabel("big Icon here");
public FileIconExtractor() {
this.setSize(200, 150);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
getIconBtn.setActionCommand("GETICON");
getIconBtn.addActionListener(this);
iconPanel.setLayout(new BoxLayout(iconPanel, BoxLayout.Y_AXIS));
iconPanel.add(smallIconLabel);
iconPanel.add(bigIconLabel);
this.add(extField, BorderLayout.NORTH);
this.add(iconPanel, BorderLayout.CENTER);
this.add(getIconBtn, BorderLayout.SOUTH);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("GETICON")) {
String ext = extField.getText();
File file;
try
{
file = File.createTempFile("icon", "." + ext);
FileSystemView view = FileSystemView.getFileSystemView();
Icon smallIcon = view.getSystemIcon(file);
ShellFolder shellFolder = ShellFolder.getShellFolder(file);
Icon bigIcon = new ImageIcon(shellFolder.getIcon(true));
setIconLabel(smallIcon, bigIcon);
file.delete();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
private void setIconLabel(Icon smallIcon, Icon bigIcon) {
smallIconLabel.setIcon(smallIcon);
bigIconLabel.setIcon(bigIcon);
}
public static void main(String[] args) {
FileIconExtractor fie = new FileIconExtractor();
}
}
#5
回复joejoe1991
org.eclipse.swt.program.Program
回复boby
谢谢你!不过这是从JSwing里边获取,我不想在SWT/JFace中掺上JSwing的东西~难道真的没有办法了?
org.eclipse.swt.program.Program
回复boby
谢谢你!不过这是从JSwing里边获取,我不想在SWT/JFace中掺上JSwing的东西~难道真的没有办法了?
#1
自己設置一個呗
#2
设置一个倒是可以,但我还是想知道如何获取系统提供的~
#3
Program 是什么类 我在api里查不到
#4
给你写了一个提取图标的,你可以参考看看
用法如下,输入文件的扩展名(不加),然后点按钮就能看到效果
import java.io.File;
import java.io.IOException;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.BoxLayout;
import javax.swing.filechooser.FileSystemView;
import sun.awt.shell.ShellFolder;
public class FileIconExtractor extends JFrame implements ActionListener{
private JButton getIconBtn = new JButton("get Icon");
private JPanel iconPanel = new JPanel();
private JTextField extField = new JTextField();
private JLabel smallIconLabel = new JLabel("small Icon here");
private JLabel bigIconLabel = new JLabel("big Icon here");
public FileIconExtractor() {
this.setSize(200, 150);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
getIconBtn.setActionCommand("GETICON");
getIconBtn.addActionListener(this);
iconPanel.setLayout(new BoxLayout(iconPanel, BoxLayout.Y_AXIS));
iconPanel.add(smallIconLabel);
iconPanel.add(bigIconLabel);
this.add(extField, BorderLayout.NORTH);
this.add(iconPanel, BorderLayout.CENTER);
this.add(getIconBtn, BorderLayout.SOUTH);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("GETICON")) {
String ext = extField.getText();
File file;
try
{
file = File.createTempFile("icon", "." + ext);
FileSystemView view = FileSystemView.getFileSystemView();
Icon smallIcon = view.getSystemIcon(file);
ShellFolder shellFolder = ShellFolder.getShellFolder(file);
Icon bigIcon = new ImageIcon(shellFolder.getIcon(true));
setIconLabel(smallIcon, bigIcon);
file.delete();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
private void setIconLabel(Icon smallIcon, Icon bigIcon) {
smallIconLabel.setIcon(smallIcon);
bigIconLabel.setIcon(bigIcon);
}
public static void main(String[] args) {
FileIconExtractor fie = new FileIconExtractor();
}
}
用法如下,输入文件的扩展名(不加),然后点按钮就能看到效果
import java.io.File;
import java.io.IOException;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.BoxLayout;
import javax.swing.filechooser.FileSystemView;
import sun.awt.shell.ShellFolder;
public class FileIconExtractor extends JFrame implements ActionListener{
private JButton getIconBtn = new JButton("get Icon");
private JPanel iconPanel = new JPanel();
private JTextField extField = new JTextField();
private JLabel smallIconLabel = new JLabel("small Icon here");
private JLabel bigIconLabel = new JLabel("big Icon here");
public FileIconExtractor() {
this.setSize(200, 150);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
getIconBtn.setActionCommand("GETICON");
getIconBtn.addActionListener(this);
iconPanel.setLayout(new BoxLayout(iconPanel, BoxLayout.Y_AXIS));
iconPanel.add(smallIconLabel);
iconPanel.add(bigIconLabel);
this.add(extField, BorderLayout.NORTH);
this.add(iconPanel, BorderLayout.CENTER);
this.add(getIconBtn, BorderLayout.SOUTH);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("GETICON")) {
String ext = extField.getText();
File file;
try
{
file = File.createTempFile("icon", "." + ext);
FileSystemView view = FileSystemView.getFileSystemView();
Icon smallIcon = view.getSystemIcon(file);
ShellFolder shellFolder = ShellFolder.getShellFolder(file);
Icon bigIcon = new ImageIcon(shellFolder.getIcon(true));
setIconLabel(smallIcon, bigIcon);
file.delete();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
private void setIconLabel(Icon smallIcon, Icon bigIcon) {
smallIconLabel.setIcon(smallIcon);
bigIconLabel.setIcon(bigIcon);
}
public static void main(String[] args) {
FileIconExtractor fie = new FileIconExtractor();
}
}
#5
回复joejoe1991
org.eclipse.swt.program.Program
回复boby
谢谢你!不过这是从JSwing里边获取,我不想在SWT/JFace中掺上JSwing的东西~难道真的没有办法了?
org.eclipse.swt.program.Program
回复boby
谢谢你!不过这是从JSwing里边获取,我不想在SWT/JFace中掺上JSwing的东西~难道真的没有办法了?