【问题标题】:Boolean value changes when run运行时布尔值变化
【发布时间】:2012-12-15 20:45:11
【问题描述】:

只需要一点帮助来定位我的代码中的错误(?),将布尔 avtive 的默认值设置为 false,但是当我运行代码时,它神秘地变成了 true

    import javax.swing.*;
    import java.awt.*;
    import java.net.URL;

    @SuppressWarnings("serial")
    public class openScreenBuild extends JPanel{
    String picPath = "pictures/";
    String[] fileName = { "openScreen.png", "playButtonPress.png",
    "playButtonRelease.png", "playButtonInactive.png" };
    ClassLoader cl = openScreenBuild.class.getClassLoader();
    URL imgURL[] = new URL[4];
    Toolkit tk = Toolkit.getDefaultToolkit();
    Image imgBG, imgPlayPress, imgPlayRelease, imgPlayInactive;
    Boolean active=false, playPress = false;

    public openScreenBuild() throws Exception {
        for (int x = 0; x < 4; x++) {
            imgURL[x] = cl.getResource(picPath + fileName[x]);
        }
        imgBG = tk.createImage(imgURL[0]);
        imgPlayPress = tk.createImage(imgURL[1]);
        imgPlayRelease = tk.createImage(imgURL[2]);
        imgPlayInactive = tk.createImage(imgURL[3]);
    }
    public void updateScreen(){
        repaint();
    }
    public void paintComponent(Graphics g) {
        g.drawImage(imgBG, 0, 0, 600, 460, 0, 0, 600, 460, this);
        if (active=true){
            if (playPress == false)
                g.drawImage(imgPlayRelease, 410, 355, 590, 450, 0, 0, 163, 87, this);
            else if (playPress == true)
                g.drawImage(imgPlayPress, 410, 355, 590, 450, 0, 0, 163, 87, this);
            System.out.println("Active");
        }
        else if(active=false){
            g.drawImage(imgPlayInactive, 410, 355, 590, 450, 0, 0, 163, 87, this);
            System.out.println("Inactive");
        }
        g.setColor(Color.WHITE);
        g.drawString("ABOUT PROGRAM STUFF", 25, 375);
    }
    public void checkCoord(Point point){
        int xPos=(int)point.getX();
        int yPos=(int)point.getY();
        if (active==true){
            if ((yPos>=355)&&(yPos<=450)&&(xPos>=410)&&(xPos<=590))
                playPress=true;
        }
        updateScreen();
    }
    public void resetScreen(){
        playPress=false;
        updateScreen();
    }
}

如您所见,如果 active 为 false,则应显示非活动播放按钮图像,但如果为 true,则显示单击/释放图像。如果它处于活动状态或不活动状态,它还会在系统框中输出(?)(不确定它叫什么)

【问题讨论】:

    标签: java eclipse graphics boolean


    【解决方案1】:
     if (active=true)
    

    将 active 赋值为 true。你想要:

     if (active==true)
    

    【讨论】:

    • 谢谢,我不知道为什么 eclipse 没有给我一个错误,但是是的,谢谢 :)
    • 因为active=true assgins active true 的值然后检查该语句是否为true。也不建议尝试针对 true 进行尝试,而只需键入 if (active) 你得到的是 if ( (active=true) == true)
    猜你喜欢
    • 2021-02-03
    • 1970-01-01
    • 2020-08-06
    • 1970-01-01
    • 2020-08-22
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 2011-04-10
    相关资源
    最近更新 更多