【问题标题】:Getting a directory? [closed]获取目录? [关闭]
【发布时间】:2014-11-17 18:28:03
【问题描述】:

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

左上角有一个按钮,上面写着“加载...”,右上角有一个按钮,上面写着“清除”, 并且有四个带编号的按钮。

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

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

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。

【问题讨论】:

  • 如果您使用的是JFileChooser,您可能需要使用getCurrentDirectory()。它将在String 中返回所选目录。
  • 发布一些代码后你会得到更有意义的回复

标签: java swing audio directory


【解决方案1】:

如果您希望JFileChooser 仅选择目录,您可以调用setFileSelectionModeDIRECTORIES_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 的章节可能很有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-26
    • 2010-09-12
    • 1970-01-01
    • 2018-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多