I have a project that accepts the number of die and the number of faces on each die from the user. It will roll the number of die the user wanted, display the face each die landed on, and show the total of all the die together. This is exactly what my program is supposed to do.
However, all of this is supposed to happen within a JFrame and a JPanel. As you can see, I have GUI code, but it is currently commented out due to the fact that it does not work. Everything initially displays correctly, with the labels, button, and text boxes... But I do not know how to make the "Roll" button work.
As of right now, nothing happens when I click the "Roll" button. I don't know how to make it work. I need the GUI part of my program to display the face each die landed on and the total of all the die when the "Roll" button is clicked.
I would like to delete the DiceRollerTest()
code when my program functions correctly, and only use showGUI()
.
Could someone please lead me in the right direction?
Here is my main, DiceRollerTest()
, ShowUI()
, and ShowGUI()
code...
public static void main(String[] args)
{
//showGUI();
showUI();
}
public void DiceRollerTest()
{
try
{
d = new DiceBag(6,6);
}
catch (FaceRangeException e){System.out.println(e.getMessage()); return;}
catch (DiceRangeException e){System.out.println(e.getMessage()); return;}
System.out.println(d.getClass());
int total = d.roll();
System.out.println(Integer.toString(total)+" "+d.toString());
}
static void showUI()
{
Scanner keyboardInput = new Scanner(System.in);
String input;
int numberOfDice;
int numberOfFaces;
System.out.println("Enter number of dice to roll:");
input = keyboardInput.nextLine();
numberOfDice = Integer.parseInt(input);
System.out.println("Enter number of faces:");
input = keyboardInput.nextLine();
numberOfFaces = Integer.parseInt(input);
try
{
d = new DiceBag(numberOfDice, numberOfFaces);
}
catch (FaceRangeException e){System.out.println(e.getMessage());}
catch (DiceRangeException e){System.out.println(e.getMessage());}
d.roll();
System.out.println("Rolls: "+d.toString());
System.out.println("Total: "+d.getTotal());
keyboardInput.close();
static void showGUI()
{
JFrame frame = new JFrame("Dice Roller");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DiceRollerPanel panel = new DiceRollerPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
And here is my DiceRollerPanel()
and RollButtonListener
code...
public DiceRollerPanel()
{
numberOfDiceLabel = new JLabel("Enter Number of Dice:");
dice = new JTextField(5);
numberOfFacesLabel = new JLabel("Enter Number of Faces:");
faces = new JTextField(5);
rollButton = new JButton("Roll");
rollButton.addActionListener(new RollButtonListener());
displayRolls = new JLabel("Rolls:");
total = new JLabel("Total:");
add(numberOfDiceLabel);
add(dice);
add(numberOfFacesLabel);
add(faces);
add(rollButton);
add(displayRolls);
add(total);
setPreferredSize(new Dimension(225,100));
setBackground(Color.gray);
}
private class RollButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
displayRolls.setText("Rolls:");
total.setText ("Total:");
DiceBag.roll();
}
}