下面的两个代码,为什么我点击 JList 的一个项目,会打印出来两个 索引呢?
public class MasFrame extends JFrame { public MasFrame(String title) {
super(title);
setCloseClick();
setLF();
} private void setCloseClick() {
// create window listener to respond to window close click
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
} // ------------------------------------------
private void setLF() {
// Force SwingApp to come up in the System L&F
String laf = UIManager.getSystemLookAndFeelClassName();
try {
UIManager.setLookAndFeel(laf);
} catch (UnsupportedLookAndFeelException exc) {
System.err.println("Warning: UnsupportedLookAndFeel: " + laf);
} catch (Exception exc) {
System.err.println("Error loading " + laf + ": " + exc);
}
}}public class TrainingFrame extends MasFrame implements ActionListener { public TrainingFrame() {
super("Training Mechanism");
// TODO Auto-generated constructor stub JPanel jp = new JPanel();

this.getContentPane().add(jp);
JScrollPane sp = new JScrollPane();
jp.add(sp);

Vector<String> vec = new Vector();

vec.addElement("Default");
vec.addElement("Advance");

final JList list = new JList(vec); //data has type Object[]
list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
list.setVisibleRowCount(-1);

list.addListSelectionListener(new ListSelectionListener(){ @Override
public void valueChanged(ListSelectionEvent arg0) {
// TODO Auto-generated method stub
System.out.println(list.getSelectedIndex());
System.out.print("AAA");
}

} ); JScrollPane listScroller = new JScrollPane(list);
listScroller.setPreferredSize(new Dimension(250, 180)); list.setBackground(new Color(255, 255, 0));

sp.getViewport().add(list);

this.setLocationByPlatform(isAlwaysOnTop());   
this.setSize(800, 500);

setVisible(true);
}
}