Java filedialog不会在运行时显示文件 [英] Java filedialog will not show files on runtime

查看:89
本文介绍了Java filedialog不会在运行时显示文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个java类简介的项目,你可以在其中添加一个库,然后播放音乐,有点像愚蠢的iTunes。不幸的是,在解决了所有语法错误之后,我现在在打开savefiledialog时得到一个运行时。它不会显示我的文件,只显示文件夹。



这是我的代码:



  //   C:\\Users \\Andrew \\Downloads \什么时候会给你up.wav  
// Andrew Douglas
// Imports
import javax.swing。*;
import javax.swing.event。*;
import java.awt。*;
import java.awt.event。*;
import java.io. *;
import javax.sound.sampled。*;
import javax.swing.filechooser。*;
import javax.swing.JTable;


// 创建类
public class JPlayer extends JFrame implements ActionListener {

// 设置表单项和必要的全局
JButton保存,播放,停止,循环;
JFileChooser对话框;
JTable表;
String 艺术家,歌曲,专辑,Loc;
Object [] []数据;
int n = 1 ;
// 制作图书馆,歌曲限制为51首。
JLibrary [ ] addedSong = new JLibrary [ 50 ];

public JPlayer(){
super JPlayer);
// 创建框架
.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this .setTitle( jPlayer< /跨度>);
.setSize( 800 600 < /跨度>);
// 为表格制作标题
String [] columnNames = { 艺术家
歌曲
< span class =code-string>专辑

位置};
// 为数组提供一个值
addedSong [ 0 ] = new JLibrary( Rick Astley NGGYU UnKnown C:\\Users \\Andrew \\Downloads \\\\ ttttttt to to up up up.wav);
// 将其添加到表数组
Object [] [] data = {
{
(addedSong [ 0 ]。returnArtist()),(addedSong [ 0 ]。returnSong()),(addedSong [ 0 ]。returnAlbum()),(addedSong [ 0 ]。returnFile())
}

};
// 创建表格
table = new JTable(data,columnNames);
table.setPreferredScrollableViewportSize( new 维度( 500 70 ));
table.setFillsViewportHeight(true);
// 让它对行进行排序
table.setAutoCreateRowSorter(true);
// 创建滚动条
JScrollPane scrollPane = new JScrollPane(table);
// 制作保存文件对话框以及播放和保存按钮
对话框= new JFileChooser();
play = new JButton( 播放歌曲);
save = new JButton( 保存文件);
// 添加按钮监听器
save.addActionListener();
play.addActionListener( this );
// 向面板添加按钮
JPanel buttons = new JPanel();
buttons.add(保存);
buttons.add(播放);
// 将按钮放在底部
add(按钮,BorderLayout。南);
add(scrollPane);
this .setVisible(true);

}
// 为按钮创建动作监听器
public void actionPerformed(ActionEvent e){
if (e.getSource()== save){
dialog.setFileFilter( new FileNameExtensionFilter( WAV文件 的.wav));
int returnVal = dialog.showSaveDialog(JPlayer。 this );
if (returnVal == dialog.APPROVE_OPTION){
File file = dialog.getSelectedFile();
addToLibrary( ,文件.getName());

}
}
其他 如果(e .getSource()== play){
String holder2;
对象持有人;
holder = table.getValueAt(table.getSelectedRow(), 3 );
尝试 {
文件soundFile = 文件(holder.toString()) ;
AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile);
Clip clip = AudioSystem.getClip();
clip.open(audioIn);
clip.start();
} catch (UnsupportedAudioFileException f){
f.printStackTrace();
} catch (IOException f){
f.printStackTrace();
} catch (LineUnavailableException f){
f.printStackTrace();
}

}}
public static < span class =code-keyword> void
main( String [] args){
JPlayer();
}
public void addToLibrary( String art, String song, String alb, String file){
addedSong [n] = new JLibrary(art,song,alb,file);
int j = 0 ;
while (n> = 0 ){
Object [] [] data = {
{
addedSong [(nj)],
}
};
j = j + 1;
}
n = n +1;

}
}



任何帮助都会受到赞赏,因为我对此很陌生:)

解决方案

请参阅本教程 [ ^ ]。另请注意,如果目录中没有 .wav 文件,那么您只会看到列出的文件夹。


您好b $ b请访问此链接

http://docs.oracle.com/javase/tutorial/ uiswing / components / filechooser.html [ ^ ]

I'm trying to build a project for an intro to java class where you have a library that you can add to and play music, kind of like a dumbed down iTunes. Unfortunately, after solving all of the syntax errors, I'm now getting one runtime when I open the savefiledialog. It won't show me files, only folders.

Here's my code:

//C:\\Users\\Andrew\\Downloads\\never gonna give you up.wav
//Andrew Douglas
//Imports
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.sound.sampled.*;
import javax.swing.filechooser.*;
import javax.swing.JTable;


//Creates class
public class JPlayer extends JFrame implements ActionListener {

	//Sets up form items and necessary globals
	JButton save, play, stop, loop;
	JFileChooser dialog;
	JTable table;
	String Artist, Song, Album, Loc;
	Object[][] data;
	int n = 1;
	//Makes the library, with a 51 song limit.
	JLibrary[] addedSong = new JLibrary[50];

	public JPlayer() {
		super ("JPlayer");
		//Creates frame
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      	this.setTitle("jPlayer");
      	this.setSize(800, 600);
      	//Makes titles for table
      	String[] columnNames =  {"Artist",
                                "Song",
                                "Album",
                                "Location"};
        //Gives one value for array
        addedSong[0] = new JLibrary ("Rick Astley", "NGGYU", "UnKnown", "C:\\Users\\Andrew\\Downloads\\never gonna give you up.wav");
        //Adds it to table array
        Object[][] data = {
        {
        	(addedSong[0].returnArtist()), (addedSong[0].returnSong()), (addedSong[0].returnAlbum()), (addedSong[0].returnFile())
        }

        };
		//Creates table
        table = new JTable(data, columnNames);
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        table.setFillsViewportHeight(true);
        //Lets it sort the rows
        table.setAutoCreateRowSorter(true);
        //Creates the scroller
        JScrollPane scrollPane = new JScrollPane(table);
        //Makes the save file dialog and the play and save buttons
        dialog = new JFileChooser();
        play = new JButton ("Play Song");
        save = new JButton ("Save a file");
        //Adds the button listeners
        save.addActionListener(this);
        play.addActionListener(this);
        //Adds buttons to panel
        JPanel buttons = new JPanel();
        buttons.add(save);
        buttons.add(play);
        //Puts the buttons at the bottom
        add(buttons, BorderLayout.SOUTH);
        add(scrollPane);
      	this.setVisible(true);

	}
	//Creates action listener for button
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == save) {
			dialog.setFileFilter(new FileNameExtensionFilter("WAV File", ".wav"));
			int returnVal = dialog.showSaveDialog(JPlayer.this);
            if (returnVal == dialog.APPROVE_OPTION) {
                File file = dialog.getSelectedFile();
                addToLibrary("", "", "", file.getName());

            }
		}
		else if (e.getSource() == play) {
			String holder2;
			Object holder;
			holder = table.getValueAt(table.getSelectedRow(), 3);
			try {
			File soundFile = new File(holder.toString());
			AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile);
			Clip clip = AudioSystem.getClip();
			clip.open(audioIn);
         	clip.start();
			} catch (UnsupportedAudioFileException f) {
         f.printStackTrace();
      } catch (IOException f) {
         f.printStackTrace();
      } catch (LineUnavailableException f) {
         f.printStackTrace();
      }

	} }
	public static void main(String[]args) {
		new JPlayer();
	}
	public void addToLibrary(String art, String song, String alb, String file) {
			addedSong[n] = new JLibrary(art, song, alb, file);
			int j = 0;
			while (n >= 0) {
			Object[][] data = {
			{
				addedSong[(n-j)],
			}
		};
			j = j+1;
		}
			n = n +1;

	}
}


Any help would be appreciated, as I'm pretty new to this:)

解决方案

See this tutorial[^] for a sample of this control. Note also that if there are no .wav files currently stored in your directories then you will only see folders listed.


Hi please visit this link
http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html[^]


这篇关于Java filedialog不会在运行时显示文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆