简体   繁体   中英

How to click and unclick to change color on a button?

My Netbeans GUI is set up, so that the color of the original buttons is green (modified the properties). I put code for each of my nine green buttons, that if you click it, it will turn grey as a sign as it's "selected." Now, I can't figure out how if you click it again, that it should turn back to green as "unselected." I think creating a boolean method would work best, but I'm not 100% positive.

private void btnTwoActionPerformed(java.awt.event.ActionEvent evt) {
    btnTwo.setBackground( backgroundColor );
}  

That's basically what it is so far, the backgroundColor is grey, and the original color I set it too was green.

只需使用if条件检查按钮是否被选中,然后根据该条件为按钮分配颜色。

You should use setEnabled() function so far if it takes true you have to use a color else if it is taking false give a color like:

      mybutton.setEnabled(true);

      mybutton.setEnabled(false);

or you may use isEnabled() method wether to check the button is enabled or not and then set the appropiate color you want have fun!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM