java利用数组随机抽取幸运观众
程序员文章站
2023-12-04 15:07:10
本文实例为大家分享了java利用数组随机抽取幸运观众的具体代码,供大家参考,具体内容如下
思想:
首先将所有观众姓名生成数组,然后获取数组元素的总数量,再在数组元素中随...
本文实例为大家分享了java利用数组随机抽取幸运观众的具体代码,供大家参考,具体内容如下
思想:
首先将所有观众姓名生成数组,然后获取数组元素的总数量,再在数组元素中随机抽取元素的下标,根据元素的下标得到幸运观众的名字。
import java.awt.borderlayout; import java.awt.eventqueue; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.border.emptyborder; import javax.swing.jtextfield; import javax.swing.jtextarea; import javax.swing.jscrollpane; import javax.swing.border.titledborder; import javax.swing.jbutton; import java.awt.event.keyadapter; import java.awt.event.keyevent; import java.awt.event.actionlistener; import java.awt.event.actionevent; import javax.swing.uimanager; import java.awt.color; public class arrayexample extends jframe { private jpanel contentpane; private jtextfield namefield; private jtextarea personnelarea; private jtextarea resultarea; /**@auther 葆宁 * launch the application. */ public static void main(string[] args) { try { uimanager .setlookandfeel("com.sun.java.swing.plaf.nimbus.nimbuslookandfeel"); } catch (throwable e) { e.printstacktrace(); } eventqueue.invokelater(new runnable() { public void run() { try { arrayexample frame = new arrayexample(); frame.setvisible(true); } catch (exception e) { e.printstacktrace(); } } }); } /** * create the frame. */ public arrayexample() { settitle("\u5229\u7528\u6570\u7ec4\u968f\u673a\u62bd\u53d6\u5e78\u8fd0\u89c2\u4f17"); setdefaultcloseoperation(jframe.exit_on_close); setbounds(100, 100, 498, 300); contentpane = new jpanel(); contentpane.setborder(new emptyborder(5, 5, 5, 5)); setcontentpane(contentpane); contentpane.setlayout(null); jpanel panel = new jpanel(); panel .setborder(new titledborder( null, "\u8f93\u5165\u5728\u573a\u89c2\u4f17\u59d3\u540d\u6309\u56de\u8f66", titledborder.leading, titledborder.top, null, new color(59, 59, 59))); panel.setbounds(10, 10, 174, 242); contentpane.add(panel); panel.setlayout(new borderlayout(0, 5)); namefield = new jtextfield(); namefield.addkeylistener(new keyadapter() { @override public void keypressed(keyevent e) { do_textfield_keypressed(e); } }); panel.add(namefield, borderlayout.north); namefield.setcolumns(10); jscrollpane scrollpane = new jscrollpane(); panel.add(scrollpane); personnelarea = new jtextarea(); personnelarea.seteditable(false); scrollpane.setviewportview(personnelarea); jpanel panel_1 = new jpanel(); panel_1.setborder(new titledborder(null, "\u9009\u53d6\u89c2\u4f17\u4eba\u5458\uff1a", titledborder.leading, titledborder.top, null, new color(59, 59, 59))); panel_1.setbounds(183, 10, 219, 242); contentpane.add(panel_1); panel_1.setlayout(new borderlayout(0, 0)); jscrollpane scrollpane_1 = new jscrollpane(); panel_1.add(scrollpane_1); resultarea = new jtextarea(); resultarea.seteditable(false); resultarea.setlinewrap(true); scrollpane_1.setviewportview(resultarea); jbutton button = new jbutton("\u62bd\u53d6"); button.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { do_button_actionperformed(e); } }); button.setbounds(407, 164, 63, 25); contentpane.add(button); jbutton button_1 = new jbutton("\u9000\u51fa"); button_1.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { do_button_1_actionperformed(e); } }); button_1.setbounds(407, 215, 63, 25); contentpane.add(button_1); } protected void do_textfield_keypressed(keyevent e) { if (e.getkeychar() != '\n')// 不是回车字符不做处理 return; string name = namefield.gettext(); if (name.isempty())// 如果文本框没有字符串不做处理 return; personnelarea.append(name + "\n");// 把输入人名与回车符添加到人员列表 namefield.selectall();// 选择文本框所有字符 } protected void do_button_actionperformed(actionevent e) { string perstring = personnelarea.gettext();// 获取人员列表文本 string[] personnelarray = perstring.split("\n{1,}");// 获取人员数组 int index = (int) (math.random() * personnelarray.length);// 生成随机数组索引 // 定义包含格式参数的中奖信息 string formatarg = "本次抽取观众人员:\n\t%1$s\n恭喜%1$s成为本次观众抽奖的大奖得主。" + "\n\n我们将为%1$s颁发:\n\t过期的酸奶二十箱。"; // 为中奖信息添加人员参数 string info = string.format(formatarg, personnelarray[index]); resultarea.settext(info);// 在文本域显示中间信息 } protected void do_button_1_actionperformed(actionevent e) { dispose(); } }
效果图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 人人网怎么删除好友 人人网删除好友教程
推荐阅读