Java 中的 JFileChooser 示例
-
使用
JFileChooser開啟檔案並在 Java 輸出中顯示其名稱和路徑 -
在 Java 中使用
JFileChooser開啟影象並將其顯示的JFrame元件中 -
在 Java 中使用
JFileChooser開啟文字檔案並的JFrame元件中顯示它
JFileChooser 為我們提供了一個視窗或提示,我們可以使用它來選擇檔案或資料夾。它是 Swing 包的一部分。
我們將在以下部分中看到 JFileChooser 的三個示例。
使用 JFileChooser 開啟檔案並在 Java 輸出中顯示其名稱和路徑
在第一個示例中,我們建立了一個 JFileChooser 類物件。如果我們想在檔案選擇器提示開啟時開啟特定目錄,我們可以將路徑傳遞給 JFileChooser 的建構函式。
現在我們呼叫 showOpenDialog() 函式,該函式接受一個引數,該引數是檔案選擇器的 GUI 的父級;我們在其中傳遞 null,因為我們只想要提示。
根據使用者輸入,對 showOpenDialog() 的呼叫會返回一個附加到靜態欄位的 int。
如果使用者在對話方塊 showOpenDialog() 上單擊確定,它會返回一個表示欄位 APPROVE_OPTION 的 int,當它返回時,我們會在一個條件中檢查它。
在條件語句中,我們使用 getSelectedFile() 函式獲取我們從提示中選擇的檔案,該函式為我們提供 File 物件。現在我們可以獲取檔案的名稱和路徑。
import java.io.File;
import javax.swing.*;
public class JavaExample {
public static void main(String[] args) {
JFileChooser jFileChooser = new JFileChooser();
int checkInput = jFileChooser.showOpenDialog(null);
if (checkInput == JFileChooser.APPROVE_OPTION) {
File openedFile = jFileChooser.getSelectedFile();
System.out.println("File Name: " + openedFile.getName());
System.out.println("File Location: " + openedFile.getAbsolutePath());
}
}
}
輸出:
File Name: New Text Document.txt
File Location: C:\Users\User\Documents\New Text Document.txt
在 Java 中使用 JFileChooser 開啟影象並將其顯示的 JFrame 元件中
我們可以使用 JFileChooser 類選擇和開啟不同的檔案,但我們必須手動處理這些檔案。
在示例中,我們使用 JFileChooser 提示符選擇並開啟一個影象,然後在 JFrame 元件中顯示該影象。
我們建立了一個 JFrame 和兩個元件:JButton 和 JLabel。我們通過建立匿名類並覆蓋 actionPerformed() 函式將 ActionListener 附加到 JButton。
在 actionPerformed() 方法中,我們建立了 JFileChooser 類的物件。
對於此示例,我們只想選擇影象並將檔案選擇器提示限制為僅選擇影象檔案,為此,我們需要建立一個過濾器。
首先,我們將 setAcceptAllFileFilterUsed() 設定為 false 以禁用選擇所有檔案的提示,然後我們建立 FileNameExtensionFilter 的物件。
在 FileNameExtensionFilter 的建構函式中,我們傳遞了兩個引數:提示中顯示的描述和要使用的副檔名,並不是說我們可以傳遞多個副檔名,因為第二個引數是一個變數。
接下來,我們呼叫 addChoosableFileFilter() 函式並將 FileNameExtensionFilter 物件作為其引數傳遞。
與前面的示例一樣,我們呼叫 showOpenDialog() 函式,然後檢查它是否返回表示欄位 APPROVE_OPTION 的值。如果是這樣,我們將執行條件的真正塊。
我們呼叫 getSelectedFile() 方法並將其儲存為 File 類引用以獲取所選檔案。
我們需要使用 ImageIO 類通過呼叫 read() 方法並傳遞 openedFile 的絕對路徑來讀取選定的影象檔案。
ImageIO.read() 函式返回一個 Image 物件,我們將其用作 JLabel 元件的 setIcon() 方法的引數,它將影象設定為標籤的圖示。
我們將元件新增到 JFrame 並將其可見性設定為 true,
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
public class JavaExample {
public static void main(String[] args) {
JFrame jFrame = new JFrame("JFileChooser Example");
JButton jButton = new JButton("Open an Image");
jButton.setBounds(200, 20, 100, 30);
JLabel jLabel = new JLabel("");
jLabel.setBounds(10, 60, 400, 400);
ImageIcon imageIcon = new ImageIcon();
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser jFileChooser = new JFileChooser();
jFileChooser.setAcceptAllFileFilterUsed(false);
FileNameExtensionFilter fileNameExtensionFilter =
new FileNameExtensionFilter("Image file", "jpg", "jpeg", "PNG");
jFileChooser.addChoosableFileFilter(fileNameExtensionFilter);
int checkInput = jFileChooser.showOpenDialog(null);
if (checkInput == JFileChooser.APPROVE_OPTION) {
File openedFile = jFileChooser.getSelectedFile();
try {
Image image = ImageIO.read(openedFile.getAbsoluteFile());
imageIcon.setImage(image);
jLabel.setIcon(imageIcon);
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
});
jFrame.add(jButton);
jFrame.add(jLabel);
jFrame.setSize(500, 500);
jFrame.setLayout(null);
jFrame.setVisible(true);
}
}
輸出:
帶有 JButton 的 JFrame 視窗:

JFileChooser 提示:

帶有 JButton 和所選影象的 JFrame 視窗:

在 Java 中使用 JFileChooser 開啟文字檔案並的 JFrame 元件中顯示它
在本例中,我們開啟一個文字檔案。代替 JLabel 元件,我們使用 JtextArea 元件來顯示所選文字檔案中的文字。
在 actionPerformed() 方法中,我們建立 JFileChooser 物件並使用副檔名 txt 為文字檔案設定過濾器。我們使用 getSelectedFile() 函式獲取選定的檔案。
為了從檔案中讀取文字,我們首先使用在 BufferedReader 的建構函式中傳遞的 FileReader,它返回一個 BufferedReader 物件。
我們使用帶有此物件的 readLine() 方法獲取每一行。我們建立一個字串變數 string1 和一個 StringBuilder 物件 string2。
我們使用 readLine() 方法並將每一行儲存在 string1 中並檢查該行是否為空;如果該行為空,則表示該行中沒有內容。
我們在每一行新增 string2,並在迴圈中的每次新增後新增一個換行符。
現在我們將 string2 設定為 JTextArea 的文字並使用 close() 方法關閉 BufferedReader。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
public class JavaExample {
public static void main(String[] args) {
JFrame jFrame = new JFrame("JFileChooser Example");
JButton jButton = new JButton("Open a text file");
jButton.setBounds(180, 20, 150, 30);
JTextArea jTextArea = new JTextArea("");
jTextArea.setBounds(10, 60, 480, 400);
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser jFileChooser = new JFileChooser();
jFileChooser.setAcceptAllFileFilterUsed(false);
FileNameExtensionFilter fileNameExtensionFilter =
new FileNameExtensionFilter("Text File", "txt");
jFileChooser.addChoosableFileFilter(fileNameExtensionFilter);
int checkInput = jFileChooser.showOpenDialog(null);
if (checkInput == JFileChooser.APPROVE_OPTION) {
File openedFile = jFileChooser.getSelectedFile();
try {
FileReader fileReader = new FileReader(openedFile);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String string1 = "";
StringBuilder string2 = new StringBuilder();
while ((string1 = bufferedReader.readLine()) != null) {
string2.append(string1).append("\n");
}
jTextArea.setText(string2.toString());
bufferedReader.close();
} catch (IOException fileNotFoundException) {
fileNotFoundException.printStackTrace();
}
}
}
});
jFrame.add(jButton);
jFrame.add(jTextArea);
jFrame.setSize(500, 500);
jFrame.setLayout(null);
jFrame.setVisible(true);
}
}
輸出:
帶有 JButton 的 JFrame 視窗:

JFileChooser 提示:

帶有 JButton 和 JTextArea 的 JFrame 視窗:

Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.
LinkedIn相關文章 - Java Swing
- Java 清除文字欄位
- 使用 Java Swing 建立畫布
- Java Swing 日期
- 在 Java Swing 中更改 JLabel 文字
- 在 Swing 中將 JLabel 居中
- 在 Java 中使用 setFont
