Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.EventQueue;
- import javax.swing.JFrame;
- import javax.swing.JTextField;
- import javax.swing.JLabel;
- import javax.swing.JButton;
- import java.awt.Font;
- import java.util.Random;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import java.awt.Color;
- import java.awt.SystemColor;
- import javax.swing.UIManager;
- import javax.swing.SwingConstants;
- public class RandomGUI {
- static int getRandom(int min, int max) {
- Random rand = new Random();
- if (min > max) {
- int temp = min;
- min = max;
- max = temp;
- }
- int num = min + rand.nextInt(max - min + 1);
- return num;
- }
- private JFrame frmRandomNumber;
- private JTextField minField;
- private JTextField maxField;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- RandomGUI window = new RandomGUI();
- window.frmRandomNumber.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the application.
- */
- public RandomGUI() {
- initialize();
- }
- /**
- * Initialize the contents of the frame.
- */
- private void initialize() {
- frmRandomNumber = new JFrame();
- frmRandomNumber.setBackground(UIManager.getColor("Button.background"));
- frmRandomNumber.getContentPane().setBackground(new Color(124, 252, 0));
- frmRandomNumber.setTitle("Random number");
- frmRandomNumber.setBounds(100, 100, 321, 311);
- frmRandomNumber.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frmRandomNumber.getContentPane().setLayout(null);
- minField = new JTextField();
- minField.setBackground(UIManager.getColor("CheckBox.background"));
- minField.setBounds(94, 55, 105, 22);
- frmRandomNumber.getContentPane().add(minField);
- minField.setColumns(10);
- maxField = new JTextField();
- maxField.setBackground(UIManager.getColor("CheckBox.background"));
- maxField.setBounds(94, 104, 105, 22);
- frmRandomNumber.getContentPane().add(maxField);
- maxField.setColumns(10);
- JLabel lblNewLabel = new JLabel("Min:");
- lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 17));
- lblNewLabel.setBounds(36, 57, 46, 14);
- frmRandomNumber.getContentPane().add(lblNewLabel);
- JLabel lblNewLabel_1 = new JLabel("Max:");
- lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 17));
- lblNewLabel_1.setBounds(36, 109, 46, 14);
- frmRandomNumber.getContentPane().add(lblNewLabel_1);
- JLabel resultLbl = new JLabel("Random number:");
- resultLbl.setFont(new Font("Tahoma", Font.PLAIN, 17));
- resultLbl.setBounds(36, 206, 188, 29);
- frmRandomNumber.getContentPane().add(resultLbl);
- JButton generateBtn = new JButton("Generate");
- generateBtn.setBackground(UIManager.getColor("Button.light"));
- generateBtn.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- try {
- int min = Integer.parseInt(minField.getText());
- int max = Integer.parseInt(maxField.getText());
- int r = getRandom(min, max);
- resultLbl.setText("Random number " + r);
- } catch (Exception ex) {
- minField.setText("");
- maxField.setText("");
- resultLbl.setText("Invalid numbers!");
- }
- }
- });
- generateBtn.setFont(new Font("Tahoma", Font.PLAIN, 15));
- generateBtn.setBounds(97, 161, 102, 44);
- frmRandomNumber.getContentPane().add(generateBtn);
- JButton clearBtn = new JButton("Clear");
- clearBtn.setBackground(UIManager.getColor("Button.light"));
- clearBtn.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- minField.setText("");
- maxField.setText("");
- resultLbl.setText("Random number ");
- }
- });
- clearBtn.setFont(new Font("Tahoma", Font.PLAIN, 15));
- clearBtn.setBounds(209, 13, 82, 44);
- frmRandomNumber.getContentPane().add(clearBtn);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement