
- Swing 教程
- Swing - 首頁
- Swing - 概述
- Swing - 環境
- Swing - 控制元件
- Swing - 事件處理
- Swing - 事件類
- Swing - 事件監聽器
- Swing - 事件介面卡
- Swing - 佈局
- Swing - 選單
- Swing - 容器
- Swing 有用資源
- Swing - 快速指南
- Swing - 有用資源
- Swing - 討論
Swing - JFileChooser 類
介紹
JFileChooser 類是一個元件,它為使用者提供了一種簡單的機制來選擇檔案。
類宣告
以下是javax.swing.JFileChooser類的宣告:
public class JFileChooser extends JComponent implements Accessible
欄位
以下是javax.swing.JFileChooser類的欄位:
static String ACCEPT_ALL_FILE_FILTER_USED_CHANGED_PROPERTY - 指示是否使用 AcceptAllFileFilter。
protected AccessibleContext accessibleContext
static String ACCESSORY_CHANGED_PROPERTY - 表示正在使用不同的附件元件(例如,用於預覽檔案)。
static String APPROVE_BUTTON_MNEMONIC_CHANGED_PROPERTY - 指示確認(是,確定)按鈕助記符的更改。
static String APPROVE_BUTTON_TEXT_CHANGED_PROPERTY - 指示確認(是,確定)按鈕文字的更改。
static String APPROVE_BUTTON_TOOL_TIP_TEXT_CHANGED_PROPERTY - 指示確認(是,確定)按鈕工具提示文字的更改。
static int APPROVE_OPTION - 如果選擇確認(是,確定),則返回此值。
static String APPROVE_SELECTION - 指令,用於確認當前選擇(與按下“是”或“確定”相同)。
static int CANCEL_OPTION - 如果選擇取消,則返回此值。
static String CANCEL_SELECTION - 指令,用於取消當前選擇。
static String CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY - 指示使用者可以選擇的檔案過濾器列表的更改。
static String CONTROL_BUTTONS_ARE_SHOWN_CHANGED_PROPERTY - 指令,用於顯示控制按鈕。
static int CUSTOM_DIALOG - 型別值,指示 JFileChooser 支援開發人員指定的指定檔案操作。
static String DIALOG_TITLE_CHANGED_PROPERTY - 指示對話方塊標題的更改。
static String DIALOG_TYPE_CHANGED_PROPERTY - 指示顯示的檔案型別的更改(僅檔案、僅目錄或檔案和目錄)。
static int DIRECTORIES_ONLY - 指令,用於僅顯示目錄。
static String DIRECTORY_CHANGED_PROPERTY - 指示使用者的目錄更改。
static int ERROR_OPTION - 如果發生錯誤,則返回此值。
static String FILE_FILTER_CHANGED_PROPERTY - 指示使用者更改了要顯示的檔案型別。
static String FILE_HIDING_CHANGED_PROPERTY - 指示顯示隱藏檔案屬性的更改。
static String FILE_SELECTION_MODE_CHANGED_PROPERTY - 指示選擇型別的更改(單個、多個等)。
static String FILE_SYSTEM_VIEW_CHANGED_PROPERTY - 表示正在使用不同的物件來查詢系統上可用的驅動器。
static String FILE_VIEW_CHANGED_PROPERTY - 表示正在使用不同的物件來檢索檔案資訊。
static int FILES_AND_DIRECTORIES - 指令,用於顯示檔案和目錄。
static int FILES_ONLY - 指令,用於僅顯示檔案。
static String MULTI_SELECTION_ENABLED_CHANGED_PROPERTY - 啟用多檔案選擇。
static int OPEN_DIALOG - 型別值,指示 JFileChooser 支援“開啟”檔案操作。
static int SAVE_DIALOG - 型別值,指示 JFileChooser 支援“儲存”檔案操作。
static String SELECTED_FILE_CHANGED_PROPERTY - 指示使用者單檔案選擇的更改。
static String SELECTED_FILES_CHANGED_PROPERTY - 指示使用者多檔案選擇的更改。
類建構函式
序號 | 建構函式和描述 |
---|---|
1 |
JFileChooser() 構造一個指向使用者預設目錄的 JFileChooser。 |
2 |
JFileChooser(File currentDirectory) 使用給定的 File 作為路徑構造一個 JFileChooser。 |
3 |
JFileChooser(File currentDirectory, FileSystemView fsv) 使用給定的當前目錄和 FileSystemView 構造一個 JFileChooser。 |
4 |
JFileChooser(FileSystemView fsv) 使用給定的 FileSystemView 構造一個 JFileChooser。 |
5 |
JFileChooser(String currentDirectoryPath) 使用給定的路徑構造一個 JFileChooser。 |
6 |
JFileChooser(String currentDirectoryPath, FileSystemView fsv) 使用給定的當前目錄路徑和 FileSystemView 構造一個 JFileChooser。 |
類方法
序號 | 方法和描述 |
---|---|
1 |
boolean accept(File f) 如果應顯示檔案,則返回 true。 |
2 |
void addActionListener(ActionListener l) 向檔案選擇器新增 ActionListener。 |
3 |
void addChoosableFileFilter(FileFilter filter) 向用戶可選檔案過濾器列表新增過濾器。 |
4 |
void approveSelection() 當用戶點選確認按鈕(預設情況下標記為“開啟”或“儲存”)時,由 UI 呼叫。 |
5 |
void cancelSelection() 當用戶選擇“取消”按鈕時,由 UI 呼叫。 |
6 |
void changeToParentDirectory() 將目錄設定為當前目錄的父目錄。 |
7 |
protected JDialog createDialog(Component parent) 建立一個新的 JDialog 包裝器,居中於父級在其父級的框架中。 |
8 |
void ensureFileIsVisible(File f) 確保指定的檔案可見且未隱藏。 |
9 |
protected void fireActionPerformed(String command) 通知所有已註冊對此事件型別感興趣的監聽器。 |
10 |
FileFilter getAcceptAllFileFilter() 返回 AcceptAll 檔案過濾器。 |
11 |
AccessibleContext getAccessibleContext() 獲取與此 JFileChooser 關聯的 AccessibleContext。 |
12 |
JComponent getAccessory() 返回附件元件。 |
13 |
ActionListener[]getActionListeners() 返回在此檔案選擇器上註冊的所有操作偵聽器的陣列。 |
14 |
int getApproveButtonMnemonic() 返回確認按鈕的助記符。 |
15 |
String getApproveButtonText() 返回 FileChooserUI 中 ApproveButton 中使用的文字。 |
16 |
String getApproveButtonToolTipText() 返回 ApproveButton 中使用的工具提示文字。 |
17 |
FileFilter[] getChoosableFileFilters() 獲取使用者可選檔案過濾器的列表。 |
18 |
boolean getControlButtonsAreShown() 返回 controlButtonsAreShown 屬性的值。 |
19 |
File getCurrentDirectory() 返回當前目錄。 |
20 |
String getDescription(File f) 返回檔案描述。 |
21 |
String getDialogTitle() 獲取 JFileChooser 標題欄中的字串。 |
22 |
int getDialogType() 返回此對話方塊的型別。 |
23 |
boolean getDragEnabled() 獲取 dragEnabled 屬性的值。 |
24 |
FileFilter getFileFilter() 返回當前選定的檔案過濾器。 |
25 |
int getFileSelectionMode() 返回當前檔案選擇模式。 |
26 |
FileSystemView getFileSystemView() 返回檔案系統檢視。 |
27 |
FileView getFileView() 返回當前檔案檢視。 |
28 |
Icon getIcon(File f) 返回此檔案或檔案型別的圖示,具體取決於系統。 |
29 |
String getName(File f) 返回檔名。 |
30 |
File getSelectedFile() 返回選定的檔案。 |
31 |
File[] getSelectedFiles() 如果檔案選擇器設定為允許多選,則返回選定檔案的列表。 |
32 |
String getTypeDescription(File f) 返回檔案型別。 |
33 |
FileChooserUI getUI() 獲取實現此元件的 L&F 的 UI 物件。 |
34 |
String getUIClassID() 返回一個字串,該字串指定呈現此元件的 L&F 類的名稱。 |
35 |
boolean isAcceptAllFileFilterUsed() 返回是否使用 AcceptAll FileFilter。 |
36 |
boolean isDirectorySelectionEnabled() 便捷呼叫,根據當前檔案選擇模式確定目錄是否可選。 |
37 |
boolean isFileHidingEnabled() 如果隱藏檔案未在檔案選擇器中顯示,則返回 true;否則返回 false。 |
38 |
boolean isFileSelectionEnabled() 便捷呼叫,根據當前檔案選擇模式確定檔案是否可選。 |
39 |
boolean isMultiSelectionEnabled() 如果可以選擇多個檔案,則返回 true。 |
40 |
boolean isTraversable(File f) 如果可以訪問檔案(目錄),則返回 true。 |
41 |
protected String paramString() 返回此 JFileChooser 的字串表示形式。 |
42 |
void removeActionListener(ActionListener l) 從檔案選擇器中刪除 ActionListener。 |
43 |
boolean removeChoosableFileFilter(FileFilter f) 從使用者可選檔案過濾器列表中刪除過濾器。 |
44 |
void rescanCurrentDirectory() 告訴 UI 從當前目錄重新掃描其檔案列表。 |
45 |
void resetChoosableFileFilters() 將可選檔案過濾器列表重置為其初始狀態。 |
46 |
void setAcceptAllFileFilterUsed(boolean b) 確定是否將 AcceptAll FileFilter 用作可選過濾器列表中的可用選項。 |
47 |
void setAccessory(JComponent newAccessory) 設定附件元件。 |
48 |
void setApproveButtonMnemonic(char mnemonic) 使用字元設定確認按鈕的助記符。 |
49 |
void setApproveButtonMnemonic(int mnemonic) 使用數字鍵碼設定確認按鈕的助記符。 |
50 |
void setApproveButtonText(String approveButtonText) 設定 FileChooserUI 中 ApproveButton 中使用的文字。 |
51 |
void setApproveButtonToolTipText(String toolTipText) 設定 ApproveButton 中使用的工具提示文字。 |
52 |
void setControlButtonsAreShown(boolean b) 設定指示是否在檔案選擇器中顯示確認和取消按鈕的屬性。 |
53 |
void setCurrentDirectory(File dir) 設定當前目錄。 |
54 |
void setDialogTitle(String dialogTitle) 設定 JFileChooser 視窗標題欄中的字串。 |
55 |
void setDialogType(int dialogType) 設定此對話方塊的型別。 |
56 |
void setDragEnabled(boolean b) 設定 dragEnabled 屬性,此屬性必須為 true 才能在此元件上啟用自動拖動處理(拖放的第一部分)。 |
57 |
void setFileFilter(FileFilter filter) 設定當前檔案過濾器。 |
58 |
void setFileHidingEnabled(boolean b) 開啟或關閉檔案隱藏。 |
59 |
void setFileSelectionMode(int mode) 設定 JFileChooser 以允許使用者僅選擇檔案、僅選擇目錄或同時選擇檔案和目錄。 |
60 |
void setFileSystemView(FileSystemView fsv) 設定 JFileChooser 用於訪問和建立檔案系統資源的檔案系統檢視,例如查詢軟盤驅動器並獲取根驅動器的列表。 |
61 |
void setFileView(FileView fileView) 設定用於檢索 UI 資訊的檔案檢視,例如表示檔案或檔案型別描述的圖示。 |
62 |
void setMultiSelectionEnabled(boolean b) 設定檔案選擇器以允許多檔案選擇。 |
63 |
void setSelectedFile(File file) 設定選定的檔案。 |
64 |
void setSelectedFiles(File[] selectedFiles) 如果檔案選擇器設定為允許多選,則設定選定檔案的列表。 |
65 |
protected void setup(FileSystemView view) 執行常見的建構函式初始化和設定。 |
66 |
int showDialog(Component parent, String approveButtonText) 彈出具有自定義確認按鈕的自定義檔案選擇器對話方塊。 |
67 |
int showOpenDialog(Component parent) 彈出“開啟檔案”檔案選擇器對話方塊。 |
68 |
int showSaveDialog(Component parent) 彈出“儲存檔案”檔案選擇器對話方塊。 |
69 |
void updateUI() 將 UI 屬性重置為當前外觀和風格的值。 |
繼承的方法
此類繼承自以下類的方法:
- javax.swing.JComponent
- java.awt.Container
- java.awt.Component
- java.lang.Object
JFileChooser 示例
使用您選擇的任何編輯器建立以下 Java 程式,例如在D:/ > SWING > com > tutorialspoint > gui >中。
SwingControlDemo.java
package com.tutorialspoint.gui; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SwingControlDemo { private JFrame mainFrame; private JLabel headerLabel; private JLabel statusLabel; private JPanel controlPanel; public SwingControlDemo(){ prepareGUI(); } public static void main(String[] args){ SwingControlDemo swingControlDemo = new SwingControlDemo(); swingControlDemo.showFileChooserDemo(); } private void prepareGUI(){ mainFrame = new JFrame("Java Swing 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 JLabel("", JLabel.CENTER); statusLabel = new JLabel("",JLabel.CENTER); statusLabel.setSize(350,100); controlPanel = new JPanel(); controlPanel.setLayout(new FlowLayout()); mainFrame.add(headerLabel); mainFrame.add(controlPanel); mainFrame.add(statusLabel); mainFrame.setVisible(true); } private void showFileChooserDemo(){ headerLabel.setText("Control in action: JFileChooser"); final JFileChooser fileDialog = new JFileChooser(); JButton showFileDialogButton = new JButton("Open File"); showFileDialogButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int returnVal = fileDialog.showOpenDialog(mainFrame); if (returnVal == JFileChooser.APPROVE_OPTION) { java.io.File file = fileDialog.getSelectedFile(); statusLabel.setText("File Selected :" + file.getName()); } else { statusLabel.setText("Open command cancelled by user." ); } } }); controlPanel.add(showFileDialogButton); mainFrame.setVisible(true); } }
使用命令提示符編譯程式。轉到D:/ > SWING並鍵入以下命令。
D:\SWING>javac com\tutorialspoint\gui\SwingControlDemo.java
如果未發生錯誤,則表示編譯成功。請使用以下命令執行程式。
D:\SWING>java com.tutorialspoint.gui.SwingControlDemo
驗證以下輸出。
