Java實(shí)現(xiàn)簡單抽獎(jiǎng)功能界面
本文實(shí)例為大家分享了Java實(shí)現(xiàn)簡單抽獎(jiǎng)功能的具體代碼,供大家參考,具體內(nèi)容如下
要求:定義文本框添加姓名,將姓名存儲(chǔ)并且在界面中可見,點(diǎn)擊抽獎(jiǎng)按鈕進(jìn)行抽獎(jiǎng)并輸出最后的中獎(jiǎng)得主。
關(guān)于抽獎(jiǎng)當(dāng)然需要用到隨機(jī)數(shù)的生成函數(shù),在Java中Random 的使用合適比較簡單的;
有兩種不同的Random方法的使用,其中一種是Math中的random。
該方法生成的是0~1之間的浮點(diǎn)數(shù),如果要生成整數(shù)類型的數(shù)字,可以乘一個(gè)整數(shù),強(qiáng)制轉(zhuǎn)換為整數(shù)類型。
int n = (int)(Math.random()*x);
還有一個(gè)是Random 類,使用時(shí)間需要先定義相關(guān)對象,之后在用對象調(diào)用方法生成隨機(jī)數(shù)。例:
Random random = new Random(); int n = random.nextInt(50);
這里生成的數(shù)字是0~50之間的整數(shù),不包含50。
下面是總體代碼:
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JButton;
public class Lottery extends JFrame {
static JTextField textField;
static JTextField textField_1;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Lottery frame = new Lottery();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Lottery() {
Font fn = new Font("宋體",Font.BOLD,15);//定義字體,并用構(gòu)造方法初始化
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//定義窗口可關(guān)閉
setBounds(100, 100, 625, 328);//窗口大小和位置
getContentPane().setLayout(null);//絕對布局
JDesktopPane desktopPane = new JDesktopPane();//定義小窗口
//desktopPane.setToolTipText("輸入觀眾姓名按回車");
desktopPane.setBounds(24, 12, 171, 286);
getContentPane().add(desktopPane);//添加界面
JLabel lblNewLabel = new JLabel(" 輸入觀眾姓名按回車");//為上面的小窗口定義標(biāo)簽名稱
lblNewLabel.setBounds(0, 12, 171, 13);
desktopPane.add(lblNewLabel);
textField = new JTextField();//文本框
textField.setBounds(10, 37, 149, 26);
desktopPane.add(textField);
textField.setColumns(30);
List list = new List();//列表定義,用于存儲(chǔ)姓名
desktopPane.setLayer(list, 100);
list.setMultipleSelections(true);
list.setBounds(8, 69, 151, 169);
desktopPane.add(list);
JDesktopPane desktopPane_1 = new JDesktopPane();
desktopPane_1.setBounds(216, 12, 317, 286);
getContentPane().add(desktopPane_1);
JLabel lblNewLabel_1 = new JLabel("抽取觀眾成員");
lblNewLabel_1.setBounds(12, 12, 220, 19);
desktopPane_1.add(lblNewLabel_1);
JLabel label = new JLabel("本次抽取的觀眾成員為");
label.setBounds(12, 32, 275, 27);
desktopPane_1.add(label);
JTextArea textArea = new JTextArea(3,20);
textArea.setBounds(12, 82, 281, 192);
desktopPane_1.add(textArea);
textArea.setFont(fn);
JButton btnNewButton = new JButton("抽取");
btnNewButton.setBounds(543, 218, 70, 23);
getContentPane().add(btnNewButton);
int i=0;
ArrayList<String> str = new ArrayList<String>();
textField.addKeyListener(new KeyListener() {//文本框鍵盤監(jiān)聽
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
public void keyPressed(KeyEvent e) {//當(dāng)出現(xiàn)回車按鍵時(shí)間,會(huì)處理文本框的字符串,將他們進(jìn)行儲(chǔ)存,添加到列表
if(e.getKeyChar()!='\n')
return ;
String name = textField.getText();
if(name.isEmpty())
return ;
list.add(name+"\n");
str.add(name);
textField.setText("");
}
});
btnNewButton.addActionListener(new ActionListener() {//按鈕監(jiān)聽,輸出隨機(jī)生成的標(biāo)號(hào)在字符串?dāng)?shù)組中的所對應(yīng)下標(biāo)的名字
public void actionPerformed(ActionEvent e) {
// TODO 自動(dòng)生成的方法存根
int n = str.size();
int x = (int) (Math.random()*n);
String s0 = str.get(x);
String s1 = "\t\t\t"+s0+"\n恭喜"+s0+"成為本次觀眾抽獎(jiǎng)的大獎(jiǎng)得主。"+"\n\n我們將為"+s0+"頒發(fā):\n\t\t過期酸奶66箱。";
textArea.setText(s1);
}
});
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java實(shí)現(xiàn)抽獎(jiǎng)功能
- Java實(shí)現(xiàn)游戲抽獎(jiǎng)算法
- Java實(shí)現(xiàn)多用戶注冊登錄的幸運(yùn)抽獎(jiǎng)
- java實(shí)現(xiàn)砸金蛋抽獎(jiǎng)功能
- java實(shí)現(xiàn)抽獎(jiǎng)概率類
- 基于Java實(shí)現(xiàn)抽獎(jiǎng)系統(tǒng)
- 簡單實(shí)現(xiàn)java抽獎(jiǎng)系統(tǒng)
- Java抽獎(jiǎng)算法第二例
- 純java代碼實(shí)現(xiàn)抽獎(jiǎng)系統(tǒng)
- JAVA使用隨機(jī)數(shù)實(shí)現(xiàn)概率抽獎(jiǎng)
相關(guān)文章
Spring使用aop切面編程時(shí)要給那些類加注解的實(shí)例
在使用切面編程時(shí),通常需要為以下類或組件添加注解來標(biāo)識(shí)它們,以便 Spring 或其他切面框架能夠正確識(shí)別和處理它們,這篇文章主要介紹了Spring使用aop切面編程時(shí)要給那些類加注解,需要的朋友可以參考下2023-11-11
spring 使用RabbitMQ進(jìn)行消息傳遞的示例代碼
這篇文章主要介紹了spring 使用RabbitMQ進(jìn)行消息傳遞的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12
SpringCloud Config連接git與數(shù)據(jù)庫流程分析講解
springcloud config是一個(gè)解決分布式系統(tǒng)的配置管理方案。它包含了 client和server兩個(gè)部分,server端提供配置文件的存儲(chǔ)、以接口的形式將配置文件的內(nèi)容提供出去,client端通過接口獲取數(shù)據(jù)、并依據(jù)此數(shù)據(jù)初始化自己的應(yīng)用2022-12-12
IDEA自動(dòng)生成TestNG的testng.xml的插件方法
這篇文章主要介紹了IDEA自動(dòng)生成TestNG的testng.xml的插件方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
Java中ExecutorService和ThreadPoolExecutor運(yùn)行原理
本文主要介紹了Java中ExecutorService和ThreadPoolExecutor運(yùn)行原理,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
IDEA項(xiàng)目使用SpringBoot+MyBatis-Plus的方法
這篇文章主要介紹了IDEA項(xiàng)目使用SpringBoot+MyBatis-Plus的方法,本文分步驟通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
Spring Boot和Thymeleaf整合結(jié)合JPA實(shí)現(xiàn)分頁效果(實(shí)例代碼)
這篇文章主要介紹了Spring Boot和Thymeleaf整合結(jié)合JPA實(shí)現(xiàn)分頁效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02

