Java/Swing: 为什么我的 JFileChooser 打开了两次?
Java/Swing: Why is my JFileChooser opening twice?
所以我创建了一个 Java GUI 应用程序,我有一个主窗体,我 运行 这个 class PathBrowser 通过单击一个 jbutton,JFileChooser 运行虽然有两次,但我尝试从我的主窗体添加 opendialog,这样我就可以在 window.
上拥有相同的徽标
这是我的代码:
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());
}
}
}
谢谢
您正在调用 browse.showOpenDialog
两次,这就是您收到两次的原因。
只需删除这一行:
browse.showOpenDialog(MainForm.frame);
要保留框架的图标,请替换
browse.showOpenDialog(select)
和
browse.showOpenDialog(MainForm.frame)
所以我创建了一个 Java GUI 应用程序,我有一个主窗体,我 运行 这个 class PathBrowser 通过单击一个 jbutton,JFileChooser 运行虽然有两次,但我尝试从我的主窗体添加 opendialog,这样我就可以在 window.
上拥有相同的徽标这是我的代码:
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());
}
}
}
谢谢
您正在调用 browse.showOpenDialog
两次,这就是您收到两次的原因。
只需删除这一行:
browse.showOpenDialog(MainForm.frame);
要保留框架的图标,请替换
browse.showOpenDialog(select)
和
browse.showOpenDialog(MainForm.frame)