简体   繁体   English

使用随机分配图像将相同图像分配给所有JButton

[英]Assigning image using random assigns the same image to all JButtons

Made a simple Dice throwing test from my code, in an attempt to find why the same image is displayed for every JButton. 从我的代码中进行了一个简单的Dice投掷测试,试图找出为什么每个JButton都显示相同的图像。 I have a feeling that the values are not being properly read by the program and so it assigns the same image to all the JButtons. 我感觉程序没有正确读取值,因此它将相同的图像分配给所有JButton。

Test Class 测试班

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Test extends JFrame {

    JButton[] pic = new JButton[5];
    JPanel panel = new JPanel();
    JButton throwDice = new JButton("Throw");
    ImageIcon img;

    Die[] dieH = new Die[5];
    Dice dice = new Dice();

    Test(String title) {
        super(title);
        setSize(400, 400);
        setLayout(new BorderLayout());
        add(panel, BorderLayout.CENTER);
        components();
        throwHandle();
    }

    public static void main(String[] args) {
        Test test = new Test("Test");
        test.setVisible(true);
    }

    public void components() {
        for (int i = 0; i < 5; i++) {
            pic[i] = new JButton("");
            pic[i].setPreferredSize(new Dimension(50, 50));
            panel.add(pic[i]);
        }
        panel.add(throwDice);
    }

    public void throwHandle() {
        throwDice.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dieH = dice.roll();

                for (int i = 0; i < 5; i++) {
                    pic[i].setIcon(dieH[i].getDieImage());
                    System.out.println(dieH[i].getValue());
                }
            }
        });
    }
}

Die Class 模具类

import javax.swing.*;

public class Die {

    int faceValue;
    ImageIcon img;

    public ImageIcon getDieImage() {
        return img;
    }

    public void setImage() {
        for (int i = 0; i < 7; i++) {
        img = new ImageIcon("./src/images/" + i + ".png");
        }
    }

    public void setValue(int v) {
        this.faceValue = v;
        setImage();
    }

    public int getValue() {
        return faceValue;
    }
}

Dice Class 骰子类

import java.util.Random;

public class Dice {

    Die[] diceArray = new Die[6];

    int min = 1, max = 6;

    public Dice() {
        for (int i = 0; i < diceArray.length; i++) {
        diceArray[i] = new Die();
        diceArray[i].setValue(i + 1);
        }
     }

    public Die[] roll() {
        Random rm = new Random();
        Die[] roll = new Die[5];

        for (int i = 0; i < 5; i++) {
            int newRandom = rm.nextInt((max - min) + 1);
            roll[i] = diceArray[newRandom];
            System.out.println(roll[i].getValue());
        }

        return roll;
    }

}

Let's examine the following code: 让我们检查以下代码:

public class Die {

    int faceValue;
    ImageIcon img;

So, a Die has a unique face value, and a unique image 因此,骰子具有独特的面值和独特的图像

    public ImageIcon getDieImage() {
        return img;
    }

    public void setImage() {
        for (int i = 0; i < 7; i++) {
            img = new ImageIcon("./src/images/" + i + ".png");
        }

So, you loop from 0 to 6, and at each iteration, you replace the previous image by a new one. 因此,您从0循环到6,并且在每次迭代中,都用新的图像替换了之前的图像。 This is equivalent to img = new ImageIcon("./src/images/6.png"); 这等效于img = new ImageIcon("./src/images/6.png"); . Note also that the image doesn't depend on the face favleu of the die. 还要注意,图像不依赖于模具的面部。

    }

    public void setValue(int v) {
        this.faceValue = v;
        setImage();

This sets the face value of the die, and it then sets the image, but the image doesn't depend on the face value at all. 这将设置模具的面值,然后设置图像,但是图像完全不依赖于面值。

    }

    public int getValue() {
        return faceValue;
    }
}

So, basically, setImage() needs to use the face value, and set the image based on the face value. 因此,基本上, setImage()需要使用面部值,并根据面部值设置图像。 It should also be private: you don't want anyone to change the image of a dice without changing its face value. 它也应该是私密的:您不希望任何人在不改变其面值的情况下更改骰子的图像。

private void setImage() {
    img = new ImageIcon("./src/images/" + this.faceValue + ".png");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM