Advertisement
kajacx

Click random

Jan 29th, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.42 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package blbosti;
  6.  
  7. import java.awt.*;
  8. import java.awt.event.*;
  9. import javax.swing.*;
  10.  
  11. /**
  12.  *
  13.  * @author kajacx
  14.  */
  15. public class RollDaDice {
  16.  
  17.     private JLabel myLabel;
  18.  
  19.     private void main() {
  20.         JFrame frame = new JFrame("Roll Da Dice");
  21.  
  22.         //puts element from left to right, aligns them at center
  23.         frame.setLayout(new FlowLayout(FlowLayout.CENTER));
  24.  
  25.  
  26.         JButton button = new JButton("Click me");
  27.  
  28.         button.addActionListener(new ActionListener() {
  29.             @Override
  30.             public void actionPerformed(ActionEvent e) {
  31.                 int randNum = (int) (Math.random() * 1000);
  32.  
  33.                 //if you're not familiar with printf / format function, replace with
  34.                 //myLabel.setText("Rolled: " + randNum);
  35.                 myLabel.setText(String.format("Rolled: %03d", randNum));
  36.             }
  37.         });
  38.  
  39.         frame.add(button);
  40.  
  41.         myLabel = new JLabel("Click to roll da dice");
  42.         frame.add(myLabel);
  43.  
  44.         //auto set size
  45.         frame.pack();
  46.  
  47.         //put frame at the center of screen
  48.         frame.setLocationRelativeTo(null);
  49.  
  50.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  51.         frame.setVisible(true);
  52.     }
  53.  
  54.     public static void main(String[] main) {
  55.         //hack so i dont have to write 'static' everywhere
  56.         new RollDaDice().main();
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement