【发布时间】:2015-03-24 16:01:01
【问题描述】:
以下是“Deck”对象中的一个方法,它为我可能编写的各种纸牌游戏实例化和操作一组 Card 对象。此特定方法接受一个 JFrame 和一个 x 和 y 坐标作为参数,然后在该 JFrame 上的该位置创建一个 JLayeredPane 以显示牌组中的卡片。一切正常,除了当它显示卡片时 JLayeredPane 不遵守 zOrd 定义的 Z 索引并且卡片以我希望它们的相反顺序显示。
我在同一个 Jframe 上有一个 JLayeredPane,我将它传递给我在设计时放置在那里的这个方法,我使用非常相似的代码来遍历卡片组并在那里显示它们,它工作正常。所以我认为我的 JLayeredPane 有一些东西我没有在运行时做或设置,它需要尊重 z 索引,但我不知道是什么。
非常感谢任何帮助。
public void display(JFrame DisplayFrame, Integer x, Integer y) {
int xPos = 0;
int zOrd = 0;
JPanel contentPane = (JPanel)DisplayFrame.getContentPane();
JLayeredPane lp = new JLayeredPane();
lp.setLayout(new FlowLayout());
Integer height = _cards[0].getCardImage().getHeight();
Integer width = _cards.length * 15 + _cards[0].getCardImage().getWidth() - 15;
lp.setBounds(x, y, width, height);
contentPane.add(lp);
for (Card _card : _cards) {
JLabel cardLabel = new JLabel();
cardLabel.setIcon(new ImageIcon(_card.getCardImage()));
cardLabel.setBounds(xPos, 0, _card.getCardImage().getWidth(), _card.getCardImage().getHeight());
lp.add(cardLabel, zOrd);
xPos+=15;
zOrd++;
}
}
【问题讨论】:
-
the cards appear in the reverse order that I wish them to.- 你希望它们如何出现?我们不是读心术的人。发布一个正确的SSCCE 来证明问题。在创建SSCCE时,您可能会发现问题。 -
我应该更清楚。每张卡片都出现在前一张卡片的下方。我希望它出现在上一张卡片的顶部。
-
您不应该设置分层窗格的布局。不知道这是不是唯一的问题。阅读我的建议。其中之一将解决您的问题。
标签: java swing jlayeredpane