Java 获取目录?
我一直在设计一个程序,加载一个声音文件,然后在按下按钮时播放 左上角有一个按钮,上面写着“加载…”,右上角有一个按钮,上面写着“清除”, 有四个按钮被编号 按下数字按钮,然后弹出一个窗口,类似于在任何其他程序上按下“加载”时所看到的窗口。我需要一个代码,将选定的目录分配给一个字符串,以便在程序中播放声音时可以调用它。有什么建议吗 编辑-我已经实施了某人的建议-以下是我所做的Java 获取目录?,java,swing,audio,directory,Java,Swing,Audio,Directory,我一直在设计一个程序,加载一个声音文件,然后在按下按钮时播放 左上角有一个按钮,上面写着“加载…”,右上角有一个按钮,上面写着“清除”, 有四个按钮被编号 按下数字按钮,然后弹出一个窗口,类似于在任何其他程序上按下“加载”时所看到的窗口。我需要一个代码,将选定的目录分配给一个字符串,以便在程序中播放声音时可以调用它。有什么建议吗 编辑-我已经实施了某人的建议-以下是我所做的 package soundboard; import javax.swing.*; import java.io.*;
package soundboard;
import javax.swing.*;
import java.io.*;
import javax.sound.sampled.*;
public class Soundboard {
JButton loadButton;
JFileChooser Loader;
JButton clearButton;
JButton button1;
JButton button2;
JButton button3;
JButton button4;
JPanel mainsPanel;
int load;
File one;
File two;
File three;
File four;
public void windowCreate() {
JFrame frame = new JFrame();
mainsPanel = new JPanel();
Loader = new JFileChooser();//174
Loader.setSize(500,500);
Loader.setLocation(174,4);
loadButton = new JButton("Load...");
loadButton.setSize(80, 30);
loadButton.setLocation(4, 4);
loadButton.addActionListener(e -> {
load = 1;
});
clearButton = new JButton("Clear");
clearButton.setSize(80, 30);
clearButton.setLocation(92, 4);
clearButton.addActionListener(e -> {
System.out.println("Cleared");
});
button1 = new JButton("1");
button1.setSize(80, 80);
button1.setLocation(4, 45);
button1.addActionListener(e -> {
if (load == 1){
one = Loader.getSelectedFile();
load = 0;
}
else {
System.out.println(one);
}
});
button2 = new JButton("2");
button2.setSize(80, 80);
button2.setLocation(92, 45);
button2.addActionListener(e -> {
if (load == 1){
System.out.println("Load 2");
load = 0;
}
else {
System.out.println("2");
}
});
button3 = new JButton("3");
button3.setSize(80, 80);
button3.setLocation(4, 133);
button3.addActionListener(e -> {
if (load == 1){
System.out.println("Load 3");
load = 0;
}
else {
System.out.println("3");
}
});
button4 = new JButton("4");
button4.setSize(80, 80);
button4.setLocation(92, 133);
button4.addActionListener(e -> {
if (load == 1){
System.out.println("Load 4");
load = 0;
}
else {
System.out.println("4");
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(loadButton);
frame.add(clearButton);
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(Loader);
frame.add(mainsPanel);
frame.setSize(675,485);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
public static void main(String[] args){
Soundboard window = new Soundboard();
window.windowCreate();
}
}
但是,我只在按钮1上实现了代码,而不是2、3或4。如果您希望
JFileChooser
只选择目录,您可以调用setFileSelectionMode
以及directories\u only
:
Loader.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
然后,作为按钮的操作侦听器,显示对话框并获取目录:
if (Loader.showOpenDialog(mainsPanel) == JFileChooser.APPROVE_OPTION) {
String dir = Loader.getCurrentDirectory();
}
else { /* No selection */ }
Java教程中有一章介绍这一点,可能会很有用。如果您正在使用,您可能希望使用。它将以
字符串的形式返回所选目录
。发布一些代码后,您将得到更有意义的响应