我正在尝试编写一个程序,该程序允许用户在特定位置制作一组文件夹。到目前为止,我已经尝试了很多方法,但是似乎没有任何效果。
为了更清楚一点,我让用户输入一个文件夹路径(使用JFileChooser
),它将在文本字段(JTextField
)中为它们设置目录。我想知道如何做到这一点,以便在用户定义的路径中创建文件夹时,它也相应地命名该文件夹。这是到目前为止我所拥有的一些“有效的”示例代码:
public static void addComponentsToPane(Container pane)
{
if (RIGHT_TO_LEFT)
{
pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
//Components
pane.setLayout(new GridBagLayout());
JLabel label;
GridBagConstraints c = new GridBagConstraints();
JButton buttonH;
JButton buttonB;
JButton cButton;
JCheckBox checkboxP;
JCheckBox checkboxV;
JCheckBox checkboxD;
JCheckBox checkboxM;
if(shouldFill)
{
c.fill = GridBagConstraints.HORIZONTAL;
}
//Input Label
label = new JLabel(" Please Enter Folder Destination ");
if(shouldWeightX)
{
c.weightx = 0.5;
}
c.ipady = 10;
c.fill = GridBagConstraints.BOTH;
c.gridwidth = 2;
c.gridx = 0;
c.gridy = 0;
pane.add(label, c);
//Blank Label
label = new JLabel (" ");
c.fill = GridBagConstraints.BOTH;
c.ipady = 5;
c.gridx = 2;
c.gridy = 1;
pane.add(label, c);
//Text Field
text = new JTextField(" Folder Path ");
c.ipady = 0;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.0;
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 3;
pane.add(text, c);
text.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent t)
{
textAreaPath = text.getText();
p1 = Paths.get(textAreaPath);
}
});
//Browse Button
buttonB = new JButton(" Browse ");
c.fill = GridBagConstraints.BOTH;
c.gridx = 3;
c.gridwidth = 1;
c.gridy = 2;
pane.add(buttonB, c);
buttonB.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent b)
{
JFileChooser fc = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter
("Directories","Directories");
fc.setCurrentDirectory(new java.io.File("home.desktop"));
fc.setAcceptAllFileFilterUsed(false);
fc.setFileFilter(filter);
fc.setDialogTitle(DIRECTORY);
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = fc.showOpenDialog(text);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
text.setText(fc.getSelectedFile().getAbsolutePath());
}
;
}
});
//Blank Label
label = new JLabel (" ");
c.fill = GridBagConstraints.BOTH;
c.ipady = 10;
c.gridx = 0;
c.gridwidth = 3;
c.gridy = 3;
pane.add(label, c);
//Continue Button
cButton = new JButton(" Continue ");
c.fill = GridBagConstraints.BOTH;
c.ipady = 0;
c.gridwidth = 1;
c.gridx = 3;
c.gridy = 4;
pane.add(cButton, c);
//Help Button
buttonH = new JButton (" Help ");
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridy = 4;
pane.add(buttonH, c);
c.weighty = 3.0;
c.anchor = GridBagConstraints.PAGE_END;
buttonH.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent h)
{
JFrame frameH = new JFrame(" Help ");
JLabel labelH;
frameH.setResizable(false);
frameH.setSize(300, 150);
labelH = new JLabel("<html> In The 'Folder Path' Text Area,"
+" Input The Location Where You Would Like To Create"
+" The New Folder. <br><br> Then Choose The File Type"
+" That You Would Like To Have Sorted Into The Folder."
+"<html> ");
frameH.getContentPane().add(labelH);
frameH.setVisible(true);
}
}); //Ends Action Listener
//Label Page Break
label = new JLabel (" ______________________________________________"
+"_____________________________ ");
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridwidth = 5;
c.gridy = 5;
pane.add(label, c);
//Blank Label
label = new JLabel (" ");
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridwidth = 5;
c.gridy = 6;
pane.add(label, c);
//Label Check Box Instructions
label = new JLabel ("<html> Please Select The File Type That You Would"
+" Like To Add To The Folder.<html> ");
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridwidth = 5;
c.gridy = 7;
pane.add(label, c);
//Check Box (Pictures);
checkboxP = new JCheckBox(" Pictures ");
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridwidth = 1;
c.gridy = 8;
pane.add(checkboxP, c);
checkboxP.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent p)
{
SEARCHER = "pictures";
}
});
//Check Box (Documents)
checkboxD = new JCheckBox(" Documents ");
c.fill = GridBagConstraints.BOTH;
c.gridx = 2;
c.gridy = 8;
pane.add(checkboxD, c);
checkboxD.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent d)
{
SEARCHER = "documents";
}
});
//Check Box (Videos)
checkboxV = new JCheckBox(" Videos ");
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridy = 10;
pane.add(checkboxV, c);
checkboxV.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent v)
{
SEARCHER = "videos";
}
});
//Check Box (Music)
checkboxM = new JCheckBox(" Music ");
c.fill = GridBagConstraints.BOTH;
c.gridx = 2;
c.gridy = 10;
pane.add(checkboxM, c);
checkboxM.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent m)
{
SEARCHER = "music";
}
});
//Searcher If's
if (SEARCHER == pictures)
{
newFolderName = "Pictures";
extensionType = ".jpg"+".png";
}
if (SEARCHER == documents)
{
newFolderName = "Documents";
extensionType = ".txt"+".doc";
}
if(SEARCHER == videos)
{
newFolderName = "Videos";
extensionType = ".wmv"+".mp4"+".mov";
}
if (SEARCHER == music)
{
newFolderName = "Music";
extensionType = ".mp3";
}
//Continue Button Action Listener
cButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent contd)
{
createDirectory(p1);
}
private void createDirectory(Path p1)
{
}
});
}
//Creating The GUI
//-----------------------------------------------------------------------------
private static void createAndShowFrame()
{
JFrame frame = new JFrame(" File Organizer ");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
//Main Method Header
public static void main(String[] args)
{
try {
UIManager.setLookAndFeel
(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException
| IllegalAccessException | UnsupportedLookAndFeelException e)
{
e.printStackTrace();
}
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowFrame();
}
});
}
}
参考方案
您唯一需要的是:
File file = new File(text.getText());
if(!file.exists()) file.mkdir();
其中text.getText()引用JTextField,但是,如果需要,您可以使用对象Path。
对于Java中的isDirectory和isFile,文件始终返回false - java为什么file为isFile()方法返回false,即使它是file。当它是目录时,它为isDirectory()返回false。难道我做错了什么?我测试的这些文件/目录不存在,我需要创建它们,所以这就是为什么我要测试使用createFile()还是mkdir()的原因。File file = new File("C:/Users/John/Des…
Java Double与BigDecimal - java我正在查看一些使用双精度变量来存储(360-359.9998779296875)结果为0.0001220703125的代码。 double变量将其存储为-1.220703125E-4。当我使用BigDecimal时,其存储为0.0001220703125。为什么将它双重存储为-1.220703125E-4? 参考方案 我不会在这里提及精度问题,而只会提及数字…
当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…
Java-父类正在从子类中调用方法? - java抱歉,我还是编码的新手,可能还没有掌握所有术语。希望您仍然能理解我的问题。我想得到的输出是:"Cost for Parent is: 77.77" "Cost for Child is: 33.33" 但是,我得到这个:"Cost for Parent is: 33.33" "Cost f…
Java Map,如何将UTF-8字符串正确放置到地图? - java我有一个地图,LinkedHashMap更确切地说。我想在上面放一个字符串对象。然后,我读取此值以查看实际存储的内容。字符串本身具有非ASCII字符(西里尔文,韩文等)。将其放到地图上然后阅读后,这些字符将替换为??? s。一些代码:Map obj = new LinkedHashMap(); System.out.println("name: &…