您能否幫我找到解決此問題的解決方案?在Windows中製作Java Swing接受用戶輸入路徑(從資源管理器複製粘貼路徑)
我試圖與Swing組件一個Java GUI
接口應該接受Windows路徑到一個特定的文件,並觸發一系列功能上的文件按下提交按鈕後
現在,我已經創建的組件,以獲取用戶輸入爲:
JTextField introducedPath1 = new JTextField(50);
我試圖改變通過複製粘貼從資源管理器的路徑獲得的默認Windows路徑爲接受的文件路徑:
File file;
String makeCanonicalPath=introducedPath1.getText().toString();
makeCanonicalPath=makeCanonicalPath.replaceAll("\\", "/");
file = new File(makeCanonicalPath);
但我不斷收到此錯誤:
Exception in thread "AWT-EventQueue-0" java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
\
^
at java.util.regex.Pattern.error(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.util.regex.Pattern.<init>(Unknown Source)
我假設它是由默認路徑在Windows引起的(例如:L:\實踐\測試)得到它「\」解釋爲逃逸序列。
任何幫助或建議,可以理解
如果使用拖放操作,則可以獲取FileList並獲取實際的File對象。沒有必要用字符串猴子。例如,請查看我的代碼:[拖放示例](http://stackoverflow.com/a/13597312/522444) –