Java 使用LinkedList实现下一个和上一个按钮
这可能是个愚蠢的问题,但我很难把它想清楚 我编写了一个方法,使用LinkedList在加载的MIDI乐器中移动。我想创建一个下一个和一个上一个按钮,以便每次单击该按钮时都能遍历LinkedList 如果我硬编码Java 使用LinkedList实现下一个和上一个按钮,java,swing,linked-list,jbutton,actionlistener,Java,Swing,Linked List,Jbutton,Actionlistener,这可能是个愚蠢的问题,但我很难把它想清楚 我编写了一个方法,使用LinkedList在加载的MIDI乐器中移动。我想创建一个下一个和一个上一个按钮,以便每次单击该按钮时都能遍历LinkedList 如果我硬编码itr.next()或itr.previous()我可以遍历LinkedList多次 public void setInsturment(Synthesizer start,MidiChannel currentChannel[]) { try { star
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");
}
}
提前谢谢各位 方法返回感兴趣的对象。如果它是我的项目,我会将从该方法返回的内容分配给一个类字段,然后将更改通知GUI
someInstrument = itr.next();
// fire notify observers method.
嗯,链表是一个列表,它的项可以通过索引访问,这不是通过索引访问项的最佳结构,但是我真的不知道你是否可以在这种集合上有一个游标,但是你可以将当前索引存储在一个实例变量上
如果你真的想要随机访问,那么你应该考虑使用ARAYLIST而不是链表。
例如:
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性能更好接口的代码:列表
。此相关文件导航列表
,但可以根据需要更改实现
MIDI乐器。。下一步和上一步按钮
使用阵列(例如仪器[]
)。它可能显示在JComboBox
、JList
或JSpinner
中,以允许用户选择仪器。下面是一个使用带有渲染器的组合的示例
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;
}
}
为什么是链表?一个ArrayList和记住当前索引似乎更容易(特别是因为你已经有了数组中的工具)。我需要使用一个数据结构,并且我熟悉链表,但是我不知道Java有ArrayList。我会使用数组来实现这一点。请参阅我的示例。+1用于使用列表的索引。迭代器方法的问题是,当用户界面打开时,您无法添加/删除任何工具,或者您被拧死了(不会是第一个遇到ConcurrentModificationException
)我只需为此使用一个数组,并将其转储到可以处理下一个/上一个的组件中,提交和通知仪器
更改。请参阅我的示例。+1用于使用列表的索引。迭代器方法的问题在于,当用户界面打开时,您无法添加/删除任何工具,或者您被拧死了(不会是第一个遇到ConcurrentModificationException
的人)
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;
}
}