import cn.itcast.day11.red.OpenMode;
public class Bootstrap {
public static void main(String[] args) {
MyRed red = new MyRed("传智播客双元课程");
red.setOwnerName("王思聪");
OpenMode random = new RandomMode();
red.setOpenWay(random);
}
}
import cn.itcast.day11.red.RedPacketFrame;
public class MyRed extends RedPacketFrame {
public MyRed(String title) {
super(title);
}
}
import cn.itcast.day11.red.OpenMode;
import java.util.ArrayList;
public class NormalMode implements OpenMode {
@Override
public ArrayList<Integer> divide(final int totalMoney, final int totalCount) {
ArrayList<Integer> list = new ArrayList<>();
int avg = totalMoney / totalCount;
int mod = totalMoney % totalCount;
for (int i = 0; i < totalCount - 1; i++) {
list.add(avg);
}
list.add(avg + mod);
return list;
}
}
import cn.itcast.day11.red.OpenMode;
import java.util.ArrayList;
import java.util.Random;
public class RandomMode implements OpenMode {
@Override
public ArrayList<Integer> divide(final int totalMoney, final int totalCount) {
ArrayList<Integer> list = new ArrayList<>();
Random r = new Random();
int leftMoney = totalMoney;
int leftCount = totalCount;
for (int i = 0; i < totalCount - 1; i++) {
int money = r.nextInt(leftMoney / leftCount * 2) + 1;
list.add(money);
leftMoney -= money;
leftCount--;
}
list.add(leftMoney);
return list;
}
}
import java.util.ArrayList;
public interface OpenMode {
ArrayList<Integer> divide(int totalMoney, int totalCount);
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.HashMap;
public abstract class RedPacketFrame extends JFrame {
private static final long serialVersionUID = 1L;
private static final String DIR = "day11-code\\pic";
private ArrayList<Integer> moneyList = null;
private static int initMoney = 0;
private static int totalMoney = 0;
private static int count = 0;
private static HashMap<JPanel, JLabel> 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);
private static final int FRAME_WIDTH = 416;
private static final int FRAME_HEIGHT = 650;
private static JLayeredPane layeredPane = null;
private static JPanel inputPanel = new JPanel();
private static JTextField input_total = new JTextField();
private static JTextField input_count = new JTextField();
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);
}
private static JPanel openPanel = new JPanel();
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_ownerName.setBackground(new Color(219, 90, 68));
open_ownerName.setEditable(false);
open_ownerName.setBorder(null);
}
private static JPanel showPanel = new JPanel();
private static JPanel showPanel2 = new JPanel();
private static JScrollPane show_jsp = new JScrollPane(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 {
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 void init() {
layeredPane = this.getLayeredPane();
initFrame();
initPanel();
setPanel(inputPanel);
addListener();
}
private void initFrame() {
this.setIconImage(Toolkit.getDefaultToolkit().getImage(DIR + "\\logo.gif"));
this.setSize(FRAME_WIDTH, FRAME_HEIGHT);
this.setLocation(280, 30);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(null);
this.setVisible(true);
}
private void initPanel() {
initInputPanel();
initOpenPanel();
initShowPanel();
}
private void initInputPanel() {
inputPanel.setLayout(null);
inputPanel.setBounds(0, -5, 400, 600);
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);
}
private void initOpenPanel() {
openPanel.setLayout(null);
openPanel.setBounds(0, 0, 400, 600);
openPanel.add(open_ownerName);
openPanel.add(open_label);
}
private void initShowPanel() {
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);
showPanel2.setPreferredSize(new Dimension(300, 1000));
showPanel2.setBackground(Color.white);
show_jsp.setBounds(0, 500, 400, 110);
}
private void setPanel(JPanel panel) {
layeredPane.removeAll();
layeredPane.add(panelLable.get(panel), JLayeredPane.DEFAULT_LAYER);
panel.setOpaque(false);
layeredPane.add(panel, JLayeredPane.MODAL_LAYER);
}
private void addListener() {
input_total.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
String input_total_money = input_total.getText();
input_total_show.setText("$ " + input_total_money);
}
});
input_count.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
}
});
input_msg.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
}
});
input_inMoney.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(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);
open_ownerName.setText(ownerName);
setPanel(openPanel);
} catch (Exception e2) {
JOptionPane.showMessageDialog(null, "请输入正确【总金额】或【红包个数】", "输入信息有误", JOptionPane.ERROR_MESSAGE);
}
}
});
open_ownerName.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
JOptionPane.showMessageDialog(null, "请通过【setOwnerName】方法设置群主名称", "群主名称未设置",
JOptionPane.QUESTION_MESSAGE);
}
});
open_label.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (openWay == null) {
JOptionPane.showMessageDialog(null, "请通过【setOpenWay】方法设置打开方式", "打开方式未设置",
JOptionPane.QUESTION_MESSAGE);
return;
}
moneyList = openWay.divide(totalMoney, count);
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 = new JTextField();
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);
}
});
}
private String ownerName = "谁谁谁";
private OpenMode openWay = null;
public RedPacketFrame(String title) {
super(title);
init();
}
public void setOwnerName(String ownerName) {
this.ownerName = ownerName;
}
public void setOpenWay(OpenMode openWay) {
this.openWay = openWay;
}
}