[英]JFileChooser not showing text files
出於某種原因,每當我嘗試選擇一個文本文件時,它都不會顯示在文件選擇器中,即使目錄中顯然有一個文本文件。 我的代碼有問題嗎?
package me.riley.logreader;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.filechooser.FileNameExtensionFilter;
public class LogReader {
public static void main(String[] args) {
ActionListeners actions = new ActionListeners();
JFrame frame = new JFrame("Log Reader");
//Window options
frame.setVisible(true);
frame.setSize(500,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Adds the panel to the frame
JPanel panel = new JPanel();
frame.add(panel);
//Creates the button and places it inside the panel
JButton button = new JButton("Click Here");
button.setLocation(10, 10);
panel.add(button);
button.addActionListener(actions);
//Allows user to open a text file
JFileChooser filechooser = new JFileChooser();
filechooser.setFileFilter(new FileNameExtensionFilter(".txt", "txt"));
filechooser.setDialogTitle("Choose a text file");
filechooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
filechooser.showOpenDialog(null);
}
}
這是因為您已將 FileSelectionMode 設置為 DIRECTORIES_ONLY。
刪除filechooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
它應該工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.