乐透抽奖模拟

程序演示:输入五个抽奖数(quickpick复选框为快速生成,personal为自己编辑),点击play程序开始,中奖情况统计在下方,默认每周开一次大乐透,drawings表示开奖次数。

乐透抽奖模拟_第1张图片

源程序包括界面类文件和事件类文件。

界面类LottoMadness: 

import java.awt.*;
import javax.swing.*;

public class LottoMadness extends JFrame {
    LottoEvent lotto  = new LottoEvent(this);

    JPanel row1 = new JPanel();
    ButtonGroup option = new ButtonGroup();
    JCheckBox quickpick = new JCheckBox("quickpick",false);
    JCheckBox personal = new JCheckBox("personal",true);

    JPanel row2 = new JPanel();
    JLabel numbersLabel = new JLabel("Your picks : ", JLabel.RIGHT);
    JTextField[] numbers = new JTextField[6];
    JLabel winnersLabel = new JLabel("winners: " , JLabel.RIGHT);
    JTextField[] winners = new JTextField[6];

    JPanel row3 = new JPanel();
    JButton stop = new JButton("stop");
    JButton play = new JButton("play");
    JButton reset = new JButton("reset");

    JPanel row4 = new JPanel();
    JLabel got3Label = new JLabel("3 of 6 :",JLabel.RIGHT);
    JTextField got3 = new JTextField("0");
    JLabel got4Label = new JLabel("4 of 6 :",JLabel.RIGHT);
    JTextField got4 = new JTextField("0");
    JLabel got5Label = new JLabel("5 of 6 :",JLabel.RIGHT);
    JTextField got5 = new JTextField("0");
    JLabel got6Label = new JLabel("6 of 6 :",JLabel.RIGHT);
    JTextField got6 = new JTextField("0");
    JLabel drawingsLabel = new JLabel("drawings :",JLabel.RIGHT);
    JTextField drawings = new JTextField("0");
    JLabel yearsLabel = new JLabel("years :",JLabel.RIGHT);
    JTextField years = new JTextField("0");

    public LottoMadness(){
        super("Lotto Madness");
        setSize(550, 260);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GridLayout layout = new GridLayout(5,1,10,10);
        setLayout(layout);

        quickpick.addItemListener(lotto);
        personal.addItemListener(lotto);
        stop.addActionListener(lotto);
        play.addActionListener(lotto);
        reset.addActionListener(lotto);

        FlowLayout layout1 = new FlowLayout(FlowLayout.CENTER, 10, 10);
        option.add(quickpick);
        option.add(personal);
        row1.setLayout(layout1);
        row1.add(quickpick);
        row1.add(personal);
        add(row1);

        GridLayout layout2 = new GridLayout(2, 7 ,10, 10);
        row2.setLayout(layout2);
        row2.add(numbersLabel);
        for (int i =0 ;i<6; i++){
            numbers[i] = new JTextField();
            row2.add(numbers[i]);
        }
        row2.add(winnersLabel);
        for (int i=0; i<6; i++){
            winners[i]=new JTextField();
            winners[i].setEnabled(false);
            row2.add(winners[i]);
        }
        add(row2);

        FlowLayout layout3 = new FlowLayout(FlowLayout.CENTER, 10, 10);
        row3.setLayout(layout3);
        stop.setEnabled(false);
        row3.add(stop);
        row3.add(play);
        row3.add(reset);
        add(row3);

        GridLayout layout4 = new GridLayout(2, 3, 20, 10);
        row4.setLayout(layout4);
        row4.add(got3Label);
        got3.setEditable(false);
        row4.add(got3);
        row4.add(got4Label);
        got4.setEditable(false);
        row4.add(got4);
        row4.add(got5Label);
        got5.setEditable(false);
        row4.add(got5);
        row4.add(got6Label);
        got6.setEditable(false);
        row4.add(got6);
        row4.add(drawingsLabel);
        drawings.setEditable(false);
        row4.add(drawings);
        row4.add(yearsLabel);
        years.setEditable(false);
        row4.add(years);
        add(row4);

        setVisible(true);

    }

    public static void main (String[] arguments){
        LottoMadness frame = new LottoMadness();
    }

}

事件类LottoEvent:

import javax.swing.*;
import java.awt.event.*;

public class LottoEvent implements ItemListener, ActionListener, Runnable {
    LottoMadness gui;  //why no import LottoMadness;
    Thread playing;

    public LottoEvent(LottoMadness in){
        gui = in;
    }

    public void actionPerformed(ActionEvent event){
        String command = event.getActionCommand();
        if(command == "play"){
            startPlaying();
        }
        if(command == "stop"){
            stopPlaying();
        }
        if(command == "reset"){
            clearAllField();
        }
    }

    void startPlaying(){
        playing = new Thread(this);
        playing.start();
        gui.play.setEnabled(false);
        gui.stop.setEnabled(true);
        gui.reset.setEnabled(false);
        gui.quickpick.setEnabled(false);
        gui.personal.setEnabled(false);

    }

    void stopPlaying() {
        gui.stop.setEnabled(false);
        gui.play.setEnabled(true);
        gui.reset.setEnabled(true);
        gui.quickpick.setEnabled(true);
        gui.personal.setEnabled(true);
        playing = null;
    }

    void clearAllField() {
        for (int i = 0; i<6; i++){
            gui.numbers[i].setText(null);
            gui.winners[i].setText(null);

        }
        gui.got3.setText("0");
        gui.got4.setText("0");
        gui.got5.setText("0");
        gui.got6.setText("0");
        gui.drawings.setText("0");
        gui.years.setText("0");
    }

    public void itemStateChanged(ItemEvent event) {
        Object item = event.getItem();
        if(item ==gui.quickpick){
            for(int i=0; i<6; i++){
                int pick;
                do{
                    pick = (int)Math.floor(Math.random()*50 + 1);
                }while(numberGone(pick,gui.numbers,i)); //To guarantee the numbers are different;
                gui.numbers[i].setText(""+ pick);
            }
        }else{
            for(int i = 0; i<6; i++){
                gui.numbers[i].setText(null);  //the differences between "" and null here;
            }
        }
    }

    void addOneToField(JTextField field){
        int num = Integer.parseInt("0"+field.getText());
        num++;
        field.setText(""+num);

    }

    boolean numberGone(int num, JTextField[] pastNums, int count){
        for(int i=0; i

 

 

你可能感兴趣的:(Java学习日记)