用swing编写一个java抽奖的程序,至少有5个按钮,显得高大上一点。

用swing编写一个java抽奖的程序,至少有5个按钮,显得高大上一点。,第1张

‍

import javaawtColor;//界面不是很高大上档次,但是能满足你的基本需求,希望对你有用

import javaawtFont;

import javaawtGridLayout;

import javaawteventActionEvent;

import javaawteventActionListener;

import javaxswingJButton;

import javaxswingJFrame;

import javaxswingJOptionPane;

import javaxswingborderBorder;

import javaxswingborderLineBorder;

public class 抽奖游戏 extends JFrame {

 static 抽奖游戏 frame;

 public 抽奖游戏() {

  setLayout(new GridLayout(3, 3, 3, 3));

  Border border = new LineBorder(ColorBLUE, 4);

  JButton l1 = new JButton();

  JButton l2 = new JButton();

  JButton l3 = new JButton();

  JButton l4 = new JButton();

  JButton l5 = new JButton();

  JButton l6 = new JButton();

  JButton l7 = new JButton("祝你好运");

  JButton l8 = new JButton("中奖规则");

  JButton l9 = new JButton("再次抽奖");

  l1setBorder(border);

  l2setBorder(border);

  l3setBorder(border);

  l4setBorder(border);

  l5setBorder(border);

  l6setBorder(border);

  l7setBorder(border);

  l8setBorder(border);

  l9setBorder(border);

  String[] num2 = { "1", "2", "3", "4", "5", "6" };

  l8addActionListener(new ActionListener() {

   public void actionPerformed(ActionEvent arg0) {

    // TODO Auto-generated method stub

    JOptionPaneshowMessageDialog(null, "游戏规则:\n" + "特等奖:\n"

      + "第一行或者第二行出现的数字全部相同\n"

      + "第一第二行出现升序的1,2,3,4,5,6或者降序的6,5,4,3,2,1\n" + "一等奖\n"

      + "1:第一行或第二行的数字降序排列或者升序排列\n"

      + "2:第一行或者第二行相邻的按钮出现一样的数字\n");

   }

  });

  l9addActionListener(new ActionListener() {

   @Override

   public void actionPerformed(ActionEvent e) {

    // TODO Auto-generated method stub

    l1setText("");

    l2setText("");

    l3setText("");

    l4setText("");

    l5setText("");

    l6setText("");

    l7setBackground(ColorlightGray);

    l7setText("离中奖不远了!");

   }

  });

  l6addActionListener(new ActionListener() {

   public void actionPerformed(ActionEvent arg0) {

    // TODO Auto-generated method stub

    int j1 = (int) (Mathrandom()  10) % 6;

    l1setText(num2[j1]);

    int j2 = (int) (Mathrandom()  10) % 6;

    l2setText(num2[j2]);

    int j3 = (int) (Mathrandom()  10) % 6;

    l3setText(num2[j3]);

    int j4 = (int) (Mathrandom()  10) % 6;

    l4setText(num2[j4]);

    int j5 = (int) (Mathrandom()  10) % 6;

    l5setText(num2[j5]);

    int j6 = (int) (Mathrandom()  10) % 6;

    l6setText(num2[j6]);

    if (j1 == j2 && j2 == j3 || j4 == j5 && j5 == j6 || j1 == 1

      && j2 == 2 && j3 == 3 && j4 == 4 && j5 == 5 && j6 == 6

      || j1 == 6 && j2 == 5 && j3 == 4 && j4 == 3 && j5 == 2

      && j6 == 1) {

     l7setBackground(Colorred);

     l7setText("特等奖");

    } else if (j1 > j2 && j2 > j3 || j3 > j2 && j2 > j1 || j4 > j5

      && j5 > j6 || j6 > j5 && j5 > j4 || j1 == j2

      && j5 == j4 || j1 == j2 && j5 == j6 || j3 == j2

      && j4 == j5 || j5 == j6 && j2 == j3) {

     l7setBackground(ColorYELLOW);

     l7setText("一等奖");

    } else {

     l7setBackground(ColorlightGray);

     l7setText("未中奖,加油!");

    }

   }

  });

  l5addActionListener(new ActionListener() {

   public void actionPerformed(ActionEvent arg0) {

    // TODO Auto-generated method stub

    int j1 = (int) (Mathrandom()  10) % 6;

    l1setText(num2[j1]);

    int j2 = (int) (Mathrandom()  10) % 6;

    l2setText(num2[j2]);

    int j3 = (int) (Mathrandom()  10) % 6;

    l3setText(num2[j3]);

    int j4 = (int) (Mathrandom()  10) % 6;

    l4setText(num2[j4]);

    int j5 = (int) (Mathrandom()  10) % 6;

    l5setText(num2[j5]);

    int j6 = (int) (Mathrandom()  10) % 6;

    l6setText(num2[j6]);

    if (j1 == j2 && j2 == j3 || j4 == j5 && j5 == j6 || j1 == 1

      && j2 == 2 && j3 == 3 && j4 == 4 && j5 == 5 && j6 == 6

      || j1 == 6 && j2 == 5 && j3 == 4 && j4 == 3 && j5 == 2

      && j6 == 1) {

     l7setBackground(Colorred);

     l7setText("特等奖");

    } else if (j1 > j2 && j2 > j3 || j3 > j2 && j2 > j1 || j4 > j5

      && j5 > j6 || j6 > j5 && j5 > j4 || j1 == j2

      && j5 == j4 || j1 == j2 && j5 == j6 || j3 == j2

      && j4 == j5 || j5 == j6 && j2 == j3) {

     l7setBackground(ColorYELLOW);

     l7setText("一等奖");

    } else {

     l7setBackground(ColorlightGray);

     l7setText("未中奖,加油!");

    }

   }

  });

  l4addActionListener(new ActionListener() {

   public void actionPerformed(ActionEvent arg0) {

    // TODO Auto-generated method stub

    int j1 = (int) (Mathrandom()  10) % 6;

    l1setText(num2[j1]);

    int j2 = (int) (Mathrandom()  10) % 6;

    l2setText(num2[j2]);

    int j3 = (int) (Mathrandom()  10) % 6;

    l3setText(num2[j3]);

    int j4 = (int) (Mathrandom()  10) % 6;

    l4setText(num2[j4]);

    int j5 = (int) (Mathrandom()  10) % 6;

    l5setText(num2[j5]);

    int j6 = (int) (Mathrandom()  10) % 6;

    l6setText(num2[j6]);

    if (j1 == j2 && j2 == j3 || j4 == j5 && j5 == j6 || j1 == 1

      && j2 == 2 && j3 == 3 && j4 == 4 && j5 == 5 && j6 == 6

      || j1 == 6 && j2 == 5 && j3 == 4 && j4 == 3 && j5 == 2

      && j6 == 1) {

     l7setBackground(Colorred);

     l7setText("特等奖");

    } else if (j1 > j2 && j2 > j3 || j3 > j2 && j2 > j1 || j4 > j5

      && j5 > j6 || j6 > j5 && j5 > j4 || j1 == j2

      && j5 == j4 || j1 == j2 && j5 == j6 || j3 == j2

      && j4 == j5 || j5 == j6 && j2 == j3) {

     l7setBackground(ColorYELLOW);

     l7setText("一等奖");

    } else {

     l7setBackground(ColorlightGray);

     l7setText("未中奖,加油!");

    }

   }

  });

  l3addActionListener(new ActionListener() {

   public void actionPerformed(ActionEvent arg0) {

    // TODO Auto-generated method stub

    int j1 = (int) (Mathrandom()  10) % 6;

    l1setText(num2[j1]);

    int j2 = (int) (Mathrandom()  10) % 6;

    l2setText(num2[j2]);

    int j3 = (int) (Mathrandom()  10) % 6;

    l3setText(num2[j3]);

    int j4 = (int) (Mathrandom()  10) % 6;

    l4setText(num2[j4]);

    int j5 = (int) (Mathrandom()  10) % 6;

    l5setText(num2[j5]);

    int j6 = (int) (Mathrandom()  10) % 6;

    l6setText(num2[j6]);

    if (j1 == j2 && j2 == j3 || j4 == j5 && j5 == j6 || j1 == 1

      && j2 == 2 && j3 == 3 && j4 == 4 && j5 == 5 && j6 == 6

      || j1 == 6 && j2 == 5 && j3 == 4 && j4 == 3 && j5 == 2

      && j6 == 1) {

     l7setBackground(Colorred);

     l7setText("特等奖");

    } else if (j1 > j2 && j2 > j3 || j3 > j2 && j2 > j1 || j4 > j5

      && j5 > j6 || j6 > j5 && j5 > j4 || j1 == j2

      && j5 == j4 || j1 == j2 && j5 == j6 || j3 == j2

      && j4 == j5 || j5 == j6 && j2 == j3) {

     l7setBackground(ColorYELLOW);

     l7setText("一等奖");

    } else {

     l7setBackground(ColorlightGray);

     l7setText("未中奖,加油!");

    }

   }

  });

  l2addActionListener(new ActionListener() {

   public void actionPerformed(ActionEvent arg0) {

    // TODO Auto-generated method stub

    int j1 = (int) (Mathrandom()  10) % 6;

    l1setText(num2[j1]);

    int j2 = (int) (Mathrandom()  10) % 6;

    l2setText(num2[j2]);

    int j3 = (int) (Mathrandom()  10) % 6;

    l3setText(num2[j3]);

    int j4 = (int) (Mathrandom()  10) % 6;

    l4setText(num2[j4]);

    int j5 = (int) (Mathrandom()  10) % 6;

    l5setText(num2[j5]);

    int j6 = (int) (Mathrandom()  10) % 6;

    l6setText(num2[j6]);

    if (j1 == j2 && j2 == j3 || j4 == j5 && j5 == j6 || j1 == 1

      && j2 == 2 && j3 == 3 && j4 == 4 && j5 == 5 && j6 == 6

      || j1 == 6 && j2 == 5 && j3 == 4 && j4 == 3 && j5 == 2

      && j6 == 1) {

     l7setBackground(Colorred);

     l7setText("特等奖");

    } else if (j1 > j2 && j2 > j3 || j3 > j2 && j2 > j1 || j4 > j5

      && j5 > j6 || j6 > j5 && j5 > j4 || j1 == j2

      && j5 == j4 || j1 == j2 && j5 == j6 || j3 == j2

      && j4 == j5 || j5 == j6 && j2 == j3) {

     l7setBackground(ColorYELLOW);

     l7setText("一等奖");

    } else {

     l7setBackground(ColorlightGray);

     l7setText("未中奖,加油!");

    }

   }

  });

  l1addActionListener(new ActionListener() {

   public void actionPerformed(ActionEvent arg0) {

    // TODO Auto-generated method stub

    int j1 = (int) (Mathrandom()  10) % 6;

    l1setText(num2[j1]);

    int j2 = (int) (Mathrandom()  10) % 6;

    l2setText(num2[j2]);

    int j3 = (int) (Mathrandom()  10) % 6;

    l3setText(num2[j3]);

    int j4 = (int) (Mathrandom()  10) % 6;

    l4setText(num2[j4]);

    int j5 = (int) (Mathrandom()  10) % 6;

    l5setText(num2[j5]);

    int j6 = (int) (Mathrandom()  10) % 6;

    l6setText(num2[j6]);

    if (j1 == j2 && j2 == j3 || j4 == j5 && j5 == j6 || j1 == 1

      && j2 == 2 && j3 == 3 && j4 == 4 && j5 == 5 && j6 == 6

      || j1 == 6 && j2 == 5 && j3 == 4 && j4 == 3 && j5 == 2

      && j6 == 1) {

     l7setBackground(Colorred);

     l7setText("特等奖");

    } else if (j1 > j2 && j2 > j3 || j3 > j2 && j2 > j1 || j4 > j5

      && j5 > j6 || j6 > j5 && j5 > j4 || j1 == j2

      && j5 == j4 || j1 == j2 && j5 == j6 || j3 == j2

      && j4 == j5 || j5 == j6 && j2 == j3) {

     l7setBackground(ColorYELLOW);

     l7setText("一等奖");

    } else {

     l7setBackground(ColorlightGray);

     l7setText("未中奖,加油!");

    }

   }

  });

  Font font = new Font("", FontBOLD, 20);

  l1setFont(font);

  l2setFont(font);

  l3setFont(font);

  l4setFont(font);

  l5setFont(font);

  l6setFont(font);

  l7setFont(font);

  l8setFont(font);

  l9setFont(font);

  add(l1);

  add(l2);

  add(l3);

  add(l4);

  add(l5);

  add(l6);

  add(l7);

  add(l8);

  add(l9);

 }

 public static void main(String[] args) {

  // TODO Auto-generated method stub

  frame = new 抽奖游戏();

  framesetTitle("抽奖大战");

  framesetSize(500, 500);

  framesetLocationRelativeTo(null);

  framesetVisible(true);

  framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

 }

}

Dim n As Integer

Dim a()

Private Sub Command1_Click()

    If n = 0 Then            '第一次点击时执行

        m = Val(Text1)

        ReDim a(1 To m)

        For i = 1 To m      '生成数组a,元素为1至m这m个数

            a(i) = i

        Next

        For i = 1 To m - 1             '随机打乱数组a中元素

            r = Int(Rnd  m + 1)

            tmp = a(i)

            a(i) = a(r)

            a(r) = tmp

        Next

    End If

    n = n + 1                 '计数点击次数

    If n > Val(Text1) Then    '次数超过指定次数,抽奖结束

        MsgBox "抽奖结束!"

        Exit Sub

    End If

    Text2 = a(n)              '从数组中抽出第n个数,由于数组中是随机排列,所以相当于生成一个随机数,并且不会重复

End Sub

只要在窗体中做二个命令按钮、一个标签和一个定时器对象,复制以下代码,就可以运行了:

Private Sub Command1_Click()

If Command1Caption = "开始" Then

Command1Caption = "暂停"

Timer1Enabled = True

Else

Command1Caption = "开始"

Timer1Enabled = False

End If

End Sub

Private Sub Command2_Click()

End

End Sub

Private Sub Form_Load()

Label1Caption = ""

Label1FontSize = 50

Timer1Interval = 50

Timer1Enabled = False

Command1Caption = "开始"

Command2Caption = "结束"

End Sub

Private Sub Timer1_Timer()

Label1Caption = Int(Rnd 50) + 1

End Sub

public class Lucky {

public static void main(String[] args){

Systemoutprintln("请输入您的4位会员卡号:");

Scanner sc = new Scanner(Systemin);

int number = scnextInt(); //接收用户从控制台输入的会员卡号,并保存在会员卡号变量中

int a = number/1000; //千位

int b = number%1000/100; //百位

int c = number%100/10; //十位

int d = number%10; //个位

if((a+b+c+d)>20){

Systemoutprintln("恭喜中奖!您是幸运客户");

}else{

Systemoutprintln("谢谢参与!");

}

}

}

最基础的 没有异常判断 无限循环输入什么东西

我用多线程写的,你可以参考下。

系统界面图如下:(注:重置功能为清除已经中奖号码,回归1-100都可以中奖状态。)

代码:

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        SystemThreadingThread g_Thread = null;

        List<int> g_list = new List<int>();

        int g_Num = 0;

        delegate void g_delegate();

        /// <summary>

        /// 开始

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void button2_Click(object sender, EventArgs e)

        {

            if(g_listCount==100)

            {

                MessageBoxShow("所有数字已使用,请重置!");

                return;

            }

            button3Enabled = true;

            Random r = new Random();

            g_delegate dele= ChangeTxt;

            g_Thread = new SystemThreadingThread(() =>

            {

                while(thisIsDisposed==false)

                {

                    g_Num = rNext(1, 100);

                    if(g_listContains(g_Num))

                    {

                          g_Num =  1;

                    }

                    while(g_listContains(g_Num))

                    {

                        g_Num++;

                    }

                    thisInvoke(dele);

                    SystemThreadingThreadSleep(50);

                }

            });

            g_ThreadStart();

        }

        void ChangeTxt()

        {

            label2Text = g_NumToString();

        }

        /// <summary>

        /// 停止

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void button3_Click(object sender, EventArgs e)

        {

            //if(g_ThreadThreadState==SystemThreadingThreadStateRunning)

            {

                g_ThreadAbort();

                g_listAdd(ConvertToInt32(label2Text));

            }

            button3Enabled = false;

        }

        /// <summary>

        /// 重置

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void button1_Click(object sender, EventArgs e)

        {

            g_listClear();

        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)

        {

            SystemEnvironmentExit(0);

        }

    }

本文实例为大家分享了js抽奖程序的编写代码,以及编写注意事项,感兴趣的小伙伴们可以参考一下

代码:

<!DOCTYPE

html>

<html

lang="en">

<head>

<meta

charset="UTF-8">

<title>简单抽奖(可用键盘)</title>

<style>

{margin:0;padding:0;}

box{width:

400px;height:

300px;margin:50px

auto;background:

red}

title{color:

#fff;font-size:

30px;font-weight:700px;padding:

50px

0;text-align:

center;height:40px;}

btm{text-align:

center;padding:20px

0;}

btm

a{display:

inline-block;width:

120px;height:60px;line-height:

60px;background:

#FEF097;margin:0

10px;text-decoration:

none;}

</style>

<script>

var

data=['Iphone','Ipad','笔记本','相机','谢谢参与','充值卡','购物券'],

timer=null,//定时器

flag=0;//阻止多次回车

windowonload=function(){

var

play=documentgetElementById('play'),

stop=documentgetElementById('stop');

//

开始抽奖

playonclick=playFun;

stoponclick=stopFun;

//

键盘事件

documentonkeyup=function(event){

event

=

event

||

windowevent;

//

回车键的code值:13

if(eventkeyCode==13){

if(flag==0){

playFun();

flag=1;

}else{

stopFun();

flag=0;

}

}

}

function

playFun(){

var

title=documentgetElementById('title');

var

play=documentgetElementById('play');

clearInterval(timer);

timer=setInterval(function(){

var

random=Mathfloor(Mathrandom()datalength);

titleinnerHTML=data[random];

},60);

playstylebackground='#999';

}

function

stopFun(){

clearInterval(timer);

var

play=documentgetElementById('play');

playstylebackground='#FEF097';

}

}

</script>

</head>

<body>

<div

class="box">

<div

class="title"

id="title">淘家趣抽奖</div>

<div

class="btm">

<a

href="javascript:;"

id="play">开始</a>

<a

href="javascript:;"

id="stop">停止</a>

</div>

</div>

</body>

</html>

注意点:

1随机数,取数组的其中一个;取0-n之间:Mathrandom()(n+1)

2定时器,开始抽奖时要停止前面的一次抽奖,不然会定时器重叠

3按键 *** 作,要判断是抽奖进行中,还是未开始,所有设置了变量

flag

想要学习更多关于javascript抽奖功能,请参考此专题:javascript实现抽奖功能

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

以上就是关于用swing编写一个java抽奖的程序,至少有5个按钮,显得高大上一点。全部的内容,包括:用swing编写一个java抽奖的程序,至少有5个按钮,显得高大上一点。、VB编写一个抽奖小程序,要求每次抽奖的数字不重复、怎么用vb做一个抽奖程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/9525104.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-29
下一篇 2023-04-29

发表评论

登录后才能评论

评论列表(0条)

保存