Write a GUI that will allow you to simulate a Die. The GUI should take input fro
ID: 3673367 • Letter: W
Question
Write a GUI that will allow you to simulate a Die. The GUI should take input from the user for the number of sides of the Die and then it should create the Die accordingly. The GUI should then allow the user to roll the Die. It should display the number of times the Die has been rolled as a history of the numbers that have been rolled. If the user creates a new Die (maybe with a different number of sides) the GUI should reset the history and the number-of-rolls counter. Make sure to use the Die class, do not re-write the die functionality in your GUI. Your GUI does not have to look as structured as the one below. This is just a sample GUI. (Extra) If the user enters invalid data for number of sides for the Die, catch the exception and show and error in a JOptionPane. Keep track, somewhere on your GUI, of the frequency of each Die value that has come up so far.Explanation / Answer
//JOptionPane
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/** Test JTextField,*/
public class JTextComponentDemo extends JFrame {
// Private variables of the GUI components
JTextField tField;
/** Constructor to set up all the GUI components */
public JTextComponentDemo() {
// JPanel for the text fields
JPanel tfPanel = new JPanel(new GridLayout(3, 2, 10, 2));
tfPanel.setBorder(BorderFactory.createTitledBorder("Text Fields: "));
tfPanel.add(new JLabel(" Number of sizes"));
tField = new JTextField(10);
tfPanel.add(tField);
JButton button = new JButton(); // use create button
button.setText("Create");
JButton button = new JButton(); // use Roll currrent die button
button.setText("Roll current die");
String inStr = JOptionPane.showInputDialog(null, '"Note that creating a new die replaces the current one","Input Dialog", JOptionPane.PLAIN_MESSAGE);
JOptionPane.showMessageDialog(null, ,"Message Dialog", JOptionPane.PLAIN_MESSAGE);
tField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
tArea.append(" You have typed " + tField.getText());
}
});
// Setup the content-pane of JFrame in BorderLayout
Container cp = this.getContentPane();
cp.setLayout(new BorderLayout(5, 5));
cp.add(tfPanel, BorderLayout.NORTH);
cp.add(tAreaScrollPane, BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("JTextComponent Demo");
setSize(350, 350);
setVisible(true);
}
/** The entry main() method */
public static void main(String[] args) {
// Run GUI codes in Event-Dispatching thread for thread safety
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new JTextComponentDemo(); // Let the constructor do the job
}
});
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.