当前位置:网站首页>Redpacketframe and openmode packages

Redpacketframe and openmode packages

2022-06-24 17:47:00 How about silence

package demo08;

import java.util.ArrayList;

public interface OpenMode {
    /**
     *  Please put totalMoney Divide into count Share , Save to ArrayList<Integer> in , Return to .
     *
     * @param totalMoney             The total amount is for the convenience of calculation , Has been converted to an integer , In minutes .
     * @param totalCount             Number of red packets 
     * @return ArrayList<Integer>	 The element is the amount value of each red envelope , The cumulative sum of the values of all elements is equal to the total amount .
     */
    ArrayList<Integer> divide(int totalMoney, int totalCount);
}
package demo09;


import demo08.OpenMode;

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;

/**
 *  The framework of the red envelope  RedPacketFrame
 *
 * AWT / Swing / JavaFX
 *
 * @author  Is not my 
 */
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; //  Unit is “ branch ”
    private static int count = 0;

    private static HashMap<JPanel, JLabel> panelLable = new HashMap<>();

    //  Set the font 
    private static Font fontYaHei = new Font(" Microsoft YaHei ", Font.BOLD, 20);
    private static Font msgFont = new Font(" Microsoft YaHei ", Font.BOLD, 20);
    private static Font totalShowFont = new Font(" Microsoft YaHei ", Font.BOLD, 40);
    private static Font nameFont = new Font(" Microsoft YaHei ", Font.BOLD, 40);
    private static Font showNameFont = new Font(" Microsoft YaHei ", Font.BOLD, 20);
    private static Font showMoneyFont = new Font(" Microsoft YaHei ", Font.BOLD, 50);
    private static Font showResultFont = new Font(" Microsoft YaHei ", Font.BOLD, 15);

    /**
     *  Form size  WIDTH:400 HEIGHT:600
     */
    private static final int FRAME_WIDTH = 416; //  Static global window size 
    private static final int FRAME_HEIGHT = 650;
    private static JLayeredPane layeredPane = null;

    /// private static JPanel contentPane = null;

    /**
     * page1: Input page  - InputPanel .  Components and initialization !
     */
    private static JPanel inputPanel = new JPanel();

    // private static JTextField input_total = new JTextField("200"); //  Test use 
    // private static JTextField input_count = new JTextField("3"); //  Test use 
    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(" May you be happy and prosperous   ,   prosperous ");
    private static JTextField input_total_show = new JTextField("$ " + input_total.getText().trim());
    private static JLabel input_inMoney = new JLabel(); //  invisible 
    private static JLabel input_bg_label = new JLabel(new ImageIcon(DIR + "\\01_input.jpg"));

    static {

        //  Set location 
        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); //  background 

        //  Set the font 
        input_total.setFont(fontYaHei);
        input_count.setFont(fontYaHei);
        input_people.setFont(fontYaHei);
        input_msg.setFont(msgFont);
        input_msg.setForeground(new Color(255, 233, 38)); //  The font color   For gold 
        input_total_show.setFont(totalShowFont);
        input_inMoney.setFont(fontYaHei);

        //  transparent 
        input_people.setOpaque(false);
        input_total_show.setOpaque(false);
        //  Ed   Compilation  --  It can't be edited 
        input_people.setEditable(false);
        input_total_show.setEditable(false);

        //  The border  --  nothing 
        input_total.setBorder(null);
        input_count.setBorder(null);
        input_people.setBorder(null);
        input_msg.setBorder(null);
        input_total_show.setBorder(null);

    }

    /**
     * page2: Open the page  - openPanel .  Components and initialization !
     */
    private static JPanel openPanel = new JPanel();

    private static JTextField open_ownerName = new JTextField(" Who? Who ");
    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 {

        //  Set up   Location .
        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);

        //  Set the font 
        open_ownerName.setFont(nameFont);
        open_ownerName.setForeground(new Color(255, 200, 163)); //  The font color   For gold 

        //  Background color 
        // open_name.setOpaque(false);
        open_ownerName.setBackground(new Color(219, 90, 68));

        //  It can't be edited 
        open_ownerName.setEditable(false);
        //  Frame 
        open_ownerName.setBorder(null);

    }

    /**
     * page3: Display page  - showPanel .  Components and initialization !
     */
    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(" User name ");
    private static JTextField show_msg = new JTextField(" Blessing message ");
    private static JTextField show_money = new JTextField("99.99");
    private static JTextField show_result = new JTextField(count + " A total of... Red envelopes " + (totalMoney / 100.0) + " element , Robbed ");

    static {
        //  Set the horizontal and vertical scroll bars to appear automatically 
        // jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        // jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

        /*
         *  Two parts   page  . 1. The red envelope I got -- showPanel 2. Others get red envelopes -- 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)); //  The font color   For gold 
        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)); //  The font color   It's gray 

        //  Set up   picture .
        show_bg_label.setBounds(0, 0, 400, 500);

    }

    static {

        //  Page and   The correspondence of the background .
        panelLable.put(inputPanel, input_bg_label);
        panelLable.put(openPanel, open_bg_label);
        panelLable.put(showPanel, show_bg_label);
    }

    private void init() {
        //  Hierarchy panel --  Used to set the background 
        layeredPane = this.getLayeredPane();
//        System.out.println(" Hierarchy panel ||" + layeredPane);
        // System.out.println(layeredPane);

        //  Initialization framework  -- logo  And basic settings 
        initFrame();
        //  initialization   Three pages  --  Prepare page 
        initPanel();

        // 2. add to   page  -- The first page ,  Input  panel  Set to   On the page .
        setPanel(inputPanel);

        // 3. add to   monitor 
        addListener();
    }


    /**
     *  Initialization framework  -- logo  And basic settings 
     */
    private void initFrame() {
        // logo
        this.setIconImage(Toolkit.getDefaultToolkit().getImage(DIR + "\\logo.gif"));
//        System.out.println("LOGO initialization ...");

        //  Window settings 
        this.setSize(FRAME_WIDTH, FRAME_HEIGHT); //  Set the interface size 
        this.setLocation(280, 30); //  Set the position where the interface appears 
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLayout(null);

        //  Test period   notes   drag   ye  ,  Run release 
        // this.setResizable(false);
        this.setVisible(true);
    }

    /**
     *  Initialization page --  Prepare three pages 
     */

    private void initPanel() {
//        System.out.println(" Page initialization ...");
        initInputPanel();
        initOpenPanel();
        initShowPanel();

    }

    private void initInputPanel() {

        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(" Input page ||" + inputPanel);

    }

    private void initOpenPanel() {
        openPanel.setLayout(null);
        openPanel.setBounds(0, 0, 400, 600);
        // this.add(bg_label);
        openPanel.add(open_ownerName);
        openPanel.add(open_label);
//        System.out.println(" Open the page ||" + openPanel);
    }

    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);
//        System.out.println(" Display page ||" + 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);

    }

    /**
     *  Every time you open the page ,  Set up  panel Methods 
     */
    private void setPanel(JPanel panel) {
        //  Remove the current page 
        layeredPane.removeAll();

//        System.out.println(" To reset : New page ");
        //  background lable Add to layeredPane Default layer 
        layeredPane.add(panelLable.get(panel), JLayeredPane.DEFAULT_LAYER);

        //  panel panel Set transparent 
        panel.setOpaque(false);

        //  panel panel  Add to  layeredPane Of modal layer 
        layeredPane.add(panel, JLayeredPane.MODAL_LAYER);
    }

    // private void setShowPanel(JPanel show) {
    // setPanel(show);
    // layeredPane.add(show_jsp, JLayeredPane.MODAL_LAYER);
    //
    // }

    /**
     *  Set the listener of the component 
     */
    private void addListener() {

        input_total.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                // System.out.println(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) {
                // System.out.println(e);
//                System.out.println(" Number :" + input_count.getText());
            }
        });
        input_msg.addKeyListener(new KeyAdapter() {

            @Override
            public void keyReleased(KeyEvent e) {
                // System.out.println(e);
//                System.out.println(" Leaving a message. :" + input_msg.getText());
            }
        });

        input_inMoney.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                try {

                    //  Get the value of the page .
                    totalMoney = (int) (Double.parseDouble(input_total.getText()) * 100); //  convert to " branch "
                    count = Integer.parseInt(input_count.getText());
                    if (count > 30) {
                        JOptionPane.showMessageDialog(null, " The number of red envelopes shall not exceed 30 individual ", " Wrong number of red envelopes ", JOptionPane.INFORMATION_MESSAGE);
                        return;
                    }

                    initMoney = totalMoney;

                    System.out.println(" Total sum :[" + totalMoney + "] branch ");
                    System.out.println(" Number of red packets :[" + count + "] individual ");

                    input_inMoney.removeMouseListener(this);

//                    System.out.println(" Jump --> Open a new page ");

                    //  Set group leader name 
                    open_ownerName.setText(ownerName);
                    //  Set open page 
                    setPanel(openPanel);

                } catch (Exception e2) {
                    JOptionPane.showMessageDialog(null, " Please input correct 【 Total sum 】 or 【 Number of red packets 】", " The input information is wrong ", JOptionPane.ERROR_MESSAGE);

                }
            }
        });

        // open_ownerName , Click on  [ name ], How to trigger  ,  Prompt how to set the group leader name .

        open_ownerName.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                JOptionPane.showMessageDialog(null, " Please pass 【setOwnerName】 Method to set the group owner name ", " The group leader name is not set ",
                        JOptionPane.QUESTION_MESSAGE);
            }
        });

        // open label ,  Click on  [ open ], How to trigger , Tips on how to set the opening method .
        open_label.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (openWay == null) {
                    JOptionPane.showMessageDialog(null, " Please pass 【setOpenWay】 Method to set the opening method ", " The opening method is not set ",
                            JOptionPane.QUESTION_MESSAGE);
                    return;
                }

//                System.out.println(" Jump --> Display page ");

                moneyList = openWay.divide(totalMoney, count);

//                System.out.println(moneyList);
                /*
                 * showPanel  Add data 
                 *
                 */
                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 + " A total of... Red envelopes " + (initMoney / 100.0) + " element , Robbed ");

                open_label.removeMouseListener(this);

                setPanel(showPanel);

                //  Add data 
                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 + " element ");

                    } else {

                        tf.setText(" Group members -" + i + ":\t" + moneyList.get(i) / 100.0 + " element ");
                    }
                    showPanel2.add(tf);
                }

                layeredPane.add(show_jsp, JLayeredPane.MODAL_LAYER);
            }

        });

    }

    /* ======================================================================
     * **********************************************************************
     * *  The above codes are part of the page processing , Including layout / Interaction / Jump / Display etc. , everyone 							*
     * *																	*
     * *																	*
     * **********************************************************************
     * ======================================================================
     */

    /**
     * ownerName :  Group leader name 
     */
    private String ownerName = " Who? Who "; //  Group leader name 
    /**
     * openWay :  The type of red envelope  [ Ordinary red envelopes / Lucky red envelope ]
     */
    private OpenMode openWay = null;


    /**
     *  Construction method : Generate red envelope interface .
     *
     * @param title  The title of the interface 
     */

    public RedPacketFrame(String title) {
        super(title);

        //  Page related initialization 
        init();
    }

    public void setOwnerName(String ownerName) {
        this.ownerName = ownerName;
    }

    public void setOpenWay(OpenMode openWay) {
        this.openWay = openWay;
    }


}

原网站

版权声明
本文为[How about silence]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211543441429.html