I'm writing a Java Game similar to Tetris for a school project. In the game I have a checkbox to turn music on/off.
My problem is that the checkbox, even though I set it to be checked, is NOT checked when I click it (don't understand me wrong, not AFTER, but before/at the same moment when I clicked it).
Okay so, in my code where I first initialize the checkbox I set it to be checked based on a variable provided in another class.
I tried debugging everything that is going on with the checkbox but I didn't get anything I didn't already knew.
Here's the code where I initialize the box:
music_cbox = TexturesHandler.getCheckboxTemplate();
music_cbox.setName("Music");
music_cbox.addMouseListener(Retris.getButtonHandler());
music_cbox.setLocation(Retris.WIDTH / 2 - 25, 250);
// Setting it to checked based on the variable
if(!Retris.getAudioHandler().canPlay()) {
music_cbox.setIcon(TexturesHandler.getUncheckedCheckBoxStyle());
music_cbox.setSelected(false);
System.out.println("box not selected");
} else {
music_cbox.setIcon(TexturesHandler.getCheckedCheckBoxStyle());
music_cbox.setSelected(true);
System.out.println("box selected");
}
The canPlay variable only gets changed when you CLICK the box:
if(box.isSelected()) {
Retris.getAudioHandler().canPlay(false);
Retris.getAudioHandler().stopMusic();
System.out.println("Music disabled");
box.setIcon(TexturesHandler.getUncheckedCheckBoxStyle());
break;
// (in a switch statement)
} else {
Retris.getAudioHandler().canPlay(true);
Retris.getAudioHandler().startMusic("Main_Menu.wav", "Main Menu", 0.1F);
System.out.println("Music enabled");
box.setIcon(TexturesHandler.getCheckedCheckBoxStyle());
break;
}
I also tried box.setSelected()
after setting the new icon and all, but somehow when i first click the box, it does the else
part instead of the if
part.
Aucun commentaire:
Enregistrer un commentaire