You are on page 1of 3

import java.awt.

Color;

import static java.awt.Color.MAGENTA;

import java.awt.Dimension;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JTextField;

public class DiceRoller extends JFrame implements ActionListener {

JFrame f;

JPanel p;

JButton roller;

JLabel dice1;

JLabel dice2;

JTextField dice1tf;

JTextField dice2tf;

Color c;

public DiceRoller() {

f = new JFrame("Dice Roller");

f.getContentPane().setBackground(new Color(0xdb6969));

f.setBounds(500, 80, 400, 300);


f.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 25));

roller = new JButton("Roll Again!");

dice1 = new JLabel("DICE 1");

dice2 = new JLabel("DICE 2");

dice1tf = new JTextField(10);

dice2tf = new JTextField(10);

p = new JPanel(new FlowLayout(FlowLayout.CENTER));

p.setBackground(new Color(0xdb6969));

p.setPreferredSize(new Dimension(200, 90));

p.add(dice1);

p.add(dice1tf);

p.add(dice2);

p.add(dice2tf);

p.add(roller);

roller.addActionListener(this);

f.add(p);

f.setVisible(true);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

@Override

public void actionPerformed(ActionEvent e) {

if (e.getSource() == roller) {

int dice1random = (int) (1 + (Math.random() * 6));

String rndnumber1 = Integer.toString(dice1random);

int dice2random = (int) (1 + (Math.random() * 6));

String rndnumber2 = Integer.toString(dice2random);

dice1tf.setText(rndnumber1);

dice2tf.setText(rndnumber2);
if (dice1random == 1 && dice2random == 1) {

JFrame f = new JFrame();

JOptionPane.showMessageDialog(f, "Great!");

public static void main(String[] args) {

new DiceRoller();

You might also like