首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >D&D骰子机

D&D骰子机
EN

Stack Overflow用户
提问于 2018-02-26 14:14:18
回答 1查看 187关注 0票数 0

所以我正在尝试编写一个D&D掷骰子的代码,它会弹出一个有5个按钮的GUI,当按下时,会拉出一个带有掷骰子的消息。当我单击D4按钮时,它会拉出消息,并且每次我单击它时都只滚动1,以此类推。我对此感到非常困惑,我已经三年没有编码了,我试图重新开始,我觉得这是很容易的事情,但我不知道。我不知道为什么,但当它掷骰子时,它掷的是1。如果我能得到任何帮助,那就太好了,我不着急,我只是为了好玩

代码语言:javascript
复制
  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);
                }
            }
        }


'
EN

回答 1

Stack Overflow用户

发布于 2018-02-26 14:31:03

当你按下按钮的时候,你永远不会重新掷骰子。您在createGui中设置了骰子的值一次,并且再也不设置一次。如果要在单击按钮时更改骰子的值,则应在每次按下按钮时重新生成它们的值(即在actionPerformed中)。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48982493

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档