我有一个纸牌游戏,它建立在使用ArrayList将人员存储在人手中。我有一个主要的方法,通过游戏进行游戏并使用主类中的InvokeLater线程更新GUI类Java Swing:等待鼠标在返回一个Int之前点击事件
当前我正在控制台上玩游戏,并将其输入到扫描仪中以使从玩家手中选择牌。我想要做的是运行一个线程,可能带有调用和等待,当玩家需要做出选择时运行。然后等待玩家在GUI中点击相应的卡并返回该卡位置的合适的int。这个选择然后由我的游戏处理。
我现在如何更新我的GUI。
public static void Play(ArrayList<Card> ...){
UpdateHand(player1, deck, deckused);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
GUI.getInstance().UpdateHand();
}
});
将听众附加到卡片上并将他们的名字设置为手中相应位置的int。
public void PlayerSelection()
{
selection = -1;
Information = new JLabel("Please Select Your Card");
ButtonDisplay.add(Information);
ButtonDisplay.updateUI();
for (int i = 0; i < (HumanHand.size()); i++)
{
Card card = HumanHand.get(i);
BufferedImage cardImage = null;
try {
cardImage = ImageIO.read(new File("card/" + card + ".jpg"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JLabel picLabel = new JLabel(new ImageIcon(cardImage));
String name = "" + i;
picLabel.setName(name);
picLabel.addMouseListener((MouseListener) this);
HumanHandDisplay.add(picLabel);
}
}
在单击鼠标时存储用于选择的int。
public void mouseClicked(MouseEvent e) {
String name = ((JLabel)e.getSource()).getName();
System.out.println("Working " + name);
selection = Integer.parseInt(name);
}
我想将此int返回到我的主类Play()方法中,以用于其余的计算。我无法解决如何做到这一点,特别是要等待选择。我听说这可以完成,但我不确定如何在我的工作中实现这一点。如果任何人都可以提供帮助,那会很棒。
请学习java命名约定并坚持使用它们。 – kleopatra 2012-04-16 08:18:06