Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package blbosti;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- /**
- *
- * @author kajacx
- */
- public class RollDaDice {
- private JLabel myLabel;
- private void main() {
- JFrame frame = new JFrame("Roll Da Dice");
- //puts element from left to right, aligns them at center
- frame.setLayout(new FlowLayout(FlowLayout.CENTER));
- JButton button = new JButton("Click me");
- button.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- int randNum = (int) (Math.random() * 1000);
- //if you're not familiar with printf / format function, replace with
- //myLabel.setText("Rolled: " + randNum);
- myLabel.setText(String.format("Rolled: %03d", randNum));
- }
- });
- frame.add(button);
- myLabel = new JLabel("Click to roll da dice");
- frame.add(myLabel);
- //auto set size
- frame.pack();
- //put frame at the center of screen
- frame.setLocationRelativeTo(null);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setVisible(true);
- }
- public static void main(String[] main) {
- //hack so i dont have to write 'static' everywhere
- new RollDaDice().main();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement