JFileChooser 下如何修改它自己的图标(就是title的java默认图标)我想换成自己的图片

解决方案 »

  1.   

    我的父窗体已经设置了Image image =Toolkit.getDefaultToolkit().getImage("./res/Logo.jpg");
    loginjframe.setIconImage(image);但是我有个写入文件的操作按钮
    点完以后是就 调用 JFileChooser 它了 
    但是它的图标还是java 的
      

  2.   

    先看看你的父窗体的图标设置上去了没有,还有留意下loginjframe是否是模态的。
    在loginjframe布什模态的情况下,只要./res/Logo.jpg设置上去了,下面的方法显示的JFileChooser是一定新图标的。
    fc.showOpenDialog(loginjframe);
    chooser.showSaveDialog(loginjframe);
      

  3.   

    loginjframe 是一个主画面在这里有个写入文件的按钮点击以后 进入下一个类 A 中 
    在A 中 我调用了JFileChooser 方法是 
    JFileChooser c = new LicenseFileChooser();我想你说的父窗体 这时候已经不在了吧
    不知道我说的对不对
      

  4.   


    import java.awt.BorderLayout;
    import java.awt.Graphics;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;import javax.imageio.ImageIO;
    import javax.swing.JButton;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;public class ShowImage extends JPanel implements ActionListener { private static final long serialVersionUID = 1L;

    static JFrame frame = null; private JButton open;
    private String filePath;
    private BufferedImage image;
    private static ShowImage show; public static ShowImage singleton() {
    if (show == null)
    show = new ShowImage();
    return show;
    } private ShowImage() {
    open = new JButton("open");
    setLayout(new BorderLayout());
    add(open, BorderLayout.SOUTH);
    open.addActionListener(this); } private void init(String str) {
    FileInputStream input;
    try {
    input = new FileInputStream(str);
    image = ImageIO.read(input);
    repaint();
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } } private static void createAndShowGUI() {
    // Create and set up the window.
    frame = new JFrame("JFileChooserDemo");
                    //换成自己的图标路径
    frame.setIconImage(Toolkit.getDefaultToolkit().createImage("E:/工作/My Documents/My Pictures/logo.png"));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // Add content to the window.
    frame.add(singleton());
    frame.setSize(300, 400);
    frame.setTitle("QRCode"); // Display the window.
    frame.setVisible(true);
    } public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    // Turn off metal's use of bold fonts
    UIManager.put("swing.boldMetal", Boolean.FALSE);
    createAndShowGUI();
    }
    });
    } @Override
    public void paint(Graphics g) {
    // TODO Auto-generated method stub
    g.drawImage(image, 26, 26, this);
    } @Override
    public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    if (e.getSource() == open) {
    JFileChooser dialog = new JFileChooser();
    int result = dialog.showOpenDialog(frame);
    if (result == JFileChooser.APPROVE_OPTION) {
    File file = dialog.getSelectedFile();
    filePath = file.getPath();
    init(filePath);
    }
    }
    }
    }
      

  5.   

    看一下你的主界面frame的图标,只要设置那个了frame的图标,JFileChooser弹出窗口的那个图标就会自动改了
      

  6.   


    在main函数所在的class,假设为App,利用单例模式,让外部可以获取loginjframe。
    private static App instance = null;
    private static JFrame loginjframe;
    public class App
    {
    public static App getApplication()
    {
    if (instance == null)
    {
    instance = new App();
    }
    return instance;
    } public JFrame getMainFrame()
    {
    return loginjframe;
    }
    public static void main(String[] args)
    {
    loginjframe = new JFrame();
    ...
    }
    }外部获取父窗体:App.getApplication().getMainFrame();
    给JDialog、JWindow设置父窗体,对于其图标、相对位置都是非常好的。
      

  7.   

    关键我这个JFileChooser 放的位置有点特殊我是在主jframe 中加了一个jpanel  在jpanel 中加了一个jbuttion调用了一个A类 在A类里面 我用到了JFileChooser 以上大家说的可能在主Jframe 中是好用的
    但是经过这么多了
    我只想问问有没有什么方法直接给JFileChooser  的图标换成自己的呢?
      

  8.   

    JFileChooser  这个东东 都不会用吗?
      

  9.   

    不管主jframe 在哪里,总之在JFileChooser的showDialog之前新声明一个jframe,
    不要设置newframe为visible,然后showDialog(newframe)JFrame j = new JFrame();
    j.setLocation(frame.getLocation());
    j.setIconImage(Toolkit.getDefaultToolkit().createImage("E:/my1.png"));
    int result = dialog.showOpenDialog(j);这样弹出的对话框的位置可能不会与主jframe位置同步...
      

  10.   

    public class Test extends JFileChooser {    public Test() {
        }    protected JDialog createDialog(Component parent) throws HeadlessException {
            JDialog dialog = super.createDialog(parent);
            dialog.setLocation(500, 200);
            dialog.setVisible(true);
            return dialog;
        }    public static void main(String[] args) throws IOException {
            Test test = new Test();
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setIconImage(ImageIO.read(new File("D:\\212217668.gif")));
            test.createDialog(frame);
        }
    }