[英]Selecting Folder Destination in Java?
I am a newbie to Java. I am trying to dynamically choose the file location to save the outcome of my project (to be initiated at the very start of my project).我是 Java 的新手。我正在尝试动态选择文件位置以保存我的项目结果(在我的项目一开始就启动)。 I worked around with a few FileDialog examples, but each one of them allows me to choose a file and not a folder.
我使用了几个 FileDialog 示例,但每个示例都允许我选择文件而不是文件夹。
Can anyone please help me with an example (or) link to one for the same?谁能帮我举个例子(或)链接到一个相同的例子?
You could try something like this (as shown here: Select a Directory with a JFileChooser ):您可以尝试这样的操作(如此处所示: Select a Directory with a JFileChooser ):
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
public class DemoJFileChooser extends JPanel
implements ActionListener {
JButton go;
JFileChooser chooser;
String choosertitle;
public DemoJFileChooser() {
go = new JButton("Do it");
go.addActionListener(this);
add(go);
}
public void actionPerformed(ActionEvent e) {
chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle(choosertitle);
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
//
// disable the "All files" option.
//
chooser.setAcceptAllFileFilterUsed(false);
//
if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
System.out.println("getCurrentDirectory(): "
+ chooser.getCurrentDirectory());
System.out.println("getSelectedFile() : "
+ chooser.getSelectedFile());
}
else {
System.out.println("No Selection ");
}
}
public Dimension getPreferredSize(){
return new Dimension(200, 200);
}
public static void main(String s[]) {
JFrame frame = new JFrame("");
DemoJFileChooser panel = new DemoJFileChooser();
frame.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);
frame.getContentPane().add(panel,"Center");
frame.setSize(panel.getPreferredSize());
frame.setVisible(true);
}
}
Oracles Java Tutorial for File Choosers: http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html Oracles Java 文件选择器教程: http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html
Note getSelectedFile()
returns the selected folder, despite the name.注意
getSelectedFile()
返回选定的文件夹,不管名称如何。 getCurrentDirectory()
returns the directory of the selected folder. getCurrentDirectory()
返回所选文件夹的目录。
import javax.swing.*;
public class Example
{
public static void main(String[] args)
{
JFileChooser f = new JFileChooser();
f.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
f.showSaveDialog(null);
System.out.println(f.getCurrentDirectory());
System.out.println(f.getSelectedFile());
}
}
try something like this尝试这样的事情
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle("select folder");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
Along with JFileChooser is possible use this:与 JFileChooser 一起可以使用这个:
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
for have a Look and Feel like Windows.外观和感觉就像 Windows。
for others settings, view here: https://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html#available对于其他设置,请在此处查看: https://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html#available
I found a good example of what you need in this link .我在这个链接中找到了一个很好的例子来说明你需要什么。
import javax.swing.JFileChooser;
public class Main {
public static void main(String s[]) {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle("choosertitle");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
System.out.println("getCurrentDirectory(): " + chooser.getCurrentDirectory());
System.out.println("getSelectedFile() : " + chooser.getSelectedFile());
} else {
System.out.println("No Selection ");
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.