0

我正在使用此代码从文本文件中加载 JmenuItems

FileInputStream wordsFile = new FileInputStream(signlink.findcachedir() + "/Playlist.div");
        BufferedReader br = new BufferedReader(new InputStreamReader(wordsFile));
        String[] favoriteSongs = new String[15];
        for (int i = 0; i < favoriteSongs.length; i++) {
            favoriteSongs[i] = br.readLine();
            if (favoriteSongs[i] != null) {
                System.out.println(favoriteSongs[i]);
                JMenuItem song = new JMenuItem(favoriteSongs[i]);
                song.addActionListener(this);
                favorites.add(favoriteSongs[i]);
            } else { 
                wordsFile.close();
                break;
            }
        }   

如何为 JmenuItems 添加操作

4

1 回答 1

0

Im not sure i understand you correctly but: You added action listner 'song.addActionListener(this)' so this mean that you have in your class an overriden method from interface, you need to put some logic there and it should work i.e.

@Override
public void actionPerformed(ActionEvent e) {
    JMenuItem item = (JMenuItem) e.getSource();
    String song = item.getText();
    listOfSth.add(song);
}

Or you could create new class that implements ActionListner and add it to created MenuItem

    JMenuItem l = new JMenuItem();
    l.addActionListener(new CustomActionListener());
于 2013-10-09T21:04:54.790 回答