所以,我在下面有一个构造函数,我是Java的新手,所以它可能比它应该更长,但它基本上做的是创建一个具有Jlist,播放按钮和暂停按钮的窗口。在Jlist中将是音乐文件的名称。首次启动时,程序应该希望询问存储音乐的目录。
我的问题是我想给用户选择,选择一个新目录,但我不知道如何更新Jpanel上的Jlist,因为所有这些都是在构造函数中确定的。我唯一的想法是重绘整个Jframe,面板和组件,但这似乎是一个坏主意。 在我的构造函数下面,我发布了我试图做的但是没有用。
Player(){
preferences = Preferences.userRoot().node(this.getClass().getName());
ID = "Preferred Directory";
preferences.get(ID,"");
features = new PlayerFeatures();
Ispaused = false;
DataLine.Info dataInfo = new DataLine.Info(Clip.class,null);
fileExplorer = new JFileChooser();
fileExplorer.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
if(preferences.get(ID,"").equals("")){
int returnVal = fileExplorer.showOpenDialog(chooseDirectory);
if(returnVal == JFileChooser.APPROVE_OPTION){
directory = fileExplorer.getCurrentDirectory();
preferences.put(ID,directory.toString());
}
}
directory = new File(preferences.get(ID,""));
//get resource grabs things, with get resource we can grab things that are sitting in side the class folder.
//directory = new File("C:\\Users\\Jude\\Documents\\musicalChairs\\src");
List<String> list = makePlayList(directory);
System.out.println(list);
songs = new String[list.size()];
songs = list.toArray(songs);
playList = new JList(songs);
playList.addMouseListener(new listMouseListener());
playList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
playList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
playList.setVisibleRowCount(-1);
frame = new JFrame();
panelTop = new JPanel();
panelBottom = new JPanel();
panelBottom.setLayout(new BoxLayout(panelBottom,BoxLayout.X_AXIS));
play = new JButton();
play.addActionListener(new playListener());
pause = new JButton();
pause.addActionListener(new pauseListener());
scrollPane = new JScrollPane(playList);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
panelTop.add(scrollPane);
menuBar = new JMenuBar();
menu = new JMenu("File");
menu.setMnemonic(KeyEvent.VK_F);
chooseDirectory = new JMenuItem("Select Music Folder",KeyEvent.VK_S);
chooseDirectory.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1,ActionEvent.ALT_MASK));
chooseDirectory.addActionListener(new chooseDirectoryListener());
menu.add(chooseDirectory);
menuBar.add(menu);
frame.setJMenuBar(menuBar);
try{
Image img = ImageIO.read(getClass().getResource("Play24.gif"));
Image pauseImg = ImageIO.read(getClass().getResource("Pause16.gif"));
play.setIcon(new ImageIcon(img));
pause.setIcon(new ImageIcon(pauseImg));
Dimension buttonDimensions = new Dimension(25,26);
play.setPreferredSize(buttonDimensions);
pause.setPreferredSize(buttonDimensions);
}catch(IllegalArgumentException ex){
System.out.println("No input exist");
ex.printStackTrace();
}catch(IOException ioe){
System.out.println("Error while reading");
ioe.printStackTrace();
}
features.songURL = Player.class.getResource(songs[0]);
panelBottom.add(play);
panelBottom.add(pause);
frame.getContentPane().add(BorderLayout.WEST,panelTop);
frame.getContentPane().add(BorderLayout.SOUTH,panelBottom);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,250);
frame.setVisible(true);
}
class chooseDirectoryListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
int returnVal = fileExplorer.showOpenDialog(chooseDirectory);
if(returnVal == JFileChooser.APPROVE_OPTION){
directory = fileExplorer.getCurrentDirectory();
preferences.put(ID,directory.toString());
List<String> list = makePlayList(directory);
songs = new String[list.size()];
songs = list.toArray(songs);
playList = new JList(songs);
playList.addMouseListener(new listMouseListener());
playList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
playList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
playList.setVisibleRowCount(-1);
scrollPane = new JScrollPane(playList);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
panelTop.add(scrollPane);
System.out.println("why");
panelTop.revalidate();
panelTop.repaint();
}
}
}
答案 0 :(得分:0)
您实现了您想要做的事情,使用DefaultListModel。像这样的东西应该工作,虽然没有经过测试。
private DefaultListModel model = new DefaultListModel;
public Constructor () {
JList playlist = new JList(model);
panelTop.add(playlist);
...
}
public void addItem (String item) {
model.add(0, item);
}
public void removeItem (String item) {
model.remove(model.indexOf(item));
}
public void clearItems () {
model.clear();
}
下面是一个相对仓促的尝试,以测试在删除和添加项目后列表是否会更新。它已按预期执行测试。
JFrame frame = new JFrame();
JPanel panel = new
DefaultListModel model = new DefaultListModel();
model.add(0, "Item 0");
model.add(1, "Item 1");
JList list = new JList(model);
frame.add(panel);
panel.add(list);
frame.setVisible(true);
model.remove(0);
model.add(1, "Item 234");