import java.awt.*;
import javax.swing.*;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
class JListPerform extends JFrame
{
JList lst;
JLabel lbl;
String []s={ "长城 ", "桂林 ", "张家界 ", "庐山 ", "韶山 ", "青岛 ", "黄鹤楼 "};
JListPerform(){
Container cn=this.getContentPane();
cn.setLayout(new BorderLayout());
lbl=new JLabel();
lst=new JList(s);
lst.setBorder(BorderFactory.createTitledBorder( "选择你想去的旅行点 "));
lst.addListSelectionListener(new Perform(this));
cn.add(lbl,BorderLayout.NORTH);
cn.add(lst,BorderLayout.CENTER);
setTitle( "JList事件处理 ");
setSize(400,300);
setVisible(true);
}
public static void main(String []args)
{
JListPerform lp=new JListPerform();
}
}
class Perform implements ListSelectionListener
{
JListPerform obj;
Perform(JListPerform obj1)
{
this.obj=obj1;
}
public void ValueChanged(ListSelectionEvent e)
{
int temp;
String st= "你的选择是: ";
int []indexs=obj.lst.getSelectedIndices();
for(int i=0;i <indexs.length;i++)
{
temp=indexs[i];
switch(temp)
{
case 0:
obj.lbl.setText(st+ "长城 ");
break;
case 1:
obj.lbl.setText(st+ "桂林 ");
break;
case 2:
obj.lbl.setText(st+ "张家界 ");
break;
case 3:
obj.lbl.setText(st+ "庐山 ");
break;
case 4:
obj.lbl.setText(st+ "韶山 ");
break;
case 5:
obj.lbl.setText(st+ "青岛 ");
break;
case 6:
obj.lbl.setText(st+ "黄鹤楼 ");
break;
}
}
}
}