2016-11-28 23 views
-1

私は音楽プレーヤーを作っています。私はいくつかの質問をしたい。この絵でJScrollpane、JFilechooser、JListを使用して選択したファイルをリストする方法は?

enter image description here

(これは私の選手です)、私は音楽のボタンを選択してください選択してください。ボタンの下の には、選択した音楽を表示するJScrollPaneがあります。 そのボタンをクリックすると、音楽があります。

enter image description here

私はsample1.mp3とsample2.mp3を選択した場合、な音楽は、次の画像のようにJScrollPaneに表示されます。..

(私の質問は、JScrollPaneの??で音楽を一覧表示する方法を、ここにあります) enter image description here

それぞれの曲(sample1、sample2)をダブルクリックすると、選択した曲が再生されています。(別の質問はこちらです、私が選んだ曲はどうやって演奏しますか? 。sample1、sample2のように、私はC://sample1.mp3,C://sample2.mp3のような音楽を再生できます。C:// 'music name '.mp3その音楽名を取得する方法は?)

+1

※「別の質問はこちらです」※SOはQ&Aサイトであり、ヘルプデスクではありません。それぞれの質問には、単一の答えで答えることができる単一の具体的な質問が必要です。 –

+0

* "JScrollPaneで音楽をリストする方法" * 'File'コレクションを' ListModel'に入れ、リストモデルをリストに設定します。 'ListCellRenderer'を使ってレンダリングしてください(おそらく、拡張子を整えてアイコンを追加することによって)。 –

+0

申し訳ありません..私はそのファイルを表示することができます。私はちょうどダブルクリックの名前を取得するヒントが欲しいです。 – westofsky159

答えて

0

これは私のコードではありません。

デフォルトでJFileChooserのは、そのための能力を持っている、あなたはファイルに何らかのフィルタリングを行いたい場合は、FileNameFilter

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


    } 

} 

を使用することができますし、音楽を再生するためにはありませんので、基本的に、あなたは、、カスタマーFileオブジェクト操作を必要としますあなたがそれのために図書館を必要とするので、単純ではありません。

関連する問題