我想創建一個簡單的基於控制檯的Java應用程序,它需要用戶從他們的本地文件系統中選擇文件。JavaVM Windows 7 64位 - JFileChooser()不顯示對話框
控制檯提示用戶選擇其中一個可用選項,然後打開給定的輸入。
public Client() throws UnknownHostException, IOException {
printuseroptions();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
char userdecision = br.readLine().charAt(0);
System.out.println(userdecision);
switch(userdecision){
case '1':
System.out.println("Which file would you like to open?");
openfile(br.readLine());
break;
case '2':
System.out.println("Which file would you like to close?");
closefile(br.readLine());
break;
}
private boolean openfile(String path){
System.out.println("openfile("+path+")");
return false;
}
private boolean closefile(String path){
System.out.println("closefile("+path+")");
new JFileChooser().showOpenDialog(null);
return false;
}
無論我做什麼,JFileChooser彈出框都不會打開。沒有顯示錯誤消息在控制檯上,但調試步進式顯示以下錯誤:
Blockquote Thread [main] (Suspended)
ClassNotFoundException(Throwable).(String, Throwable) line: 217
ClassNotFoundException(Exception).(String, Throwable) line: not available ClassNotFoundException.(String) line: not available
URLClassLoader$1.run() line: not available
AccessController.doPrivileged(PrivilegedExceptionAction, AccessControlContext) line: not available [native method]
Launcher$ExtClassLoader(URLClassLoader).findClass(String) line: not available
Launcher$ExtClassLoader.findClass(String) line: not available
Launcher$ExtClassLoader(ClassLoader).loadClass(String, boolean) line: not available Launcher$AppClassLoader(ClassLoader).loadClass(String, boolean) line: not available Launcher$AppClassLoader.loadClass(String, boolean) line: not available
Launcher$AppClassLoader(ClassLoader).loadClass(String) line: not available
ResourceBundle$RBClassLoader.loadClass(String) line: not available
CoreResourceBundleControl(ResourceBundle$Control).newBundle(String, Locale, String, ClassLoader, boolean) line: not available
ResourceBundle.loadBundle(CacheKey, List, Control, boolean) line: not available ResourceBundle.findBundle(CacheKey, List, List, int, Control, ResourceBundle) line: not available
ResourceBundle.getBundleImpl(String, Locale, ClassLoader, ResourceBundle$Control) line: not available
ResourceBundle.getBundle(String, ResourceBundle$Control) line: not available
Toolkit$3.run() line: not available AccessController.doPrivileged(PrivilegedAction) line: not available [native method]
Toolkit.() line: not available
Component.() line: not available
Client.closefile() line: 90 Client.() line: 60
Client.main(String[]) line: 36
相同的代碼完全運行在Linux 32位計算機上,所以我懷疑問題是相關的Windows。
下面的代碼在Windows和Linux上按預期運行,所以我懷疑可能是由於控制檯輸入在Windows vs Linux(CR LF)中處理的不同方式。
import javax.swing.JFileChooser;
public class Example {
public static void main(String[] args) {
new JFileChooser().showOpenDialog(null);
}
}
感謝
嗨, 第二天發佈原始問題後,我找出了問題所在。 沒有問題! 對話框打開了,但它在IDE後面打開,但是沒有任何java圖標顯示在任務欄中,所以我從未意識到它實際上是打開的。 再次感謝。 – jmccrohan 2010-12-11 16:45:37
是的 - 我發現這經常發生在Eclipse中,特別是在沒有任務欄圖標的對話框和啓動屏幕上。在JFileChooser上調用requestFocusInWindow()可能會有所幫助,但我無法確定。 – BoffinbraiN 2010-12-13 02:35:14