Advertisement
Dimitar_Iliev

Random_number

Dec 5th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.60 KB | None | 0 0
  1. import java.awt.EventQueue;
  2.  
  3. import javax.swing.JFrame;
  4. import javax.swing.JTextField;
  5. import javax.swing.JLabel;
  6. import javax.swing.JButton;
  7. import java.awt.Font;
  8. import java.util.Random;
  9. import java.awt.event.ActionListener;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.Color;
  12. import java.awt.SystemColor;
  13. import javax.swing.UIManager;
  14. import javax.swing.SwingConstants;
  15.  
  16. public class RandomGUI {
  17.  
  18.     static int getRandom(int min, int max) {
  19.         Random rand = new Random();
  20.         if (min > max) {
  21.             int temp = min;
  22.             min = max;
  23.             max = temp;
  24.         }
  25.         int num = min + rand.nextInt(max - min + 1);
  26.         return num;
  27.     }
  28.    
  29.     private JFrame frmRandomNumber;
  30.     private JTextField minField;
  31.     private JTextField maxField;
  32.  
  33.     /**
  34.      * Launch the application.
  35.      */
  36.     public static void main(String[] args) {
  37.         EventQueue.invokeLater(new Runnable() {
  38.             public void run() {
  39.                 try {
  40.                     RandomGUI window = new RandomGUI();
  41.                     window.frmRandomNumber.setVisible(true);
  42.                 } catch (Exception e) {
  43.                     e.printStackTrace();
  44.                 }
  45.             }
  46.         });
  47.     }
  48.  
  49.     /**
  50.      * Create the application.
  51.      */
  52.     public RandomGUI() {
  53.         initialize();
  54.     }
  55.  
  56.     /**
  57.      * Initialize the contents of the frame.
  58.      */
  59.     private void initialize() {
  60.         frmRandomNumber = new JFrame();
  61.         frmRandomNumber.setBackground(UIManager.getColor("Button.background"));
  62.         frmRandomNumber.getContentPane().setBackground(new Color(124, 252, 0));
  63.         frmRandomNumber.setTitle("Random number");
  64.         frmRandomNumber.setBounds(100, 100, 321, 311);
  65.         frmRandomNumber.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  66.         frmRandomNumber.getContentPane().setLayout(null);
  67.        
  68.         minField = new JTextField();
  69.         minField.setBackground(UIManager.getColor("CheckBox.background"));
  70.         minField.setBounds(94, 55, 105, 22);
  71.         frmRandomNumber.getContentPane().add(minField);
  72.         minField.setColumns(10);
  73.        
  74.         maxField = new JTextField();
  75.         maxField.setBackground(UIManager.getColor("CheckBox.background"));
  76.         maxField.setBounds(94, 104, 105, 22);
  77.         frmRandomNumber.getContentPane().add(maxField);
  78.         maxField.setColumns(10);
  79.        
  80.         JLabel lblNewLabel = new JLabel("Min:");
  81.         lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 17));
  82.         lblNewLabel.setBounds(36, 57, 46, 14);
  83.         frmRandomNumber.getContentPane().add(lblNewLabel);
  84.        
  85.         JLabel lblNewLabel_1 = new JLabel("Max:");
  86.         lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 17));
  87.         lblNewLabel_1.setBounds(36, 109, 46, 14);
  88.         frmRandomNumber.getContentPane().add(lblNewLabel_1);
  89.        
  90.         JLabel resultLbl = new JLabel("Random number:");
  91.         resultLbl.setFont(new Font("Tahoma", Font.PLAIN, 17));
  92.         resultLbl.setBounds(36, 206, 188, 29);
  93.         frmRandomNumber.getContentPane().add(resultLbl);
  94.        
  95.         JButton generateBtn = new JButton("Generate");
  96.         generateBtn.setBackground(UIManager.getColor("Button.light"));
  97.         generateBtn.addActionListener(new ActionListener() {
  98.            
  99.             public void actionPerformed(ActionEvent e) {
  100.                 try {
  101.                     int min = Integer.parseInt(minField.getText());
  102.                     int max = Integer.parseInt(maxField.getText());
  103.                     int r = getRandom(min, max);
  104.                     resultLbl.setText("Random number " + r);
  105.                 } catch (Exception ex) {
  106.                     minField.setText("");
  107.                     maxField.setText("");
  108.                     resultLbl.setText("Invalid numbers!");
  109.                 }
  110.             }
  111.         });
  112.         generateBtn.setFont(new Font("Tahoma", Font.PLAIN, 15));
  113.         generateBtn.setBounds(97, 161, 102, 44);
  114.         frmRandomNumber.getContentPane().add(generateBtn);
  115.        
  116.         JButton clearBtn = new JButton("Clear");
  117.         clearBtn.setBackground(UIManager.getColor("Button.light"));
  118.         clearBtn.addActionListener(new ActionListener() {
  119.             public void actionPerformed(ActionEvent e) {
  120.                 minField.setText("");
  121.                 maxField.setText("");
  122.                 resultLbl.setText("Random number ");
  123.             }
  124.         });
  125.         clearBtn.setFont(new Font("Tahoma", Font.PLAIN, 15));
  126.         clearBtn.setBounds(209, 13, 82, 44);
  127.         frmRandomNumber.getContentPane().add(clearBtn);
  128.        
  129.        
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement