我在一个gui中掷骰子。我得到我想要返回的数字(随机int 1-6),但我想要显示的图像没有显示出来。它第一次显示正确的图像,但从未改变它。下面是代码:JAVA。一个打印骰子的gui
public void layoutComponents()
{
Container container = frame.getContentPane();
container.setLayout(new GridLayout(1, 1));
diceRoll.setEditable(true);
container.add(button);
container.add(addPanel());
//container.add(diceRoll);
frame.setSize(200, 200);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
class DiceActions implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
layoutComponents();
//diceRoll.append(Double.toString((int)numberRolled));
}
}
public ImageIcon rollDice()
{
double numberRolled = (int)(Math.random() * 10 + 1);
ImageIcon image = dice1;
if (numberRolled > 6)
{
numberRolled -= 4;
}
if(numberRolled == 1)
{
image = dice1;
System.out.println("1");
System.out.println(numberRolled);
return image = dice1;
}
else if(numberRolled == 2)
{
image = dice2;
System.out.println("2");
return image = dice2;
}
else if(numberRolled == 3)
{
image = dice3;
System.out.println("3");
return image = dice3;
}
else if(numberRolled == 4)
{
image = dice4;
System.out.println("4");
return image = dice4;
}
else if(numberRolled == 5)
{
image = dice5;
System.out.println("5");
return image = dice5;
}
else if(numberRolled == 6)
{
image = dice6;
System.out.println("6");
return image = dice6;
}
else
System.out.println("never reached");
System.out.println(numberRolled);
return image;
}
public JPanel addPanel()
{
JPanel panel = new JPanel();
panel.add(new JLabel(rollDice()));
return panel;
}
public void addListeners()
{
DiceActions action = new DiceActions();
button.addActionListener(action);
}
它不断显示dice1图像 – user1198199 2012-03-05 02:16:43
1)为了更好地帮助您,请发布[SSCCE](http://sscce.org/)。 2)请为代码块使用一致的逻辑缩进。 – 2012-03-05 02:23:34
所以,你想让我们告诉你哪里的GUI代码是错误的,但你不给我们所有的代码。自行研究它的一种方法是将其削减为基本要素;如果你仍然没有发现你的问题,那么它可能足够小,可以发布整个事情。 – arcy 2012-03-05 02:24:03