This preview has intentionally blurred parts. Sign up to view the full document

View Full Document

Unformatted Document Excerpt

// Exercise 15.12: DiceSimulator.java // This application rolls dice and counts the frequency of each side. import java.awt.*; import java.awt.event.*; import java.util.Random; import javax.swing.*; i public class DiceSimulator extends JFrame { // JLabel and JTextField to display number of 1s private JLabel side1JLabel; private JTextField output1JTextField; // JLabel and JTextField to display number of 2s private JLabel side2JLabel; private JTextField output2JTextField; // JLabel and JTextField to display number of 3s private JLabel side3JLabel; private JTextField output3JTextField; // JLabel and JTextField to display number of 4s private JLabel side4JLabel; private JTextField output4JTextField; // JLabel and JTextField to display number of 5s private JLabel side5JLabel; private JTextField output5JTextField; // JLabel and JTextField to display number of 6s private JLabel side6JLabel; private JTextField output6JTextField; // JLabel and JTextField to display total rolls private JLabel totalJLabel; private JTextField totalJTextField; // JLabels to display dice private JLabel die1JLabel; private JLabel die2JLabel; // JButton to roll dice private JButton rollJButton; // dice face constants private final int ONE = 1; private final int TWO = 2; private final int THREE = 3; private final int FOUR = 4; private final int FIVE = 5; private final int SIX = 6; // create a new random object private Random generator = new Random(); // no-argument constructor public DiceSimulator() { createUserInterface(); } // create and position GUI components; register event handlers private void createUserInterface() { // get content pane for attaching GUI components Container contentPane = getContentPane(); // enable explicit positioning of GUI components contentPane.setLayout( null ); // set up side1JLabel side1JLabel = new JLabel(); side1JLabel.setBounds( 16, 16, 48, 23 ); side1JLabel.setText( "Side 1:" ); contentPane.add( side1JLabel ); // set up output1JTextField output1JTextField = new JTextField(); output1JTextField.setBounds( 72, 16, 56, 23 ); output1JTextField.setText( "0" );output1JTextField.... View Full Document

End of Preview

Sign up now to access the rest of the document