package three.day.frame;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class ListSelectionEventDemo extends JFrame implements ListSelectionListener{
private JLabel lbTip;
private JList list;
private JScrollPane scrollPane;
String[] strScale = {"幼儿园","小学","初中","高中","研究生","硕士","博士"};
/**
* @param args
*/
public static void main(String[] args) {
new ListSelectionEventDemo("ListSelectionEvent Demo");
}
/**
* @param arg0
* @throws HeadlessException
*/
public ListSelectionEventDemo(String title) throws HeadlessException {
super(title);
Container panel = getContentPane();
panel.setLayout(new GridLayout(2,1));
lbTip = new JLabel("你选择的是:");
list = new JList(strScale);
list.setBorder(BorderFactory.createTitledBorder("请选择"));
list.setOpaque(true);
list.setBackground(new Color(180,255,0));
list.setVisibleRowCount(3);
list.addListSelectionListener(this);
scrollPane = new JScrollPane(list);
panel.add(lbTip);
panel.add(scrollPane);
setLocation(300,200);
pack();
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setVisible(true);
}
@Override
public void valueChanged(ListSelectionEvent e) {
// TODO Auto-generated method stub
int[] selectedIndex = list.getSelectedIndices();
String str="你选择的是:";
for(int i=0;i<selectedIndex.length;i++)
{
str = str+" "+strScale[selectedIndex[i]];
}
lbTip.setText(str);
}
}