java摇号_以JAVA为平台实现摇号抽奖

展开全部

import java.awt.GridLayout;

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));//这里32313133353236313431303231363533e78988e69d8331333262356232原来是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);

}

}

本回答由提问者推荐

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值