java抽奖(根据幸运数字一二三等奖)


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



public class Lottery extends JFrame {
    static JTextField textField;
    static JTextField textField_1;
    static int luck;

    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,20);//定义字体,并用构造方法初始化
        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, 97, 149, 100);//        textField.setBounds(10, 37, 149, 26);
        desktopPane.add(textField);
        textField.setColumns(30);
        textField.setFont(new Font("宋体",Font.BOLD,50));



        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 str = new ArrayList();
        textField.addKeyListener(new KeyListener() {//文本框键盘监听
            public void keyTyped(KeyEvent e) {}
            public void keyReleased(KeyEvent e) {}
            public void keyPressed(KeyEvent e) {//当出现回车按键时间,会处理文本框的字符串,将他们进行储存,添加到列表

                if(e.getKeyChar()!='\n')
                    return ;

                luck= Integer.parseInt(textField.getText());


                textField.setText("");
            }
        });

        btnNewButton.addActionListener(new ActionListener() {//按钮监听,输出随机生成的标号在字符串数组中的所对应下标的名字
            public void actionPerformed(ActionEvent e) {
                // TODO 自动生成的方法存根


                int n=Integer.parseInt(String.valueOf(Lottery.luck));

                String s2="一";
                String s3="二";
                String s4="三";
                String s5;
                int x1 = (int) (0+Math.random()* (10-0+0));
                int x2 = (int) (0+Math.random()* (10-0+0));
                int x3 = (int) (0+Math.random()* (10-0+0));

                // String s0 = str.get(x);
                if(n==x1&&n==x2&&n==x3){
                    s5=s2;
                    String s1 = "\t"+x1+"\t"+x2+"\t"+x3+"\n\n\n\n\n\n\t恭喜获得"+s5+"等奖!";
                    textArea.setText(s1);}
               else if((n==x1&&n==x3)||(n==x2&&n==x3)||(n==x2&&n==x1)){
                    s5=s3;
                    String s1 = "\t"+x1+"\t"+x2+"\t"+x3+"\n\n\n\n\n\n\t恭喜获得"+s5+"等奖!";
                    textArea.setText(s1);}
               else if(n==x1||n==x2||n==x3){
                    s5=s4;
                    String s1 = "\t"+x1+"\t"+x2+"\t"+x3+"\n\n\n\n\n\n\t恭喜获得"+s5+"等奖!";
                    textArea.setText(s1);
                }


                else {
                    String s1 = "\t"+x1+"\t"+x2+"\t"+x3+"\n\n\n\n\n\n\t恭喜没有获奖!";
                    textArea.setText(s1);}
                }

            })
        ;
    }
}

感谢!

写的非常菜,比较繁琐,欢迎指正!

效果图展示:

输入数字5

java抽奖(根据幸运数字一二三等奖)_第1张图片

随机生成数字 9 9 4

没有获奖

java抽奖(根据幸运数字一二三等奖)_第2张图片

 

 

这是作业要求:java抽奖(根据幸运数字一二三等奖)_第3张图片




 

你可能感兴趣的:(java,开发语言,后端)