【问题标题】:How change the image of open Dialog box and how to customize Dialog box in swing?如何改变打开对话框的图像以及如何在swing中自定义对话框?
【发布时间】:2014-08-27 07:11:18
【问题描述】:

当我打开显示对话框的文件时,我需要更改 Java 图像并添加我自己的图像。如何自定义对话框?

例如,我需要在对话框中添加编码以及如何将不同类型的文件添加到文件类型下拉框中。例如,我将textjavahtml 添加到Files of type 框。

这是我的代码,

FileDialog fd = new FileDialog(OpenExample.this, "Select File", FileDialog.LOAD);
fd.setVisible(true);

【问题讨论】:

  • Swing 的文件选择器是JFileChooserFileDialog 是纯 AWT。有关使用 Swing 文件选择器的更多详细信息和示例,请参阅How to Use File Choosers
  • 使用所需设置将 JFileChooser 放入 JDialog
  • 谢谢您的回复,通过filedialog是不可能的。如何更改 java 主题图像。@mKorbel 和 @AndrewThompson
  • 看起来适合 FileDialog.@AndrewThompson
  • 如何更改图像和自定义 FileChooser 或 FileDialog。你能提供这个例子吗?请@AndrewThompson

标签: java swing jfilechooser openfiledialog


【解决方案1】:

要为文件选择器或对话框提供图标,请为父框架设置图标。

import java.awt.image.BufferedImage;
import javax.swing.*;

public class FileChooserIcon {

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    // see nice icons in chooser!
                    UIManager.setLookAndFeel(
                            UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {}
                JLabel ui = new JLabel("Big Space");
                ui.setBorder(new javax.swing.EmptyBorder(40, 200, 40, 200));

                JFrame f = new JFrame("Show file chooser icon");
                f.setIconImage(new BufferedImage(
                        20, 20, BufferedImage.TYPE_INT_RGB));
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setContentPane(ui);
                f.pack();
                f.setLocationByPlatform(true);
                f.setVisible(true);

                JFileChooser jfc = new JFileChooser();
                jfc.showOpenDialog(f); // use frame icon!
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

.. 如何将不同类型的文件添加到文件类型下拉框中?例如:将textjavahtml添加到Files of type框。

请参阅How to Use File Choosers: FileChooserDemo2,它为Just Images 提供文件过滤器..

【讨论】:

  • 正是我需要这个。非常感谢。@andrewThompson
猜你喜欢
  • 2014-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多