博为峰小博老师:
在JList类中有addListSelectionListener()方法,可以检测用户是否对JList的选取有任何的改变。在ListSelectionListener接口中只定义一个方法,那就是valueChanged(ListSelectionEvente),所以必须实现这个方法,才能在用户改变选取值时取得用户最后的选取状态。
这个程序首先使用数组创建了一个列表框对象,然后再通过实现列表框的事件处理方法,让列表框中选项被选中时在标签组件中显现出来,其代码程序如下所示:
public class BWF implements ListSelectionListener{
static JList<String> list=null;
static JLabel label=null;
public static void main(String[] args) {
JFrame jf=new JFrame("