AWT FileDialog 类
简介
FileDialog 控件表示一个对话框窗口,用户可以从中选择文件。
类声明
以下是 java.awt.FileDialog 类的声明:
public class FileDialog extends Dialog
字段
以下是 java.awt.Image 类的字段:
static int LOAD -- 此常量值表示文件对话框窗口的目的是定位要读取的文件。
static int SAVE -- 此常量值表示文件对话框窗口的目的是定位要读取的文件。要写入的内容。
类构造函数
S.N. | 构造函数 &描述 |
---|---|
1 | FileDialog(Dialog parent) 创建用于加载文件的文件对话框。 |
2 | FileDialog(Dialog parent, String title) 创建具有指定标题的文件对话框窗口,用于加载文件。 |
3 | FileDialog(Dialog parent, String title, int mode) 创建具有指定标题的文件对话框窗口,用于加载或保存文件。 |
4 | FileDialog(Frame parent) 创建用于加载文件的文件对话框。 |
5 | FileDialog(Frame parent, String title) 创建一个具有指定标题的文件对话框窗口,用于加载文件。 |
6 | FileDialog(Frame parent, String title, int mode) 创建一个具有指定标题的文件对话框窗口,用于加载或保存文件。 |
类方法
S.N. | 方法 &描述 |
---|---|
1 | void addNotify() 创建文件对话框的对等体。 |
2 | String getDirectory() 获取此文件对话框的目录。 |
3 | String getFile() 获取此文件对话框的选定文件。 |
4 | FilenameFilter getFilenameFilter() 确定此文件对话框的文件名过滤器。 |
5 | int getMode() 指示此文件对话框是用于从文件加载还是用于保存到文件。 |
6 | protected String paramString() 返回表示此 FileDialog 窗口状态的字符串。 |
7 | void setDirectory(String dir) 将此文件对话框窗口的目录设置为指定目录。 |
8 | void setFile(String file) 将此文件对话框窗口的选定文件设置为指定文件。 |
9 | void setFilenameFilter(FilenameFilter filter) 将此文件对话框窗口的文件名过滤器设置为指定的过滤器。 |
10 | void setMode(int mode) 设置文件对话框的模式。 |
继承的方法
此类从以下类继承方法:
java.awt.Dialog
java.awt.Window
java.awt.Component
java.lang.Object
FileDialog 示例
使用您选择的任何编辑器在 D:/ > AWT > com > tutorialspoint > gui > 中创建以下 Java 程序
AwtControlDemo.javapackage com.tutorialspoint.gui; import java.awt.*; import java.awt.event.*; public class AwtControlDemo { private Frame mainFrame; private Label headerLabel; private Label statusLabel; private Panel controlPanel; public AwtControlDemo(){ prepareGUI(); } public static void main(String[] args){ AwtControlDemo awtControlDemo = new AwtControlDemo(); awtControlDemo.showFileDialogDemo(); } private void prepareGUI(){ mainFrame = new Frame("Java AWT Examples"); mainFrame.setSize(400,400); mainFrame.setLayout(new GridLayout(3, 1)); mainFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent windowEvent){ System.exit(0); } }); headerLabel = new Label(); headerLabel.setAlignment(Label.CENTER); statusLabel = new Label(); statusLabel.setAlignment(Label.CENTER); statusLabel.setSize(350,100); controlPanel = new Panel(); controlPanel.setLayout(new FlowLayout()); mainFrame.add(headerLabel); mainFrame.add(controlPanel); mainFrame.add(statusLabel); mainFrame.setVisible(true); } private void showFileDialogDemo(){ headerLabel.setText("Control in action: FileDialog"); final FileDialog fileDialog = new FileDialog(mainFrame,"Select file"); Button showFileDialogButton = new Button("Open File"); showFileDialogButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { fileDialog.setVisible(true); statusLabel.setText("File Selected :" + fileDialog.getDirectory() + fileDialog.getFile()); } }); controlPanel.add(showFileDialogButton); mainFrame.setVisible(true); } }
使用命令提示符编译程序。转到 D:/ > AWT 并键入以下命令。
D:\AWT>javac com utorialspoint\gui\AwtControlDemo.java
如果没有错误,则表示编译成功。使用以下命令运行程序。
D:\AWT>java com.tutorialspoint.gui.AwtControlDemo
验证以下输出
