当程序加载时,已经有一个图像作为占位符
ImageIcon icon = new ImageIcon(Path + "Hangman1.jpg");
JLabel label = new JLabel();
label.setIcon(icon);
现在我知道如何编写打印正文部分的 if 语句
if (guesses >= 1) System.out.print("HEAD ");
if (guesses >= 2) System.out.print("BODY ");
if (guesses >= 3) System.out.print("LEFT ARM ");
if (guesses >= 4) System.out.print("RIGHT ARM ");
if (guesses >= 5) System.out.print("LEFT LEG ");
if (guesses >= 6) System.out.print("RIGHT LEG ");
但是我不想使用普通的 System.out.print 来调出我的图像(想要改变图像)
最佳答案
类似的东西
String imageName = null;
if (guesses >= 1) imageName = "Head.jpg";
if (guesses >= 2) imageName = "Body.jpg";
if (guesses >= 3) imageName = "LeftArm.jpg";
if (guesses >= 4) imageName = "RightArm.jpg";
if (guesses >= 5) imageName = "LeftLeg.jpg";
if (guesses >= 6) imageName = "RightLeg.jpg";
ImageIcon icon = null;
if (imageName != null) {
icon = new ImageIcon(Path + File.seperator + imageName);
}
label.setIcon(icon);
显然,每个图像都需要相互叠加...
已更新
正如 PaulBellora 所指出的那样,前面的示例是最简单的代码更改,需要满足 OP 的要求,但这并不意味着它是正确的
switch (guesses) {
case 1:
imageName = "Head.jpg";
break;
case 2:
imageName = "Body.jpg";
break;
case 3:
imageName = "LeftArm.jpg";
break;
case 4:
imageName = "RightArm.jpg";
break;
case 5:
imageName = "LeftLeg.jpg";
break;
case 6:
imageName = "RightLef.jpg";
break;
}
会是一个稍微好一点的方法。
通过一点巧妙的布局管理,您可以改为使用六个标签,每个 body 部位一个...
// Global references to the body parts
public static final ImageIcon RIGHT_LEG_ICON = new ImageIcon(Path + File.seperator + "RightLeg.jpg");
public static final ImageIcon LEFT_LEG_ICON = new ImageIcon(Path + File.seperator + "LeftLeg.jpg");
public static final ImageIcon RIGHT_ARM_ICON = new ImageIcon(Path + File.seperator + "RightArm.jpg");
public static final ImageIcon LEFT_ARM_ICON = new ImageIcon(Path + File.seperator + "LeftArm.jpg");
public static final ImageIcon BODY_ICON = new ImageIcon(Path + File.seperator + "Body.jpg");
public static final ImageIcon HEAD_ICON = new ImageIcon(Path + File.seperator + "Head.jpg");
// Used as fillers to allow the layout manager to maintain the layout
public static final ImageIcon BLANK_LEG_ICON = new ImageIcon(Path + File.seperator + "BlankLeg.jpg");
public static final ImageIcon BLANK_ARM_ICON = new ImageIcon(Path + File.seperator + "BlankArm.jpg");
public static final ImageIcon BLANK_BODY_ICON = new ImageIcon(Path + File.seperator + "BlankBody.jpg");
public static final ImageIcon BLANK_HEAD_ICON = new ImageIcon(Path + File.seperator + "BlanHead.jpg");
...
// Setup the initial state...(probably in the constructor or when the game rests)
rightLegLabel.setIcon(BLANK_LEG_ICON);
leftLegLabel.setIcon(BLANK_LEG_ICON);
rightArmLabel.setIcon(BLANK_ARM_ICON);
leftArmLabel.setIcon(BLANK_ARM_ICON);
bodyLabel.setIcon(BLANK_BODY_ICON);
headLabel.setIcon(BLANK_BODY_ICON);
...
// As the guesses change...
switch (guesses) {
case 6:
rightLegLabel.setIcon(RIGHT_LEG_ICON);
case 5:
leftLegLabel.setIcon(LEFT_LEG_ICON);
case 4:
rightArmLabel.setIcon(RIGHT_ARM_ICON);
case 3:
leftArmLabel.setIcon(LEFT_ARM_ICON);
case 2:
bodyLabel.setIcon(BODY_ICON);
case 1:
headLabel.setIcon(HEAD_ICON);
}
关于java - 使用 if 语句更改图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12065088/