使用随机分配图像会将相同的图像分配给所有JButtons

时间:2015-12-13 09:57:57

标签: java jframe jbutton

从我的代码中做了一个简单的骰子投掷测试,试图找出为每个JButton显示相同图像的原因。我感觉程序没有正确读取这些值,所以它为所有JButton分配了相同的图像。

测试类

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

骰子类

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

}

1 个答案:

答案 0 :(得分:1)

让我们检查以下代码:

public class Die {

    int faceValue;
    ImageIcon img;

因此,Die具有独特的面值和独特的图像

    public ImageIcon getDieImage() {
        return img;
    }

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

因此,您从0循环到6,并在每次迭代时,用新的图像替换上一个图像。这相当于img = new ImageIcon("./src/images/6.png");。另请注意,图像不依赖于模具的面部优势。

    }

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

设置骰子的面值,然后设置图像,但图像根本不依赖于面值。

    }

    public int getValue() {
        return faceValue;
    }
}

因此,基本上,setImage()需要使用面值,并根据面值设置图像。它也应该是私密的:你不希望任何人改变骰子的形象而不改变它的面值。

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