這篇文章給大家介紹利用Java編寫一個簡單的抽獎功能,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
要求:定義文本框添加姓名,將姓名存儲并且在界面中可見,點擊抽獎按鈕進行抽獎并輸出最后的中獎得主。
關于抽獎當然需要用到隨機數的生成函數,在Java中Random 的使用合適比較簡單的;
有兩種不同的Random方法的使用,其中一種是Math中的random。
該方法生成的是0~1之間的浮點數,如果要生成整數類型的數字,可以乘一個整數,強制轉換為整數類型。
int n = (int)(Math.random()*x);
還有一個是Random 類,使用時間需要先定義相關對象,之后在用對象調用方法生成隨機數。例:
Random random = new Random(); int n = random.nextInt(50);
這里生成的數字是0~50之間的整數,不包含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);//定義字體,并用構造方法初始化
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//定義窗口可關閉
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(" 輸入觀眾姓名按回車");//為上面的小窗口定義標簽名稱
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();//列表定義,用于存儲姓名
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() {//文本框鍵盤監聽
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
public void keyPressed(KeyEvent e) {//當出現回車按鍵時間,會處理文本框的字符串,將他們進行儲存,添加到列表
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() {//按鈕監聽,輸出隨機生成的標號在字符串數組中的所對應下標的名字
public void actionPerformed(ActionEvent e) {
// TODO 自動生成的方法存根
int n = str.size();
int x = (int) (Math.random()*n);
String s0 = str.get(x);
String s1 = "\t\t\t"+s0+"\n恭喜"+s0+"成為本次觀眾抽獎的大獎得主。"+"\n\n我們將為"+s0+"頒發:\n\t\t過期酸奶66箱。";
textArea.setText(s1);
}
});
}
}關于利用Java編寫一個簡單的抽獎功能就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。