java实现红包要多少钱_Java实现发红包模拟
packagecom.cmk;import javax.swing.*;import java.awt.*;importjava.awt.event.KeyAdapter;importjava.awt.event.KeyEvent;importjava.awt.event.MouseAdapter;importjava.awt.event.MouseEvent;importjava.util.ArrayList;importjava.util.HashMap;/*** 红包的框架 RedPacketFrame
*
* AWT / Swing / JavaFX
**/
public abstract class RedPacketFrame extendsJFrame {private static final long serialVersionUID = 1L;private static final String DIR = "E:\\mycode\\Hongbao\\pic";private ArrayList moneyList = null;private static int initMoney = 0;private static int totalMoney = 0; //单位为“分”
private static int count = 0;private static HashMap panelLable = new HashMap<>();//设置字体
private static Font fontYaHei = new Font("微软雅黑", Font.BOLD, 20);private static Font msgFont = new Font("微软雅黑", Font.BOLD, 20);private static Font totalShowFont = new Font("微软雅黑", Font.BOLD, 40);private static Font nameFont = new Font("微软雅黑", Font.BOLD, 40);private static Font showNameFont = new Font("微软雅黑", Font.BOLD, 20);private static Font showMoneyFont = new Font("微软雅黑", Font.BOLD, 50);private static Font showResultFont = new Font("微软雅黑", Font.BOLD, 15);/*** 窗体大小 WIDTH:400 HEIGHT:600*/
private static final int FRAME_WIDTH = 416; //静态全局窗口大小
private static final int FRAME_HEIGHT = 650;private static JLayeredPane layeredPane = null;/// private static JPanel contentPane = null;
/*** page1:输入页面 - InputPanel . 组件和初始化!*/
private static JPanel inputPanel = newJPanel();//private static JTextField input_total = new JTextField("200");//测试用//private static JTextField input_count = new JTextField("3");//测试用
private static JTextField input_total = newJTextField();private static JTextField input_count = newJTextField();private static JTextField input_people = new JTextField("30");private static JTextField input_msg = new JTextField("恭喜发财 , 大吉大利");private static JTextField input_total_show = new JTextField("$ " +input_total.getText().trim());private static JLabel input_inMoney = new JLabel(); //不可见
private static JLabel input_bg_label = new JLabel(new ImageIcon(DIR + "\\01_input.jpg"));static{//设置位置
input_total.setBounds(200, 90, 150, 50);
input_count.setBounds(200, 215, 150, 50);
input_people.setBounds(90, 275, 25, 30);
input_msg.setBounds(180, 340, 200, 50);
input_total_show.setBounds(130, 430, 200, 80);
input_inMoney.setBounds(10, 535, 380, 65);
input_bg_label.setBounds(0, 0, 400, 600); //背景//设置字体
input_total.setFont(fontYaHei);
input_count.setFont(fontYaHei);
input_people.setFont(fontYaHei);
input_msg.setFont(msgFont);
input_msg.setForeground(new Color(255, 233, 38)); //字体颜色 为金色
input_total_show.setFont(totalShowFont);
input_inMoney.setFont(fontYaHei);//透明
input_people.setOpaque(false);
input_total_show.setOpaque(false);//编 辑 -- 不可编辑
input_people.setEditable(false);
input_total_show.setEditable(false);//边界 -- 无
input_total.setBorder(null);
input_count.setBorder(null);
input_people.setBorder(null);
input_msg.setBorder(null);
input_total_show.setBorder(null);
}/*** page2:打开页面 - openPanel . 组件和初始化!*/
private static JPanel openPanel = newJPanel();private static JTextField open_ownerName = new JTextField("谁谁谁");private static JLabel open_label = new JLabel(new ImageIcon(DIR + "\\02_open_2.gif"));private static JLabel open_bg_label = new JLabel(new ImageIcon(DIR + "\\02_open_1.jpg"));static{//设置 位置.
open_ownerName.setBounds(0, 110, 400, 50);
open_bg_label.setBounds(0, 0, 400, 620);
open_label.setBounds(102, 280, 200, 200);
open_ownerName.setHorizontalAlignment(JTextField.CENTER);//设置字体
open_ownerName.setFont(nameFont);
open_ownerName.setForeground(new Color(255, 200, 163)); //字体颜色 为金色//背景色//open_name.setOpaque(false);
open_ownerName.setBackground(new Color(219, 90, 68));//不可编辑
open_ownerName.setEditable(false);//边框
open_ownerName.setBorder(null);
}/*** page3:展示页面 - showPanel . 组件和初始化!*/
private static JPanel showPanel = newJPanel();private static JPanel showPanel2 = newJPanel();private static JScrollPane show_jsp = newJScrollPane(showPanel2);private static JLabel show_bg_label = new JLabel(new ImageIcon(DIR + "\\03_money_1.jpg"));private static JTextField show_name = new JTextField("用户名称");private static JTextField show_msg = new JTextField("祝福信息");private static JTextField show_money = new JTextField("99.99");private static JTextField show_result = new JTextField(count + "个红包共" + (totalMoney / 100.0) + "元,被抢光了");static{//分别设置水平和垂直滚动条自动出现//jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);//jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
/** 两部分 页面 . 1.本人获得的红包-- showPanel 2.别人获得的红包-- show_jsp*/show_name.setBounds(125, 180, 100, 30);
show_name.setOpaque(false);
show_name.setBorder(null);
show_name.setFont(showNameFont);
show_msg.setBounds(0, 220, 400, 30);
show_msg.setOpaque(false);
show_msg.setBorder(null);
show_msg.setFont(msgFont);
show_msg.setHorizontalAlignment(JTextField.CENTER);
show_money.setBounds(0, 270, 250, 40);
show_money.setOpaque(false);
show_money.setBorder(null);
show_money.setFont(showMoneyFont);
show_money.setForeground(new Color(255, 233, 38)); //字体颜色 为金色
show_money.setHorizontalAlignment(SwingConstants.RIGHT);
show_result.setBounds(10, 460, 400, 20);
show_result.setOpaque(false);
show_result.setBorder(null);
show_result.setFont(showResultFont);
show_result.setForeground(new Color(170, 170, 170)); //字体颜色 为灰色//设置 图片.
show_bg_label.setBounds(0, 0, 400, 500);
}static{//页面和 背景的对应关系.
panelLable.put(inputPanel, input_bg_label);
panelLable.put(openPanel, open_bg_label);
panelLable.put(showPanel, show_bg_label);
}private voidinit() {//层次面板-- 用于设置背景
layeredPane = this.getLayeredPane();//System.out.println("层次面板||" + layeredPane);//System.out.println(layeredPane);//初始化框架 -- logo 和基本设置
initFrame();//初始化 三个页面 -- 准备页面
initPanel();//2.添加 页面 --第一个页面, 输入 panel 设置到 页面上.
setPanel(inputPanel);//3.添加 监听
addListener();
}/*** 初始化框架 -- logo 和基本设置*/
private voidinitFrame() {//logo
this.setIconImage(Toolkit.getDefaultToolkit().getImage(DIR + "\\logo.gif"));//System.out.println("LOGO初始化...");//窗口设置
this.setSize(FRAME_WIDTH, FRAME_HEIGHT); //设置界面大小
this.setLocation(280, 30); //设置界面出现的位置
this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setLayout(null);//测试期 注释 拖 拽 , 运行放开//this.setResizable(false);
this.setVisible(true);
}/*** 初始化页面-- 准备三个页面*/
private voidinitPanel() {//System.out.println("页面初始化...");
initInputPanel();
initOpenPanel();
initShowPanel();
}private voidinitInputPanel() {
inputPanel.setLayout(null);
inputPanel.setBounds(0, -5, 400, 600);//this.add(bg_label);
inputPanel.add(input_total);
inputPanel.add(input_count);
inputPanel.add(input_people);
inputPanel.add(input_msg);
inputPanel.add(input_total_show);
inputPanel.add(input_inMoney);//System.out.println("输入页面||" + inputPanel);
}private voidinitOpenPanel() {
openPanel.setLayout(null);
openPanel.setBounds(0, 0, 400, 600);//this.add(bg_label);
openPanel.add(open_ownerName);
openPanel.add(open_label);//System.out.println("打开页面||" + openPanel);
}private voidinitShowPanel() {
showPanel.setLayout(null);
showPanel.setBounds(10, 10, 300, 600);//==============
showPanel.add(show_name);
showPanel.add(show_msg);
showPanel.add(show_money);
showPanel.add(show_result);//System.out.println("展示页面||" + showPanel);//====================================//showPanel2.setLayout(null);//showPanel2.setBounds(0, 500, 401, 300);
showPanel2.setPreferredSize(new Dimension(300, 1000));
showPanel2.setBackground(Color.white);
show_jsp.setBounds(0, 500, 400, 110);
}/*** 每次打开页面, 设置 panel的方法*/
private voidsetPanel(JPanel panel) {//移除当前页面
layeredPane.removeAll();//System.out.println("重新设置:新页面");//背景lable添加到layeredPane的默认层
layeredPane.add(panelLable.get(panel), JLayeredPane.DEFAULT_LAYER);//面板panel设置为透明
panel.setOpaque(false);//面板panel 添加到 layeredPane的modal层
layeredPane.add(panel, JLayeredPane.MODAL_LAYER);
}//private void setShowPanel(JPanel show) {//setPanel(show);//layeredPane.add(show_jsp, JLayeredPane.MODAL_LAYER);//
//}
/*** 设置组件的监听器*/
private voidaddListener() {
input_total.addKeyListener(newKeyAdapter() {
@Overridepublic voidkeyReleased(KeyEvent e) {//System.out.println(e);
String input_total_money =input_total.getText();
input_total_show.setText("$ " +input_total_money);
}
});
input_count.addKeyListener(newKeyAdapter() {
@Overridepublic voidkeyReleased(KeyEvent e) {//System.out.println(e);//System.out.println("个数:" + input_count.getText());
}
});
input_msg.addKeyListener(newKeyAdapter() {
@Overridepublic voidkeyReleased(KeyEvent e) {//System.out.println(e);//System.out.println("留言:" + input_msg.getText());
}
});
input_inMoney.addMouseListener(newMouseAdapter() {
@Overridepublic voidmouseClicked(MouseEvent e) {try{//获取页面的值.
totalMoney = (int) (Double.parseDouble(input_total.getText()) * 100); //转换成"分"
count =Integer.parseInt(input_count.getText());if (count > 30) {
JOptionPane.showMessageDialog(null, "红包个数不得超过30个", "红包个数有误", JOptionPane.INFORMATION_MESSAGE);return;
}
initMoney=totalMoney;
System.out.println("总金额:[" + totalMoney + "]分");
System.out.println("红包个数:[" + count + "]个");
input_inMoney.removeMouseListener(this);//System.out.println("跳转-->打开新页面");//设置群主名称
open_ownerName.setText(ownerName);//设置打开页面
setPanel(openPanel);
}catch(Exception e2) {
JOptionPane.showMessageDialog(null, "请输入正确【总金额】或【红包个数】", "输入信息有误", JOptionPane.ERROR_MESSAGE);
}
}
});//open_ownerName ,点击 [名称],触发的方法 , 提示如何设置群主名称.
open_ownerName.addMouseListener(newMouseAdapter() {
@Overridepublic voidmouseClicked(MouseEvent arg0) {
JOptionPane.showMessageDialog(null, "请通过【setOwnerName】方法设置群主名称", "群主名称未设置",
JOptionPane.QUESTION_MESSAGE);
}
});//open label , 点击 [开],触发的方法,提示如何设置打开方式.
open_label.addMouseListener(newMouseAdapter() {
@Overridepublic voidmouseClicked(MouseEvent e) {if (openWay == null) {
JOptionPane.showMessageDialog(null, "请通过【setOpenWay】方法设置打开方式", "打开方式未设置",
JOptionPane.QUESTION_MESSAGE);return;
}//System.out.println("跳转-->展示页面");
moneyList=openWay.divide(totalMoney, count);//System.out.println(moneyList);
/** showPanel 添加数据
**/show_name.setText(ownerName);
show_msg.setText(input_msg.getText());if (moneyList.size() > 0) {
show_money.setText(moneyList.get(moneyList.size()- 1) / 100.0 + "");
}
show_result.setText(count+ "个红包共" + (initMoney / 100.0) + "元,被抢光了");
open_label.removeMouseListener(this);
setPanel(showPanel);//添加数据
for (int i = 0; i < moneyList.size(); i++) {
JTextField tf= newJTextField();
tf.setBorder(null);
tf.setFont(showNameFont);
tf.setHorizontalAlignment(JTextField.LEFT);if (i == moneyList.size() - 1) {
tf.setText(ownerName+ ":\t" + moneyList.get(i) / 100.0 + "元");
}else{
tf.setText("群成员-" + i + ":\t" + moneyList.get(i) / 100.0 + "元");
}
showPanel2.add(tf);
}
layeredPane.add(show_jsp, JLayeredPane.MODAL_LAYER);
}
});
}/*======================================================================
* **********************************************************************
* * 以上代码均为页面部分处理,包括布局/互动/跳转/显示等,大家 *
* * *
* * *
* **********************************************************************
* ======================================================================*/
/*** ownerName : 群主名称*/
private String ownerName = "谁谁谁"; //群主名称
/*** openWay : 红包的类型 [普通红包/手气红包]*/
private OpenMode openWay = null;/*** 构造方法:生成红包界面。
*
*@paramtitle 界面的标题*/
publicRedPacketFrame(String title) {super(title);//页面相关的初始化
init();
}public voidsetOwnerName(String ownerName) {this.ownerName =ownerName;
}public voidsetOpenWay(OpenMode openWay) {this.openWay =openWay;
}
}
java实现红包要多少钱_Java实现发红包模拟相关推荐
- java实现红包要多少钱_Java实现发红包功能
Java发红包案例,供大家参考,具体内容如下 首先我们需要分析这个代码的架构是什么,需要什么类组成等. 我们需要建立4个类,这4个类分别是用户类.群主类.成员类,运行类: 因为群主类和成员类都有共同的 ...
- java实现红包要多少钱_java实现微信红包分配算法
本文由我司收集整编,推荐下载,如有疑问,请与我司联系 java 实现微信红包分配算法 2017/03/25 0 红包算法分析有人认为,抢红包的额度是从 0.01 到剩余平均值 *N(N 是一个系数,决 ...
- java实现红包要多少钱_java实现微信红包分配算法(示例代码)
红包算法分析 有人认为,抢红包的额度是从0.01到剩余平均值*N(N是一个系数,决定最大的红包值)之间,比如一共发了10块钱,发了10个红包:第一个人可以拿到(0.01~1*N)之间的一个红包值,当然 ...
- php如何把红包放地图上,PHP实现发红包程序
我们先来分析下规律. 设定总金额为10元,有N个人随机领取: N=1 第一个 则红包金额=X元: N=2 第二个 为保证第二个红包可以正常发出,第一个红包金额=0.01至9.99之间的某个随机数. 第 ...
- java实现红包要多少钱_JAVA实现简单抢红包算法(模拟真实抢红包)
闲来无事,最近项目需求要写出用户登录首页来发现金红包,没有限额.我就自己稍微计算了一下如果有限额该怎么写.觉得这样与微信红包差不多.等项目需求完成以后.正好来博客贴一下我自己写的拆红包算法.个人觉得这 ...
- java实现红包要多少钱_Java实现抢红包功能
本文实例为大家分享了Java实现抢红包功能的具体代码,供大家参考,具体内容如下 关键思想: 1.抢红包涉及多人并发操作,需要做好同步保证多线程运行结果正确. 2.由于同时在线人数大,从性能方面考虑,玩 ...
- Java实现微信、QQ等群主发红包实例(普通红包)
许久没有写blog 手有点生,但是准备开启常更模式了! 最近重拾Java知识,挑一些有趣的案例分享给大家.(拼手气红包案例) 分析: 首先,群主和成员都是微信.QQ等应用程序的用户,他们都可以归为用户 ...
- 继承练习:发红包案例实现(群主发红包、群成员接收红包)
//User package ExtendTest; /*群主发红包的功能实现(用到Extend): 父类:User(用户) 属性:name(姓名),money(总金额) 方法:无参构造和含参数(na ...
- 1088红包数字代表什么_微信发红包的数字含义?各种红包代表着什么,你有没有发错呢?...
微信发红包是现在常见的社交手段,不管是关系一般还是关系亲密,都可以用发红包这个方式表达自己的亲近,但是不同的关系下,可以发的红包数字也是不同的,有兴趣的小伙伴可以和喜辰乐嫁一起来了解一下. 我也是根据 ...
最新文章
- python绘图实例-Python——matplotlib基础绘图函数示例
- 构造数组MaxTree、环形单链表的约瑟夫问题等总结
- 状态压缩DP AcWing算法提高课 (详解)
- Java程序员须知:分布式微服务为什么很难?
- 音视频技术开发周刊 | 158
- Linux中使用异步 I/O 大大提高应用程序的性能
- JS如何监听动画结束
- LeetCode 267. 回文排列 II(回溯)
- 【clickhouse】ClickHouse表引擎 MergeTree 数据生命周期
- Amazon验证码机器算法识别
- 无法读取方案文档 ‘http://www.springframework.org/schema/beans/spring-beans-4.1.xsd‘问题解决
- tf.shape()和tf.reshape()
- 选择排序及其不稳定性介绍
- 《unity2021》如何改成中文
- MATLAB数字图像处理模块在哪,MATLAB在数字图像处理中的应用
- endnote软件X9下载安装
- 为什么Java编程语言用一种咖啡名做名字
- APP微信支付java demo
- 微信公众平台 个人微信号订阅号发图文文章 总显示发送失败
- 用户体验五要素_什么是用户体验五要素?
热门文章
- svg大小自适应_网格自适应的 2 种方法——实现更高效的计算
- QML 编程之旅 -- QML程序的基本结构概念
- uva1507(状态压缩+dfs)
- UOJ #214 [UNR #1]合唱队形 (概率期望计数、DP、Min-Max容斥)
- windows拾取像素坐标_窗口坐标获取 windows
- Mysql学习(三)之数据库管理工具Navicat
- GeoHash -------寻找附近人
- C语言——反弹球游戏(第三阶段
- POJ2942 Knights of the Round Table 点双连通分量 二分图判定
- DPM(Deformable Parts Model)--原理(一)