获取目录?

时间:2014-09-23 13:53:43

标签: java swing audio directory

我一直在设计一个加载声音文件的程序,然后在按下按钮时播放它。

左上角有一个按钮,表示"正在加载......",右上角有一个按钮,表示"清除", 并且有四个按钮编号。

按,然后按一个数字按钮会弹出一个窗口,类似于按下" loading"在任何其他计划。我需要一个代码将所选目录分配给一个字符串,以便在程序中播放声音时可以调用它。有什么建议吗?

编辑 - 我已经实施了某人的建议 - 这就是我所做的。

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();

}


}

我只在button1上实现了代码 - 但不是2,3或4。

1 个答案:

答案 0 :(得分:0)

如果您希望JFileChooser只选择目录,可以与setFileSelectionMode一起致电DIRECTORIES_ONLY

Loader.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

然后,作为按钮的动作监听器,显示对话框并获取目录:

if (Loader.showOpenDialog(mainsPanel) == JFileChooser.APPROVE_OPTION) { 
    String dir = Loader.getCurrentDirectory();
}
else { /* No selection */ }

Java教程有一章可能有用How to use File Choosers