gpt4 book ai didi

java - DnD 骰子滚轴

转载 作者:行者123 更新时间:2023-12-02 02:18:02 25 4
gpt4 key购买 nike

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

  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);
}
}
}


'

最佳答案

当您按下按钮时,您永远不会重新掷骰子。您只需在 createGui 中设置一次骰子的值,然后就不再设置了。如果您想在单击按钮时更改骰子的值,则应在每次按下按钮时重新生成它们的值(即在 actionPerformed 中)。

关于java - DnD 骰子滚轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48982493/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com