能帮忙用java做一个抽奖程序吗,就是简单的一,二,三等奖,特等奖。谢谢
展开全部
找了两组程序:
代码一:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class GoodLucky extends JFrame implements ActionListener
{
JTextField tf = new JTextField();
JButton b1 = new JButton("开始");
JButton b2 = new JButton("停止");
boolean sg=false;
public GoodLucky(){
b1.setActionCommand("start");
JPanel p = new JPanel();
p.add(b1);
p.add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
b2.setEnabled(false);
this.getContentPane().add(tf,"North");
this.getContentPane().add(p,"South");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300,200);
this.setLocation(300,300);
Cursor cu = new Cursor(Cursor.HAND_CURSOR);
this.setCursor(cu);
this.setVisible(true);
tf.setText("祝大家鸿运!");
this.go();
}
public void go()
{
while(true)
{
if(sg==true)
{
String s = " ";
for(int j = 1; j <= 7;j++)
{
int i = (int)(Math.random() * 9) + 1;
if(i < 10)
{
s = s + " 0" + i;
}
else
{
s = s + " " + i;
}
}
tf.setText(s);
}
try
{
Thread.sleep(100);
}
catch(java.lang.InterruptedException e)
{
e.printStackTrace();
}
}
}
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if(s.equals("start"))
{
sg = true;
b1.setEnabled(false);
b2.setEnabled(true);
}
else
{
sg= false;
b2.setEnabled(false);
b1.setEnabled(true);
}
}
public static void main(String[] args)
{
new GoodLucky();
}
}
代码二:
package com.softeem.lesson03;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class LuckyNumberInterface extends JFrame {
JLabel ltitle = new JLabel("欢迎使用本抽奖机", JLabel.CENTER);
JLabel linput = new JLabel("幸运号码:");
JTextField input = new JTextField(20);
JButton btnStart = new JButton("开始");
JButton btnStop = new JButton("停止");
JPanel lpanel = new JPanel();
JPanel btnPanel = new JPanel();
public LuckyNumberInterface() {
super("抽奖机");
final LuckyNumber ln = new LuckyNumber(input);
ln.start();
Container container = getContentPane();
lpanel.add(linput);
lpanel.add(input);
btnPanel.add(btnStart);
btnPanel.add(btnStop);
Font font = new Font("宋体", Font.BOLD, 16);
ltitle.setFont(font);
container.add(ltitle, BorderLayout.NORTH);
container.add(lpanel, BorderLayout.CENTER);
container.add(btnPanel, BorderLayout.SOUTH);
btnStart.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
ln.setFlag(true);
}
});
btnStop.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
ln.setFlag(false);
}
});
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400, 120);//设置窗口的显示大小
setLocation(400, 300);//设置窗口显示的位置
setVisible(true);//显示窗口
}
public static void main(String[] args) {
new LuckyNumberInterface();
}
}
package com.softeem.lesson03;
import java.text.DecimalFormat;
import java.util.Random;
import javax.swing.JTextField;
public class LuckyNumber extends Thread {
private String luckyNumber = "";
private boolean flag = false;//开关
private JTextField input;
public LuckyNumber(JTextField input) {
this.input = input;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
@Override
public void run() {
while (true) {
if (flag) {
generatorLuckyNumber();
}
}
}
public void generatorLuckyNumber() {
String[] firstNumber = { "130", "131", "134", "137", "135", "139",
"150", "151", "155", "158", "159" };
Random rd = new Random();
int number = rd.nextInt(10000);
DecimalFormat df = new DecimalFormat("0000");
String lastNumber = df.format(number);
luckyNumber = firstNumber[rd.nextInt(firstNumber.length)] + "****"
+ lastNumber;
input.setText("");
input.setText(luckyNumber);
}
}
代码一:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class GoodLucky extends JFrame implements ActionListener
{
JTextField tf = new JTextField();
JButton b1 = new JButton("开始");
JButton b2 = new JButton("停止");
boolean sg=false;
public GoodLucky(){
b1.setActionCommand("start");
JPanel p = new JPanel();
p.add(b1);
p.add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
b2.setEnabled(false);
this.getContentPane().add(tf,"North");
this.getContentPane().add(p,"South");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300,200);
this.setLocation(300,300);
Cursor cu = new Cursor(Cursor.HAND_CURSOR);
this.setCursor(cu);
this.setVisible(true);
tf.setText("祝大家鸿运!");
this.go();
}
public void go()
{
while(true)
{
if(sg==true)
{
String s = " ";
for(int j = 1; j <= 7;j++)
{
int i = (int)(Math.random() * 9) + 1;
if(i < 10)
{
s = s + " 0" + i;
}
else
{
s = s + " " + i;
}
}
tf.setText(s);
}
try
{
Thread.sleep(100);
}
catch(java.lang.InterruptedException e)
{
e.printStackTrace();
}
}
}
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if(s.equals("start"))
{
sg = true;
b1.setEnabled(false);
b2.setEnabled(true);
}
else
{
sg= false;
b2.setEnabled(false);
b1.setEnabled(true);
}
}
public static void main(String[] args)
{
new GoodLucky();
}
}
代码二:
package com.softeem.lesson03;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class LuckyNumberInterface extends JFrame {
JLabel ltitle = new JLabel("欢迎使用本抽奖机", JLabel.CENTER);
JLabel linput = new JLabel("幸运号码:");
JTextField input = new JTextField(20);
JButton btnStart = new JButton("开始");
JButton btnStop = new JButton("停止");
JPanel lpanel = new JPanel();
JPanel btnPanel = new JPanel();
public LuckyNumberInterface() {
super("抽奖机");
final LuckyNumber ln = new LuckyNumber(input);
ln.start();
Container container = getContentPane();
lpanel.add(linput);
lpanel.add(input);
btnPanel.add(btnStart);
btnPanel.add(btnStop);
Font font = new Font("宋体", Font.BOLD, 16);
ltitle.setFont(font);
container.add(ltitle, BorderLayout.NORTH);
container.add(lpanel, BorderLayout.CENTER);
container.add(btnPanel, BorderLayout.SOUTH);
btnStart.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
ln.setFlag(true);
}
});
btnStop.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
ln.setFlag(false);
}
});
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400, 120);//设置窗口的显示大小
setLocation(400, 300);//设置窗口显示的位置
setVisible(true);//显示窗口
}
public static void main(String[] args) {
new LuckyNumberInterface();
}
}
package com.softeem.lesson03;
import java.text.DecimalFormat;
import java.util.Random;
import javax.swing.JTextField;
public class LuckyNumber extends Thread {
private String luckyNumber = "";
private boolean flag = false;//开关
private JTextField input;
public LuckyNumber(JTextField input) {
this.input = input;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
@Override
public void run() {
while (true) {
if (flag) {
generatorLuckyNumber();
}
}
}
public void generatorLuckyNumber() {
String[] firstNumber = { "130", "131", "134", "137", "135", "139",
"150", "151", "155", "158", "159" };
Random rd = new Random();
int number = rd.nextInt(10000);
DecimalFormat df = new DecimalFormat("0000");
String lastNumber = df.format(number);
luckyNumber = firstNumber[rd.nextInt(firstNumber.length)] + "****"
+ lastNumber;
input.setText("");
input.setText(luckyNumber);
}
}
哎呦互动
2024-07-12 广告
2024-07-12 广告
现场大屏幕互动软件产品开发欢迎咨询上海爱友信息科技有限公司,公司成立于2016年,是国内最早的一批H5软件开发商之一。公司经过多年的自主研发并不断迭代的互动产品“哎呦互动”,已经获得数十项软件著作权和多项软件专利,并积累了众多的品牌客户和无...
点击进入详情页
本回答由哎呦互动提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询