Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何列出我使用JScrollpane、JFilechooser、JList选择的文件?_Java_Swing - Fatal编程技术网

Java 如何列出我使用JScrollpane、JFilechooser、JList选择的文件?

Java 如何列出我使用JScrollpane、JFilechooser、JList选择的文件?,java,swing,Java,Swing,我在做一个音乐播放器。我想问一些问题 在这张图片中(这是我的播放器),我选择“选择音乐”按钮。 在按钮下,有一个JScrollPane,显示我选择的音乐。 如果我点击那个按钮,就会有音乐 如果我选择sample1.mp3和sample2.mp3,音乐将显示给JScrollPane,如下图所示。。(我的问题是,如何在JScrollPane上列出音乐??) 如果我双击每个音乐(sample1,sample2),我选择的音乐正在播放…(另一个问题是,如何播放我选择的音乐?我想要我选择的音乐的名称

我在做一个音乐播放器。我想问一些问题

在这张图片中(这是我的播放器),我选择“选择音乐”按钮。 在按钮下,有一个JScrollPane,显示我选择的音乐。 如果我点击那个按钮,就会有音乐

如果我选择sample1.mp3和sample2.mp3,音乐将显示给JScrollPane,如下图所示。。(我的问题是,如何在JScrollPane上列出音乐??)


如果我双击每个音乐(sample1,sample2),我选择的音乐正在播放…(另一个问题是,如何播放我选择的音乐?我想要我选择的音乐的名称..像sample1,sample2这样我可以播放像C://sample1.mp3,C://sample2.mp3格式是C://‘音乐名称’.mp3如何获得该音乐名称??)

这不是我的代码,我在大学的时候就在某个地方找到了它,可以随意使用

基本上,您需要自定义
文件
对象操作,因为默认情况下,如果您想对可以使用的文件进行一些过滤,JFileChooser没有这方面的功能


播放音乐部分并不是那么简单,因为你需要图书馆。

“另一个问题在这里,…”问答网站也是如此,而不是服务台。每个问题都应该有一个特定的问题,可以用一个答案来回答。“如何在JScrollPane上列出音乐?”将
文件
集合放入
列表模型
,然后将列表模型设置为列表。使用
ListCellRenderer
,使其能够很好地渲染(可能是通过修剪扩展并添加图标)。对不起。。现在我可以显示这些文件了。。我只想知道如何获得我双击的名称的提示..我用谷歌搜索了那个文件和defaultListModel,所以我就这么做了。感谢您的回答“我只想知道如何双击名称的提示”,这与上面提出的两个问题完全不同!-/第三个问题的问题是您现在想要的“提示”。顺便说一句,你做了关于的教程吗?它似乎涵盖了这个(最新的)问题。
import java.awt.BorderLayout;
import java.io.File;
import javax.swing.*;

public class FileDemonstration extends JFrame {

    private static final long serialVersionUID = 1L;
    private JTextArea outputArea; 
    private JScrollPane scrollPane; 

    public FileDemonstration() {
        super("Get Current File demo");

        outputArea = new JTextArea();


        scrollPane = new JScrollPane(outputArea);

        add(scrollPane, BorderLayout.CENTER); 

        setSize(400, 400); 
        setVisible(true); 

        analyzePath(); 
    } 

    // allow user to specify file name
    private File getCurrentFile() {
        // display file dialog, so user can choose file to open
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

        int result = fileChooser.showOpenDialog(this);

        // if user clicked Cancel button on dialog, return
        if (result == JFileChooser.CANCEL_OPTION)
            System.exit(1);

        File fileName = fileChooser.getSelectedFile(); // get selected file

        // display error if invalid
        if ((fileName == null) || (fileName.getName().equals(""))) {
            JOptionPane.showMessageDialog(this, "Invalid File Name",
                    "Invalid File Name", JOptionPane.ERROR_MESSAGE);
            System.exit(1);
        } // end if

        return fileName;
    } // end method getFile

    // display information about file user specifies
    public void analyzePath() {
        // create File object based on user input
        File name = getCurrentFile();

        if (name.exists()) // if name exists, output information about it
        {
            // display file (or directory) information
            outputArea.setText(String.format(
                    "%s%s\n%s\n%s\n%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s", name
                            .getName(), " exists", (name.isFile() ? "is a file"
                            : "isn't a file"),
                    (name.isDirectory() ? "is a directory"
                            : "isn't a directory"),
                    (name.isAbsolute() ? "is absolute path"
                            : "isn't absolute path"), "Last modified: ", name
                            .lastModified(), "Length: ", name.length(),
                    "Path: ", name.getPath(), "Absolute path: ", name
                            .getAbsolutePath(), "Parent: ", name.getParent()));

            if (name.isDirectory()) // output directory listing
            {
                String directory[] = name.list();
                outputArea.append("\n\nDirectory contents:\n");

                for (String directoryName : directory)
                    outputArea.append(directoryName + "\n");
            } 
        } 
        else // not file /directory, display error
        {
            JOptionPane.showMessageDialog(this, name + " does not exist.",
                    "ERROR", JOptionPane.ERROR_MESSAGE);
        } 
    } 

    public static void main(String args[]) {

            FileDemonstration application = new FileDemonstration();
            application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    } 

}