簡體   English   中英

如何在JFileChooser中顯示文件的默認系統圖標?

[英]How to display default system icon for files in JFileChooser?

如何在JFileChooser顯示文件的默認系統圖標? JFileChooser中的文件圖標應該與桌面和資源管理器上顯示的圖標相同?

例如,NetBeans圖標在JFileChooser中的顯示方式與桌面上顯示的不同!

這個怎么做?

我們可以使用FileSystemView類並通過調用其中的getFileSystemView()靜態方法獲取它的對象,然后使用getSystemIcon()方法獲取File對象並返回它的圖標。

FileSystemViewFileView類存在於javax.swing.filechooser包中。 File類位於java.io包中。

注意: FileSystemView不擴展FileView 因此你不能在jf.setFileView()使用FileSystemView對象

JFileChooser jf=new JFileChooser();
jf.setFileView(new MyFileView());
jf.showOpenDialog(this);

class MyFileView extends FileView
{
      public Icon getIcon(File f)
      {
      FileSystemView view=FileSystemView.getFileSystemView();
            return view.getSystemIcon(f);
      }
}

this代表當前幀。 假設編寫此代碼的類是JFrame子類

或者以一種簡單的方式,

jf.setFileView(new FileView(){
            public Icon getIcon(File f)
            {
                return FileSystemView.getFileSystemView().getSystemIcon(f);
            }
        });

@JavaTechnical顯示的方式是一種方式。 這是另一種(更簡單)的方式。 將GUI(或至少文件選擇器)設置為本機PLAF。 例如

文件選擇器與原生PLAF

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class FileChooserIcons {

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(
                            UIManager.getSystemLookAndFeelClassName());
                } catch(Exception e) {
                    e.printStackTrace();
                }
                // the GUI as seen by the user (without frame)
                JPanel gui = new JPanel(new BorderLayout());
                gui.setBorder(new EmptyBorder(20, 30, 20, 30));

                JButton browse = new JButton("Show File Chooser");
                final JFrame f = new JFrame("File Chooser");
                ActionListener showChooser = new ActionListener() {

                    JFileChooser jfc = new JFileChooser();

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        jfc.showOpenDialog(f);
                    }
                };
                browse.addActionListener(showChooser);
                gui.add(browse);

                f.add(gui);
                // Ensures JVM closes after frame(s) closed and
                // all non-daemon threads are finished
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                // See http://stackoverflow.com/a/7143398/418556 for demo.
                f.setLocationByPlatform(true);

                // ensures the frame is the minimum size it needs to be
                // in order display the components within it
                f.pack();
                // should be done last, to avoid flickering, moving,
                // resizing artifacts.
                f.setVisible(true);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }
}

當然,如果你感覺很勇敢,你可以創建一個自定義文件選擇器,從文件瀏覽器GUI開始

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM