只需要一点帮助就可以在我的代码中找到错误(?),将布尔 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,则显示单击/释放图像。如果它处于活动状态或不活动状态,它也会在系统框中输出(?)(不确定它叫什么)