我正在尝试创建一个播放列表gui。 此代码显示带有菜单栏的JFrame和运行时自动显示的文件选择器,我需要添加什么才能使用户选择按钮打开文件选择器以及如何在选择后显示音乐文件?好像它要像播放列表一样显示,以便他们可以选择删除,复制等。感谢您的帮助!!
public class PlaylistView extends JFrame{
public static void main(String[] args){
JFrame f = new JFrame("Playlist");
f.setVisible(true);
f.setSize(800,600);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
//The creation of a menubar
JMenuBar mb = new JMenuBar();
//sets menu bar onto the frame
f.setJMenuBar(mb);
//New menu items
JMenu file = new JMenu("File");
JMenu edit = new JMenu("Edit");
JMenu exit = new JMenu("Exit");
mb.add(file);
mb.add(edit);
mb.add(exit);
JMenuItem addsong = new JMenuItem("Add Song");
JMenuItem deletesong = new JMenuItem("Delete Song");
JMenuItem copysong = new JMenuItem("Copy Song");
file.add(addsong);
file.add(deletesong);
file.add(copysong);
JMenuItem addplaylist = new JMenuItem("Add Playlist");
edit.add(addplaylist);
JMenuItem deleteplaylist = new JMenuItem("Delete Playlist");
edit.add(deleteplaylist);
JMenuItem saveplaylist = new JMenuItem("Save Playlist");
edit.add(saveplaylist);
JMenuItem copyplaylist = new JMenuItem("Copy Playlist");
edit.add(copyplaylist);
//Button actually open to the file chooser
JButton open = new JButton("Add Song");
//Filechooser to add a song in
JFileChooser fc = new JFileChooser();
fc.setCurrentDirectory(new java.io.File("."));
fc.setDialogTitle("Add Song");
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
if (fc.showOpenDialog(open) == JFileChooser.OPEN_DIALOG){
}
System.out.println("File Selected:" + fc.getSelectedFile().getAbsoluteFile());
}
}
答案 0 :(得分:1)
要显示列表中的文件,可以将它们保存在JList中。
DefaultListModel mylistmodel = new DefaultListModel();
JList songs= new JList(mylistmodel );
Everytime you select a file, you can call add the item to the list by,
mylistmodel.addElement(songtoAdd); // songtoadd is an object
mylistmodel.insertElementAt(songtoAdd, 0); // to insert songs at specific positions.
mylistmodel.remove(IndexofSong);
您可以使用特定方法进行上述活动,然后调用相应的方法来执行每个操作。