Advertisement
UKTC162

Lottery

May 20th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.53 KB | None | 0 0
  1. mport java.awt.EventQueue;
  2.  
  3. import javax.swing.JFrame;
  4. import javax.swing.JLabel;
  5. import javax.swing.SwingConstants;
  6. import java.awt.Font;
  7. import javax.swing.JButton;
  8. import java.awt.event.ActionListener;
  9. import java.util.Random;
  10. import java.awt.event.ActionEvent;
  11.  
  12. public class HiLoLottery {
  13.  
  14.     private JFrame frame;
  15.  
  16.     /**
  17.      * Launch the application.
  18.      */
  19.     public static void main(String[] args) {
  20.         EventQueue.invokeLater(new Runnable() {
  21.             public void run() {
  22.                 try {
  23.                     HiLoLottery window = new HiLoLottery();
  24.                     window.frame.setVisible(true);
  25.                 } catch (Exception e) {
  26.                     e.printStackTrace();
  27.                 }
  28.             }
  29.         });
  30.     }
  31.  
  32.     /**
  33.      * Create the application.
  34.      */
  35.     public HiLoLottery() {
  36.         initialize();
  37.     }
  38.  
  39.     /**
  40.      * Initialize the contents of the frame.
  41.      */
  42.     private void initialize() {
  43.         frame = new JFrame();
  44.         frame.setBounds(100, 100, 450, 300);
  45.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  46.         frame.getContentPane().setLayout(null);
  47.  
  48.         Random rand = new Random();
  49.  
  50.         int[] price = { 1500, 2000, 3000, 5000, 7000, 13000, 20000, 35000, 50000, 200000 };
  51.  
  52.         JLabel numberLbl = new JLabel("");
  53.         numberLbl.setFont(new Font("Tahoma", Font.PLAIN, 25));
  54.         numberLbl.setHorizontalAlignment(SwingConstants.CENTER);
  55.         numberLbl.setBounds(10, 11, 140, 140);
  56.         frame.getContentPane().add(numberLbl);
  57.  
  58.         JLabel textLbl = new JLabel("Number:");
  59.         textLbl.setFont(new Font("Tahoma", Font.PLAIN, 16));
  60.         textLbl.setHorizontalAlignment(SwingConstants.CENTER);
  61.         textLbl.setBounds(10, 158, 110, 30);
  62.         frame.getContentPane().add(textLbl);
  63.  
  64.         JLabel sumLbl = new JLabel("<html>Your current <br> sum is: 0<html>");
  65.         sumLbl.setHorizontalAlignment(SwingConstants.CENTER);
  66.         sumLbl.setFont(new Font("Tahoma", Font.PLAIN, 16));
  67.         sumLbl.setBounds(291, 199, 110, 50);
  68.         frame.getContentPane().add(sumLbl);
  69.  
  70.         JLabel countLbl = new JLabel("");
  71.         countLbl.setHorizontalAlignment(SwingConstants.LEFT);
  72.         countLbl.setFont(new Font("Tahoma", Font.PLAIN, 16));
  73.         countLbl.setBounds(101, 160, 49, 26);
  74.         frame.getContentPane().add(countLbl);
  75.  
  76.         JButton startBtn = new JButton("Start");
  77.         startBtn.setFont(new Font("Tahoma", Font.PLAIN, 14));
  78.         startBtn.addActionListener(new ActionListener() {
  79.             public void actionPerformed(ActionEvent e) {
  80.                 int num = rand.nextInt(51);
  81.                 numberLbl.setText("" + num);
  82.                 countLbl.setText("" + 0);
  83.                 sumLbl.setText("<html>Your current <br> sum is: " + price[0]);
  84.             }
  85.         });
  86.         startBtn.setBounds(20, 199, 110, 50);
  87.         frame.getContentPane().add(startBtn);
  88.  
  89.         JButton upBtn = new JButton("UP");
  90.         upBtn.addActionListener(new ActionListener() {
  91.             public void actionPerformed(ActionEvent e) {
  92.                 int num = rand.nextInt(51);
  93.                 int prevNum = Integer.parseInt(numberLbl.getText());
  94.                 numberLbl.setText("" + num);
  95.                 if (num > prevNum) {
  96.                     int count = Integer.parseInt(countLbl.getText());
  97.                     count++;
  98.                     if (count == 9) {
  99.                         sumLbl.setText("<htnl> Congratulations! <br> You Won 200 000 <html>");
  100.                     } else {
  101.                         sumLbl.setText("<html>Your current <br> sum is: <html>" + price[count]);
  102.                         countLbl.setText("" + count);
  103.                     }
  104.                 } else {
  105.                     sumLbl.setText("You Lost");
  106.                 }
  107.  
  108.             }
  109.         });
  110.         upBtn.setFont(new Font("Tahoma", Font.PLAIN, 14));
  111.         upBtn.setBounds(291, 11, 84, 65);
  112.         frame.getContentPane().add(upBtn);
  113.  
  114.         JButton downBtn = new JButton("DOWN");
  115.         downBtn.addActionListener(new ActionListener() {
  116.             public void actionPerformed(ActionEvent e) {
  117.                 int num = rand.nextInt(51);
  118.                 int prevNum = Integer.parseInt(numberLbl.getText());
  119.                 numberLbl.setText("" + num);
  120.                 if (num < prevNum) {
  121.                     int count = Integer.parseInt(countLbl.getText());
  122.                     count++;
  123.                     if (count == 9) {
  124.                         sumLbl.setText("<html> Congratulations! <br> You Won 200 000 <html>");
  125.                     } else {
  126.                         sumLbl.setText("<html>Your current <br> sum is: <html>" + price[count]);
  127.                         countLbl.setText("" + count);
  128.                     }
  129.                 } else {
  130.                     sumLbl.setText("You Lost");
  131.                 }
  132.  
  133.             }
  134.         });
  135.         downBtn.setFont(new Font("Tahoma", Font.PLAIN, 14));
  136.         downBtn.setBounds(291, 87, 84, 65);
  137.         frame.getContentPane().add(downBtn);
  138.  
  139.         JButton moneyBtn = new JButton("Take Money");
  140.         moneyBtn.addActionListener(new ActionListener() {
  141.             public void actionPerformed(ActionEvent e) {
  142.                 int count = Integer.parseInt(countLbl.getText());
  143.                 sumLbl.setText("<html> Congratulations! <br> You Won <html>" + price[count]);
  144.             }
  145.         });
  146.         moneyBtn.setFont(new Font("Tahoma", Font.PLAIN, 14));
  147.         moneyBtn.setBounds(144, 199, 110, 50);
  148.         frame.getContentPane().add(moneyBtn);
  149.  
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement