0

So i'm trying to code a D&D Dice roller that pulls up a GUI that has 5 buttons and that when pressed pulls up a message with the roll of that dice. When i click the D4 button it pulls up the message and only rolls a 1 every time i click it and so on with the other buttons. I'm so confused with this i haven't coded in 3 years and trying to get back into it and i feel like its something easy but i have no idea. I don't know why but when it rolls any of the dice it rolls a 1. If i can get any help that would be great there's no rush i'm just doing this for fun

  import javax.swing.*;
     import java.awt.*;
     import java.awt.event.ActionEvent;
     import java.awt.event.ActionListener;
     import java.util.Random;

        public class GUIRNG implements ActionListener {

            int d4 = rand.nextInt(4) + 1;
            int d6 = rand.nextInt(6) + 1;
            int d8 = rand.nextInt(8) + 1;
            int d10 = rand.nextInt(10) + 1;
            int d20 = rand.nextInt(20) + 1;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new GUIRNG().createGui();

                    }
                });
            }
            private enum Actions {
                D4,
                D6,
                D8,
                D10,
                D20,
        }
            public void createGui() {
                JFrame frame = new JFrame("D&D Dice Roller");
                frame.setSize(700, 700);
                frame.setVisible(true);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                JPanel panel = new JPanel(new GridBagLayout());
                panel.setBackground(Color.black);
                frame.add(panel);
                frame.getContentPane().add(panel, BorderLayout.WEST);
                GridBagConstraints c = new GridBagConstraints();

        JButton button1 = new JButton("D4");
                button1.setActionCommand(Actions.D4.name());
                button1.addActionListener(this);
                d4 = rand.nextInt(4) + 1;
                c.gridx = 0;
                c.gridy = 0;
                c.insets = new Insets(40, 40, 40, 40);
                panel.add(button1, c); 

        JButton button2 = new JButton("D6");
                button2.setActionCommand(Actions.D6.name());
                button2.addActionListener(this);
                d6 = rand1.nextInt(6) + 1;
                c.gridx = 0;
                c.gridy = 1;
                panel.add(button2, c);

        JButton button3 = new JButton("D8");
                button3.setActionCommand(Actions.D8.name());
                button3.addActionListener(this);
                d8 = rand.nextInt(8) + 1;
                c.gridx = 0;
                c.gridy = 2;
                panel.add(button3, c);
                button3.addActionListener(this);

        JButton button4 = new JButton("D10");
                button4.setActionCommand(Actions.D10.name());
                button4.addActionListener(this);
                d10 = rand.nextInt(10) + 1;
                c.gridx = 0;
                c.gridy = 3;
                panel.add(button4, c);
                button4.addActionListener(this);

        JButton button5 = new JButton("D20");
                button5.setActionCommand(Actions.D20.name());
                button5.addActionListener(this);
                d20 = rand.nextInt(20) + 1;
                c.gridx = 0;
                c.gridy = 4;
                panel.add(button5, c);
                button5.addActionListener(this);
            }




            public void actionPerformed(ActionEvent e) {
                if (e.getActionCommand() == Actions.D4.name()) {
                    JOptionPane.showMessageDialog(null, "You rolled: "+d4);
                }
                if (e.getActionCommand() == Actions.D6.name()) {
                    JOptionPane.showMessageDialog(null, "You rolled: "+d6);
                }
                if (e.getActionCommand() == Actions.D8.name()) {
                    JOptionPane.showMessageDialog(null, "You rolled: "+d8);
                }
                if (e.getActionCommand() == Actions.D10.name()) {
                    JOptionPane.showMessageDialog(null, "You rolled: "+d10);
                }
                if (e.getActionCommand() == Actions.D20.name()) {
                    JOptionPane.showMessageDialog(null, "You rolled: "+d20);
                }
            }
        }


'
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Josh Reid
  • 1
  • 1

1 Answers1

2

You're never rerolling the dice when you press the buttons. You're setting the values of the dice once, in createGui, and never again. If you want to change the value of the dice when you click the buttons, you should re-generate their values each time the button is pressed (I.E. in actionPerformed).

EKW
  • 2,059
  • 14
  • 24