And*_*tou 1 java swing jfilechooser
所以我创建了一个Java GUI应用程序,我有一个主窗体,我通过单击一个jbutton来运行这个类PathBrowser,JFileChooser运行两次,我尝试从我的mainform添加opendialog,这样我就可以在窗口.
这是我的代码:
public class PathBrowser {
public static String filepath = null;
public static void main(String[] args)
{
JButton select = new JButton();
JFileChooser browse = new JFileChooser();
//add the icon of main form for JFileChooser
//OPENS TWICE?! Error
browse.showOpenDialog(MainForm.frame);
//if blank goes to user/documents. Unsure about other OSes
browse.setCurrentDirectory(new java.io.File("C:/"));
browse.setDialogTitle("Browse Folder");
browse.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
//when clicked open (approve option)
if (browse.showOpenDialog(select) == JFileChooser.APPROVE_OPTION){
//folder has peen selected
MainForm.selfolder = true;
//add the path to the string filepath
filepath = (browse.getSelectedFile().getAbsolutePath());
System.out.println("The path for the server is: "+browse.getSelectedFile().getAbsolutePath());
//add the information to the textarea
MainForm.textArea.setText("The path for the server is: "+browse.getSelectedFile().getAbsolutePath());
}
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢
你打电话browse.showOpenDialog两次,这就是为什么你得到它两次.
只需删除此行:
browse.showOpenDialog(MainForm.frame);
Run Code Online (Sandbox Code Playgroud)
并保持框架的图标,替换
browse.showOpenDialog(select)
Run Code Online (Sandbox Code Playgroud)
同
browse.showOpenDialog(MainForm.frame)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
799 次 |
| 最近记录: |