设置了两个按钮button_start,button_stop
当点击button_start时开始滚动显示学生姓名,当点击button_stop时停止滚动。
若要实现滚动,需要无限循环 while (true),学生信息都放在一个数组中,这个数组的大小是确定的(常量),要实现无限循环时可以显示学生信息,
在while中生成一个随机数(随机数的取值范围最大值为数组大小),然后数组的下标为这个随机数。
在public void actionPerformed(ActionEvent e) 中
while(bool){
r=(int) (Math.random() * 100 % num); // Math.random()返回的是0.0到1.0之间的数
arrs=str[r].split(",");
sno=arrs[0];
sname=arrs[1];
text_sno.setText(sno);
text_sname.setText(sname);
if(e.getSource()==button_start){
bool=true;
}
if(e.getSource()==button_stop){
bool=false;
}
}
这样的话当点击button_stop时没有用,点击关闭按钮也没有用
解决办法用线程解决
程序为:Random