Java实现简单抽奖功能界面
本文实例为大家分享了Java实现简单抽奖功能的具体代码,供大家参考,具体内容如下
要求:定义文本框添加姓名,将姓名存储并且在界面中可见,点击抽奖按钮进行抽奖并输出最后的中奖得主。
关于抽奖当然需要用到随机数的生成函数,在Java中Random的使用合适比较简单的;
有两种不同的Random方法的使用,其中一种是Math中的random。
该方法生成的是0~1之间的浮点数,如果要生成整数类型的数字,可以乘一个整数,强制转换为整数类型。
intn=(int)(Math.random()*x);
还有一个是Random类,使用时间需要先定义相关对象,之后在用对象调用方法生成随机数。例:
Randomrandom=newRandom(); intn=random.nextInt(50);
这里生成的数字是0~50之间的整数,不包含50。
下面是总体代码:
importjava.awt.EventQueue; importjava.awt.Font; importjava.awt.List; importjava.awt.event.ActionEvent; importjava.awt.event.ActionListener; importjava.awt.event.KeyEvent; importjava.awt.event.KeyListener; importjava.util.ArrayList; importjava.util.Random; importjavax.swing.JDesktopPane; importjavax.swing.JFrame; importjavax.swing.JLabel; importjavax.swing.JTextArea; importjavax.swing.JTextField; importjavax.swing.JButton; publicclassLotteryextendsJFrame{ staticJTextFieldtextField; staticJTextFieldtextField_1; publicstaticvoidmain(String[]args){ EventQueue.invokeLater(newRunnable(){ publicvoidrun(){ try{ Lotteryframe=newLottery(); frame.setVisible(true); }catch(Exceptione){ e.printStackTrace(); } } }); } publicLottery(){ Fontfn=newFont("宋体",Font.BOLD,15);//定义字体,并用构造方法初始化 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//定义窗口可关闭 setBounds(100,100,625,328);//窗口大小和位置 getContentPane().setLayout(null);//绝对布局 JDesktopPanedesktopPane=newJDesktopPane();//定义小窗口 //desktopPane.setToolTipText("输入观众姓名按回车"); desktopPane.setBounds(24,12,171,286); getContentPane().add(desktopPane);//添加界面 JLabellblNewLabel=newJLabel("输入观众姓名按回车");//为上面的小窗口定义标签名称 lblNewLabel.setBounds(0,12,171,13); desktopPane.add(lblNewLabel); textField=newJTextField();//文本框 textField.setBounds(10,37,149,26); desktopPane.add(textField); textField.setColumns(30); Listlist=newList();//列表定义,用于存储姓名 desktopPane.setLayer(list,100); list.setMultipleSelections(true); list.setBounds(8,69,151,169); desktopPane.add(list); JDesktopPanedesktopPane_1=newJDesktopPane(); desktopPane_1.setBounds(216,12,317,286); getContentPane().add(desktopPane_1); JLabellblNewLabel_1=newJLabel("抽取观众成员"); lblNewLabel_1.setBounds(12,12,220,19); desktopPane_1.add(lblNewLabel_1); JLabellabel=newJLabel("本次抽取的观众成员为"); label.setBounds(12,32,275,27); desktopPane_1.add(label); JTextAreatextArea=newJTextArea(3,20); textArea.setBounds(12,82,281,192); desktopPane_1.add(textArea); textArea.setFont(fn); JButtonbtnNewButton=newJButton("抽取"); btnNewButton.setBounds(543,218,70,23); getContentPane().add(btnNewButton); inti=0; ArrayListstr=newArrayList (); textField.addKeyListener(newKeyListener(){//文本框键盘监听 publicvoidkeyTyped(KeyEvente){} publicvoidkeyReleased(KeyEvente){} publicvoidkeyPressed(KeyEvente){//当出现回车按键时间,会处理文本框的字符串,将他们进行储存,添加到列表 if(e.getKeyChar()!='\n') return; Stringname=textField.getText(); if(name.isEmpty()) return; list.add(name+"\n"); str.add(name); textField.setText(""); } }); btnNewButton.addActionListener(newActionListener(){//按钮监听,输出随机生成的标号在字符串数组中的所对应下标的名字 publicvoidactionPerformed(ActionEvente){ //TODO自动生成的方法存根 intn=str.size(); intx=(int)(Math.random()*n); Strings0=str.get(x); Strings1="\t\t\t"+s0+"\n恭喜"+s0+"成为本次观众抽奖的大奖得主。"+"\n\n我们将为"+s0+"颁发:\n\t\t过期酸奶66箱。"; textArea.setText(s1); } }); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。