我無法讓JFileChooser只顯示我想要的擴展名(文本文件)。無法讓FileNameExtensionFilter工作
JFileChooser fc = new JFileChooser();
FileNameExtensionFilter textFilter = new FileNameExtensionFilter("Text Files","txt");
fc.addChoosableFileFilter(textFilter);
我在做什麼錯?如果我刪除過濾器,則會顯示文本文件。 問題可能看起來很愚蠢,但我試圖找到解決方案,我的代碼看起來與其他示例相同。也許我只是大腦鎖定。
謝謝您的時間
我嘗試了建議的做法,我仍然有同樣的問題。所以其他地方一定有什麼問題。我將加載類代碼 - 也許有人能發現我在做什麼錯:
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.filechooser.FileNameExtensionFilter;
public class ButtonListener implements ActionListener {
private JFrame fr;
public ButtonListener (JFrame frame){
fr = frame;
}
public void actionPerformed(ActionEvent event) {
if(event.getSource() instanceof JButton) {
String action = event.getActionCommand();
if (action.equals("First text")){
JFileChooser fc = new JFileChooser();
FileNameExtensionFilter textFilter = new FileNameExtensionFilter("Text Files","txt");
fc.addChoosableFileFilter(textFilter);
int returnVal = fc.showOpenDialog(fr);
}
else if (action.equals("Second text")){
}
else {
System.out.println("Error in ButtonListener");
}
}
}
}
還沒有完成,但它仍然應該能夠處理,然後,我有延長的問題。
此代碼適用於我。您可能會遇到導致此行爲的不同問題 – ControlAltDel 2012-04-11 17:17:18
嗯,我在幾天後重新啓動計算機,現在可以運行 - 感謝您的幫助。 – 2012-04-18 14:56:26