import javax.swing.JButton;
import javax.swing.JApplet;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import javax.swing.SwingUtilities;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.Dimension;
import java.awt.Container;
import java.awt.Color;
import java.util.Random;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Lottery extends JApplet {
/** Initialization method that will be called after the applet is loaded
* into the browser.
*/
public void init() {
// TODO start asynchronous download of heavy resources创建事件线程接口
SwingUtilities.invokeLater (
new Runnable() {
public void run() {
createGUI();
}
} );
}
//创建Applet用户接口
public void createGUI() {
//建立容器
Container content=getContentPane();
content.setLayout(new GridLayout(0,1));//建立布局
//建立第一个面板及面板边框,面板中装入数字按钮
JPanel buttonPane=new JPanel();
buttonPane.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(Color.cyan,Color.blue),"您的彩票号!"));
int[] choices=getNumbers();//得到初始化的一系列数字
for(int i=0;i<numberCount;i++) {
luckyNumbers[i]=new Selection(choices[i]);
buttonPane.add(luckyNumbers[i]);
}
content.add(buttonPane);
//建立第二个面板,装入控制按钮
JPanel controlPane=new JPanel(new FlowLayout(FlowLayout.CENTER,5,10));
JButton button;
Dimension buttonSize=new Dimension(100,20);
controlPane.add(button=new JButton("重新买彩票!"));
button.setBorder(BorderFactory.createRaisedBevelBorder());
button.addActionListener(new HandleControlButton(PICK_LUCKY_NUMBERS));
button.setPreferredSize(buttonSize);
controlPane.add(button=new JButton("选择颜色!"));
button.setBorder(BorderFactory.createRaisedBevelBorder());
button.addActionListener(new HandleControlButton(COLOR));
button.setPreferredSize(buttonSize);
content.add(controlPane);
}
//生产随机数
static int[] getNumbers() {
int[] numbers=new int[numberCount];
int candidate=0;
for(int i=0;i<numberCount;i++) {
search:
for(;;) {
candidate=values[choice.nextInt(values.length)];
for(int j=0;j<i;j++) {
if(candidate==numbers[j]) {
continue search;
}
}
numbers[i]=candidate;
break;
}
}
return numbers;
}
//定义选择数字按钮的类,每个数字按钮都实现监听器接口
class Selection extends JButton implements ActionListener {
//定义结构
public Selection(int value) {
super(Integer.toString(value));
this.value=value;
setBackground(startColor);
setBorder(BorderFactory.createRaisedBevelBorder());
setPreferredSize(new Dimension(80,20));
addActionListener(this);
}
//处理选择按钮事件
public void actionPerformed(ActionEvent e) {
//改变当前按钮的值
int candidate=0;
for(;;) {
candidate=values[choice.nextInt(values.length)];
if(isCurrentSelection(candidate)) {
continue;
}
setValue(candidate);
return;
}
}
public void setValue(int value) {
setText(Integer.toString(value));
this.value=value;
}
boolean hasValue(int possible) {
return value==possible;
}
boolean isCurrentSelection(int possible) {
for(int i=0;i<numberCount;i++) {
if(luckyNumbers[i].hasValue(possible)) {
return true;
}
}
return false;
}
private int value;
}
//定义控制按钮的类
class HandleControlButton implements ActionListener {
//定义结构
public HandleControlButton(int buttonID) {
this.buttonID=buttonID;
}
//处理按钮的点击
public void actionPerformed(ActionEvent e) {
switch(buttonID) {
case PICK_LUCKY_NUMBERS:
int[] numbers=getNumbers();
for(int i=0;i<numberCount;i++) {
luckyNumbers[i].setValue(numbers[i]);
}
break;
case COLOR:
Color color=new Color(flipColor.getRGB()^luckyNumbers[0].getBackground().getRGB());
for(int i=0;i<numberCount;i++)
luckyNumbers[i].setBackground(color);
break;
}
}
private int buttonID;
}
final static int numberCount=6;
final static int minValue=1;
final static int maxValue=49;
static int[] values=new int[maxValue-minValue+1];
static {
for(int i=0;i<values.length;i++)
values[i]=i+minValue;
}
//定义一组选择数字的普通按钮
private Selection[] luckyNumbers = new Selection[numberCount];
final public static int PICK_LUCKY_NUMBERS=1; //选择按钮ID
final public static int COLOR=2; //按钮颜色
Color flipColor =new Color(Color.RED.getRGB()^Color.YELLOW.getRGB());
Color startColor=Color.RED;
private static Random choice=new Random();
//public void paint(Graphics g) {
}

hebopolo
- 粉丝: 16
最新资源
- python 练习题,python题目
- 【嵌入式系统】基于STM32单片机的按键控制LED闪烁程序:初学者快速上手指南
- 首个实现全参数训练的知识产权大模型 -MoZi(墨子)
- ADO.NET专业项目实战指南
- 一项基于大模型的App隐私开关探测技术
- 支持多情感男女声,实时离线文本合成 TTS,可单模变声、调速率音量及自定义语音模型
- 首个全参数训练的知识产权大模型 MoZi (墨子)
- 基于 Next.js 的大模型小说创作工具 AI-Novel
- mmexport1755910142185.mp4
- 基于 Next.js 的大模型小说创作工具 AI-Novel
- 【移动应用开发】多框架教程汇总:智慧林业IoT、Rhodes、Kivy、Android、Ionic4开发资源与入门指导
- 冰心3.9多开(推荐).apk
- 唯雨超自然-1.6.apk
- 大数据信息的处理模式与模型构建
- 基于 TinyVue 的前后端分离后台管理系统,支持在线配置菜单、路由、国际化及页签模式、多级菜单,模板丰富、构建工具多样,功能强大且开箱即用!
- CST联合Matlab仿真程序
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


