使用if语句更改图像

时间:2012-08-22 01:04:00

标签: java

当程序加载时,已存在占位符

的图像
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来调用我的图像(希望图像改变)

2 个答案:

答案 0 :(得分:2)

有点像

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;
}

稍微好一些。

通过一些巧妙的布局管理,您可以使用六个标签,每个身体部位一个......

// 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);
}

答案 1 :(得分:1)

你应该创建7个图像,每个图像都处于自己的“刽子手”状态。并在每次失败的猜测后加载适当的图像。改为使用System.out.print("HEAD ")来代替label.setIcon()来电。