我有一整副卡片作为图像,我需要: a) 作为 JPanels 加载并且 b) 在 JFrame 中显示。
有没有更好的方法让它们进入我的程序,然后一遍又一遍(52x)编写类似的内容
final JPanel panelName = draw(new ImageIcon("spritesheet.gif"));
每个图像 (spritesheet.gif) 都有一个唯一的名称。这是一副纸牌。
这是绘制
public static JPanel draw(final ImageIcon img)
{
JPanel panel = new JPanel()
{
private static final long serialVersionUID = 1L;
//paintComponent is called automatically by the JRE whenever
//the panel needs to be drawn or redrawn
public void paintComponent(Graphics g) {
super.paintComponent(g);
img.paintIcon(this, g, 10, 10);
}
};
panel.setOpaque(false);
return panel;
}
最佳答案
each image has a unique name..
只要有某种模式,事情就很容易。例如。 "spades-queen.gif"
可以由花色数组 String[]
、级别数组、用于分隔它们的 -
组成& 末尾有一个 .gif
..
They're formatted like
aceSpades
and so on.
这是一个实现:
public class CardNames {
public final static String[] SUITS = {
"Spades", "Hearts", "Diamonds", "Clubs"
};
public final static String[] LEVELS = {
"ace", "two", "three", "four", "five", "six", "seven", "eight", "nine",
"ten", "jack", "queen", "king"
};
public final static String SEP = "";
public final static String XTN = ".gif";
public static void main(String[] args) {
for (String suit : SUITS) {
for (String level : LEVELS) {
System.out.println(level + SEP + suit + XTN);
}
}
}
}
输出
aceSpades.gif
twoSpades.gif
threeSpades.gif
fourSpades.gif
fiveSpades.gif
sixSpades.gif
sevenSpades.gif
eightSpades.gif
nineSpades.gif
tenSpades.gif
jackSpades.gif
queenSpades.gif
kingSpades.gif
aceHearts.gif
// ...
aceClubs.gif
twoClubs.gif
threeClubs.gif
fourClubs.gif
fiveClubs.gif
sixClubs.gif
sevenClubs.gif
eightClubs.gif
nineClubs.gif
tenClubs.gif
jackClubs.gif
queenClubs.gif
kingClubs.gif
关于java - 有没有更好的方法来添加大量带有附加到数组的独特图像的 JPanel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28467763/