【问题标题】:implementing next and previous buttons using a LinkedList使用 LinkedList 实现下一个和上一个按钮
【发布时间】:2012-04-24 01:59:52
【问题描述】:

这可能是一个愚蠢的问题,但我很难考虑清楚。

我编写了一个使用 LinkedList 在加载的 MIDI 乐器中移动的方法。我想制作一个下一个和一个上一个按钮,以便每次单击该按钮时都会遍历 LinkedList。

如果我多次硬编码itr.next();itr.previous();,我可以遍历LinkedList

public void setInsturment(Synthesizer start,MidiChannel currentChannel[])
{
    try
    {
        start.open();

        Soundbank bank = start.getDefaultSoundbank();

        start.loadAllInstruments(bank);

        LinkedList<Instrument> currentInstrument = new LinkedList<Instrument>();

        Instrument instrs[] = start.getLoadedInstruments();

        currentInstrument.add(instrs[0]);
        currentInstrument.add(instrs[1]);
        currentInstrument.add(instrs[2]);
        currentInstrument.add(instrs[3]);
        currentInstrument.add(instrs[4]);

        ListIterator itr = currentInstrument.listIterator();
        itr.next();
        itr.next();
        itr.next();
     // nextInstrument();

        currentChannel[1].programChange(0,itr.nextIndex());

    }

    catch(MidiUnavailableException e)
    {
        System.out.print("error");
    }

}

我在制作一个可以遍历列表的按钮时遇到了很多麻烦。有没有一种有效的方法来做到这一点?我尝试过这样的事情,但没有成功。

public void actionPerformed(ActionEvent e)
{
    if (e.getSource() == nextButton)
    {
        sound.nextInstrument();
    }

public void nextInstrument()
{
    itr.next();
}

提前谢谢各位!

【问题讨论】:

  • 为什么是链表?一个 ArrayList 并记住当前索引似乎更容易(尤其是因为您已经将仪器放在一个数组中)。
  • 我需要使用数据结构并且我熟悉链表,但是我不知道Java有ArrayLists。

标签: java swing linked-list jbutton actionlistener


【解决方案1】:

嗯,嗯,链表是一个列表,它的项目可以通过索引访问,这不是通过索引访问项目的最佳结构,但我真的不知道你是否可以在那种类型上使用光标集合,但您可以将当前索引存储在实例变量中。

如果你真的想要随机访问,那么你应该考虑使用 ArrayList 而不是链表。

例子:

class NextPrevList {
    private int index = 0;
    private List currentList; //initialize it with some list

    public Object next() {
        return list.get(++index);
    }
    public Object prev() {
        //maybe add a check for out of bounds
        if (index == 0) return null;
        return list.get(--index);
    }
}

我个人认为使用 ArrayList 而不是 LinkedList 会更高效

【讨论】:

  • +1 用于使用列表的索引。迭代器方法的问题是您无法在 UI 打开时添加/删除任何工具,或者您被搞砸了(不会是第一个遇到 ConcurrentModificationException 的人)
【解决方案2】:

ListIterator#next() 方法返回感兴趣的对象。如果是我的项目,我会将该方法返回的内容分配给类字段,然后通知 GUI 更改。

someInstrument = itr.next();
// fire notify observers method.

【讨论】:

  • 我只是为此使用一个数组并将其转储到一个组件中,该组件可以处理Instrument 更改的下一个/上一个、呈现和通知。看我的例子。
【解决方案3】:

接口代码:List&lt;Instrument&gt;。这个相关的example 导航List&lt;ImageIcon&gt;,但可以根据需要更改实现。

【讨论】:

  • 我只是为此使用一个数组。看我的例子。
  • +1 用于使用列表的索引。迭代器方法的问题是您无法在 UI 打开时添加/删除任何工具,或者您被搞砸了(不会是第一个遇到 ConcurrentModificationException 的人)
【解决方案4】:

MIDI 乐器 .. 下一个和上一个按钮

使用数组(例如Instrument[])。它可能以JComboBoxJListJSpinner 的形式显示,以允许用户选择仪器。这是一个使用与渲染器组合的示例。

import java.awt.Component;
import javax.swing.plaf.basic.BasicComboBoxRenderer;
import javax.swing.*;
import javax.sound.midi.*;

class InstrumentSelector {

    public static void main(String[] args) throws MidiUnavailableException {
        Synthesizer synthesizer = MidiSystem.getSynthesizer();
        synthesizer.open();
        final Instrument[] orchestra = synthesizer.getAvailableInstruments();
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                JComboBox orchestraSelector = new JComboBox(orchestra);
                orchestraSelector.setRenderer(new InstrumentRenderer());

                JOptionPane.showMessageDialog(null, orchestraSelector);
            }
        });
    }
}

class InstrumentRenderer extends BasicComboBoxRenderer {

    @Override
    public Component getListCellRendererComponent(
        JList list,
        Object value,
        int index,
        boolean isSelected,
        boolean cellHasFocus) {
        Component c = super.getListCellRendererComponent(
            list, value, index, isSelected, cellHasFocus);
        if (c instanceof JLabel && value instanceof Instrument) {
            JLabel l = (JLabel)c;
            Instrument i = (Instrument)value;
            l.setText(i.getName());
        }
        return c;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-06
    • 1970-01-01
    • 2014-01-10
    • 1970-01-01
    • 2013-12-14
    相关资源
    最近更新 更多