java组合框的事件有哪些_博为峰Java技术文章 ——JavaSE Swing列表框选取事件的处理...

博为峰小博老师:

在JList类中有addListSelectionListener()方法,可以检测用户是否对JList的选取有任何的改变。在ListSelectionListener接口中只定义一个方法,那就是valueChanged(ListSelectionEvente),所以必须实现这个方法,才能在用户改变选取值时取得用户最后的选取状态。

这个程序首先使用数组创建了一个列表框对象,然后再通过实现列表框的事件处理方法,让列表框中选项被选中时在标签组件中显现出来,其代码程序如下所示:

publicclassBWFimplementsListSelectionListener{

staticJListlist=null;

staticJLabellabel=null;

publicstaticvoidmain(String[]args) {

JFramejf=newJFrame("博为峰教育");

ContainercontentPane=jf.getContentPane();

ListModelmode=newDataModel();

label=newJLabel();

list=newJList(mode);

list.setVisibleRowCount(5);

list.setBorder(BorderFactory.createTitledBorder("电脑配件"));

list.addListSelectionListener(newBWF());

contentPane.add(label,BorderLayout.NORTH);

contentPane.add(newJScrollPane(list),BorderLayout.CENTER);

jf.pack();

jf.setVisible(true);

jf.addWindowListener(newWindowAdapter() {

publicvoidwindowClosing(WindowEvente) {

System.exit(0);

}

});

}

publicvoidvalueChanged(ListSelectionEvente) {

inttmp=0;

Stringstmp="您目前选取:";

int[]index=list.getSelectedIndices();//所选数据的序列号

for(inti=0;i

tmp=index[i];

stmp+=newDataModel().s[tmp]+" ";

}

label.setText(stmp);

}

}

classDataModelextendsAbstractListModel{

publicstaticString[]s={"主板","显示器","内存","CPU","硬盘","电源","键盘","鼠标","显卡"};

publicintgetSize() {

returns.length;

}

publicString getElementAt(intindex) {

return(index+1)+"."+s[index++];

}

}

8bec1a7e8e4e

8bec1a7e8e4e

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值