JAVA初学者 创建和掷骰子



我想创建一个掷骰子模拟器。这就是我到目前为止得到的。

public class RollDice {
    private static class rollDice extends JPanel {
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            // custom draw code goes here
        }
    }
    public static void main(String[] args) { 
        JLabel message = new JLabel ("nRoll the Die!n", JLabel.CENTER); 
        message.setForeground(Color.BLACK); 
        message.setBackground(Color.WHITE); 
        message.setFont(new Font("Courier", Font.PLAIN, 25));
        message.setOpaque(true);
        JButton roll = new JButton("ROLL");
        JPanel content = new JPanel(); 
        content.setLayout(new BorderLayout()); 
        content.add(message, BorderLayout.NORTH);
        content.add(roll, BorderLayout.SOUTH);
        JFrame window = new JFrame("Roll Dice"); 
        window.setContentPane(content); 
        window.setSize(250,300); 
        window.setLocation(300,300); 
        window.setVisible(true);
    }
}

我得到了一个JFrame,JLabel和按钮,上面写着滚动,简单的东西。

我想弄清楚的是如何在 JPanel 中创建两个骰子,以及如何在单击"ROLL"按钮时使用它滚动,使用 math.Random 和图形。

如果它尽可能简单,我将不胜感激,因为我在编程领域不是很先进,而且最近才开始。如果您在给我答案之前尽可能详细地解释,我将不胜感激,以便我有机会事先尝试自己弄清楚。

谢谢!

就像fd.说的,你需要模具的组件。让我们使用 JLabel 作为组件。它们可以像这样排列:

+==============================+
|        Roll the Die!         |
|   +---------+  +---------+   |
|   |         |  |         |   |
|   |  dice1  |  |  dice2  |   |
|   |         |  |         |   |
|   +---------+  +---------+   |
| +--------------------------+ |
| |           ROLL           | |
| +--------------------------+ |
+==============================+

你可以在标签上设置一个ImageIcon(看看:Java:如何将图像添加到Jlabel?),所以创建6个骰子不同位置的图像。按下按钮时,将生成一个随机数(介于 1 和 6 之间)(使用 Math.random )。每个数字代表一个图像。根据此数字设置JLabel的图像。

为此,您需要一个 ActionListener。创建一个自定义ActionListener如下所示(注意我为一个骰子做了):

public class RollDiceActionListener implements ActionListener {
    private JLabel dice;
    
    public RollDiceActionListener(JLabel dice) {
        this.dice = dice;
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        int rnd = (int)(Math.random() * 6) + 1;
        
        switch (rnd) {
        case 1:
            dice.setIcon(new ImageIcon("/path/to/dice_1.png"));
            break;
        case 2:
            dice.setIcon(new ImageIcon("/path/to/dice_2.png"));
            break;
        case 3:
            dice.setIcon(new ImageIcon("/path/to/dice_3.png"));
            break;
        case 4:
            dice.setIcon(new ImageIcon("/path/to/dice_4.png"));
            break;
        case 5:
            dice.setIcon(new ImageIcon("/path/to/dice_5.png"));
            break;
        case 6:
            dice.setIcon(new ImageIcon("/path/to/dice_6.png"));
            break;
        }
    }
} 

每次按下按钮时,都会调用ActionPerformed方法并随机更改每个JLabel的图标,模拟掷骰子。

要将自定义ActionListener添加到按钮,请执行以下操作:

roll.addActionListener(new RollDiceActionListener(die));

ActionListener需要修改表示骰子的Jlabel,因此不要忘记将其作为参数添加到侦听器的构造函数中。

希望这有帮助。祝你好运!

如果我们现在忽略骰子的某种自定义绘制/动画,那么您的代码缺少一些功能元素:

  • 实例化 2 个骰子的 UI 组件的对象
  • 将这些组件添加到布局中(您可能还需要在此处创建嵌套布局对象,具体取决于您希望它们如何定位)
  • 定义一个ActionListener对象以生成掷骰子的随机数并更新骰子 UI 组件
  • ActionListener添加到滚动按钮

最新更新