2012-04-24 95 views
3

这可能是一个愚蠢的问题,但我在思考这个问题时遇到了困难。使用LinkedList实现下一个和上一个按钮

我写了一个方法,它使用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(); 
} 

在此先感谢你们!

+0

为什么链表?一个ArrayList和记住当前索引似乎更容易(特别是因为你已经有你的仪器在一个数组中)。 – Thilo 2012-04-24 02:02:36

+0

我需要使用数据结构,并且我熟悉链表,但是我不知道Java有ArrayLists。 – FireStorm 2012-04-24 02:10:36

回答

4

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

如果你真的想随机访问,那么你应该考虑使用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

+0

+1编码到'List'接口。 – trashgod 2012-04-24 02:10:42

+1

+1使用列表的索引。迭代器方法的问题是,当你的UI打开时,你不能添加/删除任何工具,或者你被搞砸了(不会是第一个碰到'ConcurrentModificationException'的人) – Robin 2012-04-24 05:40:48

4

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

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

+1表示松耦合。 – trashgod 2012-04-24 02:09:02

+1

我只是为此使用一个数组,并将其转储到一个组件中,该组件可以处理“仪器”更改的下一个/上一个,呈现和通知。看我的例子。 – 2012-04-24 02:35:06

4

代码发送到界面更高性能:List<Instrument>。这个相关的example导航List<ImageIcon>,但实施可以根据需要进行更改。

+0

我只是用这个数组。看我的例子。 – 2012-04-24 02:33:32

+1

+1使用列表的索引。迭代器方法的问题在于,您的UI在打开时无法添加/删除任何工具,或者您被搞砸了(为了清晰起见,您不会第一个碰到'ConcurrentModificationException'的人) – Robin 2012-04-24 05:40:23

4

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

使用阵列(例如Instrument[])。它可以显示在JComboBoxJListJSpinner中,以允许用户选择乐器。这里是一个使用渲染器组合的例子。

enter image description here

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; 
    } 
} 
+0

+1。 – trashgod 2012-04-24 09:42:59

相关问题