0

我想让客户端从文本文件中加载玩家最喜欢的歌曲并将每一行

整个班级都在这里:

http://pastebin.com/TeMk3Nft(因为如果我在这里发布它会是太多的代码和文本)

但我不确定该怎么做

ps 我不太确定循环应该迭代什么(第 104 行)

4

1 回答 1

1

您的课程很大,并且缺少其他课程参考。尽管如此,我为您整理了一个示例。我相信这会对你有所帮助。

import java.awt.EventQueue;

public class SongPlayer {

    private JFrame frmSongPlayer;
    private List<String> songs;
    private ActionListener listener = new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            if (e.getSource() instanceof JMenuItem) {
                JMenuItem item = (JMenuItem) e.getSource();
                // now
                String url = "http://songs/" + item.getName();
                System.out.println(url);
            }

        }
    };

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    SongPlayer window = new SongPlayer();
                    window.frmSongPlayer.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public SongPlayer() {
        try {
            songs = FileUtils.readLines(new File(SongPlayer.class.getResource("/PlayList.txt").getPath()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frmSongPlayer = new JFrame();
        frmSongPlayer.setTitle("Song player");
        frmSongPlayer.setBounds(100, 100, 450, 300);
        frmSongPlayer.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmSongPlayer.getContentPane().setLayout(null);

        JMenuBar songBar = new JMenuBar();
        songBar.setBounds(10, 11, 101, 23);
        frmSongPlayer.getContentPane().add(songBar);

        JMenu song = new JMenu("Songs");

        songBar.add(song);
        for (String mp3song : songs) {
            JMenuItem mntmNewMenuItem = new JMenuItem(mp3song);
            mntmNewMenuItem.setName(mp3song);
            mntmNewMenuItem.addActionListener(listener);

            song.add(mntmNewMenuItem);
        }

    }
}

上面的类将打开Swing UI歌曲菜单,并从文件中选择项目。Playlist.txt当您单击歌曲时,它会生成适当的 url。

于 2013-10-08T11:06:07.617 回答