摇号java代码实现 c语言摇号代码
以JAVA为平台实现摇号抽奖
import java.awt.GridLayout;
创新互联是一家专注于网站设计制作、成都网站制作与策划设计,禹王台网站建设哪家好?创新互联做网站,专注于网站建设10多年,网设计领域的专业建站公司;建站业务涵盖:禹王台等地区。禹王台做网站价格咨询:18982081108
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import java.util.*;
import java.io.FileReader;
import java.io.File;
// This example demonstrates the use of JButton, JTextField
// and JLabel.
public class LunarPhases implements ActionListener {
final static int NUM_IMAGES = 1000;
final static int START_INDEX = 0;
int REAL_NUM_IMAGES = 0;
ImageIcon[] images = new ImageIcon[NUM_IMAGES];
String[] imageNames = new String[NUM_IMAGES];
JPanel mainPanel, selectPanel, displayPanel, resultPanel;
JButton phaseChoice = null;
JLabel phaseIconLabel = null, phaseResult = null;
// Constructor
public LunarPhases() {
// Create the phase selection and display panels.
selectPanel = new JPanel();
displayPanel = new JPanel();
resultPanel = new JPanel();
// Add various widgets to the sub panels.
addWidgets();
// Create the main panel to contain the two sub panels.
mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(1, 3, 5, 5));
mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
// Add the select and display panels to the main panel.
mainPanel.add(selectPanel);
mainPanel.add(displayPanel);
mainPanel.add(resultPanel);
}
// Create and the widgets to select and display the phases of the moon.
private void addWidgets() {
// Get the images and put them into an array of ImageIcon.
File dir = new File("C:\\Program Files\\JavaSoft\\JDK1.3.1\\bin\\images");
File[] files = dir.listFiles();
String imageName = null;
String temp = null;
int j = 0;
for (int i = 0; i files.length; i++) {
if (!files[i].isDirectory()) {
imageName = "images/" + files[i].getName();
}
temp = imageName.substring(imageName.lastIndexOf(".") + 1, imageName.length());
if(!temp.equals("gif"))
{
continue;
}
URL iconURL = ClassLoader.getSystemResource(imageName);
ImageIcon icon = new ImageIcon(iconURL);
images[j] = icon;
imageNames[j] = imageName.substring(7,imageName.lastIndexOf("."));
j++;
}
REAL_NUM_IMAGES = j;
// Create label for displaying moon phase images and put a border around
// it.
phaseIconLabel = new JLabel();
phaseIconLabel.setHorizontalAlignment(JLabel.CENTER);
phaseIconLabel.setVerticalAlignment(JLabel.CENTER);
phaseIconLabel.setVerticalTextPosition(JLabel.CENTER);
phaseIconLabel.setHorizontalTextPosition(JLabel.CENTER);
phaseIconLabel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLoweredBevelBorder(), BorderFactory
.createEmptyBorder(5, 5, 5, 5)));
phaseIconLabel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createEmptyBorder(0, 0, 10, 0), phaseIconLabel
.getBorder()));
phaseResult = new JLabel();
// Create combo box with lunar phase choices.
phaseChoice = new JButton("开始/停止");
// Display the first image.
phaseIconLabel.setIcon(images[START_INDEX]);
phaseIconLabel.setText("");
// Add border around the select panel.
selectPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory
.createTitledBorder("Select Phase"), BorderFactory
.createEmptyBorder(5, 5, 5, 5)));
resultPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory
.createTitledBorder("Result Phase"), BorderFactory
.createEmptyBorder(5, 5, 5, 5)));
// Add border around the display panel.
displayPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory
.createTitledBorder("Display Phase"), BorderFactory
.createEmptyBorder(5, 5, 5, 5)));
// Add moon phases combo box to select panel and image label to
// displayPanel.
selectPanel.setLayout(new GridLayout(3,3));//这里原来是selectPanel.add(phaseChoice);
// displayPanel.add(phaseIconLabel);
// resultPanel.add(phaseResult);
selectPanel.add(new JLabel());
selectPanel.add(new JLabel());
selectPanel.add(new JLabel());
selectPanel.add(new JLabel());
selectPanel.add(phaseChoice);
selectPanel.add(new JLabel());
selectPanel.add(new JLabel());
selectPanel.add(new JLabel());
selectPanel.add(new JLabel());
resultPanel.setLayout(new BorderLayout());
displayPanel.add(phaseIconLabel);
resultPanel.add(phaseResult);
// Listen to events from combo box.
phaseChoice.addActionListener(this);
}
boolean run = false;
// Implementation of ActionListener interface.
public void actionPerformed(ActionEvent event) {
if(run){
run = false;
}
else{
run = true;
new Thread(){
public void run(){
while(run){
int a =(int)( Math.random()*REAL_NUM_IMAGES);
phaseIconLabel.setIcon(images[a]);
phaseResult.setText(imageNames[a]);
try{
Thread.sleep(100);
}
catch(Exception e){}
}
}
}.start();
}
}
// main method
public static void main(String[] args) {
// create a new instance of LunarPhases
LunarPhases phases = new LunarPhases();
// Create a frame and container for the panels.
JFrame lunarPhasesFrame = new JFrame("Lunar Phases");
// Set the look and feel.
try {
UIManager.setLookAndFeel(UIManager
.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) {
}
lunarPhasesFrame.setContentPane(phases.mainPanel);
// Exit when the window is closed.
lunarPhasesFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Show the converter.
lunarPhasesFrame.pack();
lunarPhasesFrame.setVisible(true);
}
}
用JAVA编写
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.BorderFactory;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
public class JNotePadUI extends JFrame {
private JMenuItem menuOpen;
private JMenuItem menuSave;
private JMenuItem menuSaveAs;
private JMenuItem menuClose;
private JMenu editMenu;
private JMenuItem menuCut;
private JMenuItem menuCopy;
private JMenuItem menuPaste;
private JMenuItem menuAbout;
private JTextArea textArea;
private JLabel stateBar;
private JFileChooser fileChooser;
private JPopupMenu popUpMenu;
public JNotePadUI() {
super("新建文本文件");
setUpUIComponent();
setUpEventListener();
setVisible(true);
}
private void setUpUIComponent() {
setSize(640, 480);
// 菜单栏
JMenuBar menuBar = new JMenuBar();
// 设置「文件」菜单
JMenu fileMenu = new JMenu("文件");
menuOpen = new JMenuItem("打开");
// 快捷键设置
menuOpen.setAccelerator(
KeyStroke.getKeyStroke(
KeyEvent.VK_O,
InputEvent.CTRL_MASK));
menuSave = new JMenuItem("保存");
menuSave.setAccelerator(
KeyStroke.getKeyStroke(
KeyEvent.VK_S,
InputEvent.CTRL_MASK));
menuSaveAs = new JMenuItem("另存为");
menuClose = new JMenuItem("关闭");
menuClose.setAccelerator(
KeyStroke.getKeyStroke(
KeyEvent.VK_Q,
InputEvent.CTRL_MASK));
fileMenu.add(menuOpen);
fileMenu.addSeparator(); // 分隔线
fileMenu.add(menuSave);
fileMenu.add(menuSaveAs);
fileMenu.addSeparator(); // 分隔线
fileMenu.add(menuClose);
// 设置「编辑」菜单
JMenu editMenu = new JMenu("编辑");
menuCut = new JMenuItem("剪切");
menuCut.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_X,
InputEvent.CTRL_MASK));
menuCopy = new JMenuItem("复制");
menuCopy.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_C,
InputEvent.CTRL_MASK));
menuPaste = new JMenuItem("粘贴");
menuPaste.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_V,
InputEvent.CTRL_MASK));
editMenu.add(menuCut);
editMenu.add(menuCopy);
editMenu.add(menuPaste);
// 设置「关于」菜单
JMenu aboutMenu = new JMenu("关于");
menuAbout = new JMenuItem("关于JNotePad");
aboutMenu.add(menuAbout);
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(aboutMenu);
setJMenuBar(menuBar);
// 文字编辑区域
textArea = new JTextArea();
textArea.setFont(new Font("宋体", Font.PLAIN, 16));
textArea.setLineWrap(true);
JScrollPane panel = new JScrollPane(textArea,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
Container contentPane = getContentPane();
contentPane.add(panel, BorderLayout.CENTER);
// 状态栏
stateBar = new JLabel("未修改");
stateBar.setHorizontalAlignment(SwingConstants.LEFT);
stateBar.setBorder(
BorderFactory.createEtchedBorder());
contentPane.add(stateBar, BorderLayout.SOUTH);
popUpMenu = editMenu.getPopupMenu();
fileChooser = new JFileChooser();
}
private void setUpEventListener() {
// 按下窗口关闭钮事件处理
addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
closeFile();
}
}
);
// 菜单 - 打开
menuOpen.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
openFile();
}
}
);
// 菜单 - 保存
menuSave.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
saveFile();
}
}
);
// 菜单 - 另存为
menuSaveAs.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
saveFileAs();
}
}
);
// 菜单 - 关闭文件
menuClose.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
closeFile();
}
}
);
// 菜单 - 剪切
menuCut.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
cut();
}
}
);
// 菜单 - 复制
menuCopy.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
copy();
}
}
);
// 菜单 - 粘贴
menuPaste.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
paste();
}
}
);
// 菜单 - 关于
menuAbout.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 显示对话框
JOptionPane.showOptionDialog(null,
"程序名称:\n JNotePad \n" +
"程序设计:\n \n" +
"简介:\n 一个简单的文字编辑器\n" +
" 可作为验收Java的实现对象\n" +
" 欢迎网友下载研究交流\n\n" +
" /",
"关于JNotePad",
JOptionPane.DEFAULT_OPTION,
JOptionPane.INFORMATION_MESSAGE,
null, null, null);
}
}
);
// 编辑区键盘事件
textArea.addKeyListener(
new KeyAdapter() {
public void keyTyped(KeyEvent e) {
processTextArea();
}
}
);
// 编辑区鼠标事件
textArea.addMouseListener(
new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
if(e.getButton() == MouseEvent.BUTTON3)
popUpMenu.show(editMenu, e.getX(), e.getY());
}
public void mouseClicked(MouseEvent e) {
if(e.getButton() == MouseEvent.BUTTON1)
popUpMenu.setVisible(false);
}
}
);
}
private void openFile() {
if(isCurrentFileSaved()) { // 文件是否为保存状态
open(); // 打开
}
else {
// 显示对话框
int option = JOptionPane.showConfirmDialog(
null, "文件已修改,是否保存?",
"保存文件?", JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE, null);
switch(option) {
// 确认文件保存
case JOptionPane.YES_OPTION:
saveFile(); // 保存文件
break;
// 放弃文件保存
case JOptionPane.NO_OPTION:
open();
break;
}
}
}
private boolean isCurrentFileSaved() {
if(stateBar.getText().equals("未修改")) {
return false;
}
else {
return true;
}
}
private void open() {
// fileChooser 是 JFileChooser 的实例
// 显示文件选取的对话框
int option = fileChooser.showDialog(null, null);
// 使用者按下确认键
if(option == JFileChooser.APPROVE_OPTION) {
try {
// 开启选取的文件
BufferedReader buf =
new BufferedReader(
new FileReader(
fileChooser.getSelectedFile()));
// 设定文件标题
setTitle(fileChooser.getSelectedFile().toString());
// 清除前一次文件
textArea.setText("");
// 设定状态栏
stateBar.setText("未修改");
// 取得系统相依的换行字符
String lineSeparator = System.getProperty("line.separator");
// 读取文件并附加至文字编辑区
String text;
while((text = buf.readLine()) != null) {
textArea.append(text);
textArea.append(lineSeparator);
}
buf.close();
}
catch(IOException e) {
JOptionPane.showMessageDialog(null, e.toString(),
"开启文件失败", JOptionPane.ERROR_MESSAGE);
}
}
}
private void saveFile() {
// 从标题栏取得文件名称
File file = new File(getTitle());
// 若指定的文件不存在
if(!file.exists()) {
// 执行另存为
saveFileAs();
}
else {
try {
// 开启指定的文件
BufferedWriter buf =
new BufferedWriter(
new FileWriter(file));
// 将文字编辑区的文字写入文件
buf.write(textArea.getText());
buf.close();
// 设定状态栏为未修改
stateBar.setText("未修改");
}
catch(IOException e) {
JOptionPane.showMessageDialog(null, e.toString(),
"写入文件失败", JOptionPane.ERROR_MESSAGE);
}
}
}
private void saveFileAs() {
// 显示文件对话框
int option = fileChooser.showSaveDialog(null);
// 如果确认选取文件
if(option == JFileChooser.APPROVE_OPTION) {
// 取得选择的文件
File file = fileChooser.getSelectedFile();
// 在标题栏上设定文件名称
setTitle(file.toString());
try {
// 建立文件
file.createNewFile();
// 进行文件保存
saveFile();
}
catch(IOException e) {
JOptionPane.showMessageDialog(null, e.toString(),
"无法建立新文件", JOptionPane.ERROR_MESSAGE);
}
}
}
private void closeFile() {
// 是否已保存文件
if(isCurrentFileSaved()) {
// 释放窗口资源,而后关闭程序
dispose();
}
else {
int option = JOptionPane.showConfirmDialog(
null, "文件已修改,是否保存?",
"保存文件?", JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE, null);
switch(option) {
case JOptionPane.YES_OPTION:
saveFile();
break;
case JOptionPane.NO_OPTION:
dispose();
}
}
}
private void cut() {
textArea.cut();
stateBar.setText("已修改");
popUpMenu.setVisible(false);
}
private void copy() {
textArea.copy();
popUpMenu.setVisible(false);
}
private void paste() {
textArea.paste();
stateBar.setText("已修改");
popUpMenu.setVisible(false);
}
private void processTextArea() {
stateBar.setText("已修改");
}
public static void main(String[] args) {
new JNotePadUI();
}
}
希望对你能有所帮助。
摇号抽奖软件 必须用Java编写
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class RandomTest extends JFrame implements Runnable {
JTextField tf = new JTextField();
public RandomTest() {
this.add(tf);
this.setSize(300,200);
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
// System.exit(0);
dispose();
}
});
}
public void run() {
String[] list = { "aaa", "123", "张三", "李四" };
int time = 100; //3秒钟出结果
Random rd = new Random(); // 随机数生成类
int i = 0;
while (time 0) {
try {
i = rd.nextInt(list.length);
tf.setText(list[i]);
Thread.sleep(10);
time --;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
tf.setText("获奖者:" + list[i]);
}
public static void main(String[] args) {
new Thread(new RandomTest()).start();
}
}
跪求:用Java设计抽奖程序
程序循环应该写在开始按钮的监听里,当按下开始按钮后,程序开始循环,当按停止按钮时,循环停止. 你可以用while循环, 条件是一个boolean型的值,当按开始值为true,当按停止值为false.
java语言实现一个摇号系统,但是可以内部设定中奖名单这个怎么实现?
1、简单控制台程序如下,如需界面需要自己加个。
package zhidao;
import java.util.HashSet;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
/**
* @author bufei
* @datetime 2020年8月31日15:54:11
*/
public class YaoHao {
public static void main(String[] args) {
String xian = "= = = = = = = = =";
// 起点 终点 奖个数 指定的号码个数
int start = 0, end = 0, prizeNum = 0, defaNum = 0;
System.out.println(xian);
System.out.println("欢迎使用xxx 抽奖系统!");
System.out.println(xian);
Scanner scanner = new Scanner(System.in);
System.out.println("请输入号码范围例如 1 10:");
start = scanner.nextInt();
end = scanner.nextInt();
System.out.println("请输入奖项个数:");
prizeNum = scanner.nextInt();
System.out.println("请输入指定中奖号码个数,不指定请输入0:");
defaNum = scanner.nextInt();
int[] defa = new int[defaNum];
if (defaNum != 0) {
System.out.println("请输入指定的中奖号码,空格隔开:");
for (int i = 0; i defaNum; i++) {
defa[i] = scanner.nextInt();
}
}
System.out.println(xian);
System.out.println("本次抽奖中奖号码为:");
for (int num : randomDraw(start, end, prizeNum, defa)) {
System.out.print(num+" ");
}
}
/**
* @param start 抽奖范围起点
* @param end 抽奖号码范围终点
* @param prizeNum 中奖号码个数
* @param defa 指定中奖号码
* @return
*/
public static SetInteger randomDraw(int start, int end, int prizeNum, int[] defa) {
SetInteger set = new HashSet();
// 未指定中奖号码
if (defa.length == 0) {
//随机抽 prizeNum 个奖
while (set.size() prizeNum) {
set.add(new Random().nextInt(end - start + 1) + start);
}
return set;
} else {
//指定了中奖号码
//把指定的号码加入进去
for (int num : defa) {
set.add(num);
}
//如果没有全部指定 则继续抽剩余的奖项
while (set.size() prizeNum - defa.length) {
set.add(new Random().nextInt(end - start + 1) + start);
}
return set;
}
}
}
2、运行效果如图
指定了中奖号码
未指定中奖号码
未指定中奖号码
当前题目:摇号java代码实现 c语言摇号代码
网页路径:http://azwzsj.com/article/dodeecc.html