/*ch0708.java*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;      //ListSelectionListener属于这个包

public class ch0708 extends JFrame implements ListSelectionListener
{ String str[]={"Java语言","C语言","PowerBuilder","SQl Sever","JBuilder"};
  JComboBox cb=new JComboBox(str);
  JList list=new JList(str);
  JPanel jp1=new JPanel();
  JPanel jp2=new JPanel();
  JLabel lb=new JLabel();
  public ch0708()
  { super("列表框应用实例");
    Container c=getContentPane( );
    cb.insertItemAt("选项"+cb.getItemCount( )+"个",0);
    cb.setEditable(true);
    jp1.add(cb);
    c.add(jp1,BorderLayout.CENTER);
    list.setBackground(Color.yellow);
    list.setSelectionBackground(Color.red);  //设置选项的背景色
    list.setSelectionForeground(Color.white);
    list.setSelectionMode(1);                //设置列表可以按住shift键后多选
    list.addListSelectionListener(this);
    jp2.add(lb);
    jp2.add(list);
    c.add(jp2,BorderLayout.SOUTH);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(300,200);
    setVisible(true);
  }
  public void valueChanged(ListSelectionEvent e)
  { int[] index=list.getSelectedIndices();
    for(int i=0;i<index.length;i++)
    { lb.setText("你选择学习的语言是："+str[index[i]]+" ");
    }
  }
  public static void main(String args[])
  { new ch0708();
  }
}

