简体   繁体   English

当我 hover 时如何使每个按钮突出显示

[英]How do i make each of the buttons highlight when i hover

I'm trying to make the buttons highlight (by changing color) when i am hovering above them with my mouse.当我用鼠标悬停在按钮上方时,我试图使按钮突出显示(通过改变颜色)。 this is my code;这是我的代码;

import javax.swing.JOptionPane;
final int BUTTON_WIDTH = 100;
final int BUTTON_HEIGHT = 50;

int buttonX = 0;
int COLOR = 150;

void setup() {
  size(800, 400);
}

void draw() {
  for (int i=0; i<=8; i++) {
    drawButtons();
    buttonX = buttonX+BUTTON_WIDTH;
  }
}

void drawButtons() {
  strokeWeight(2);
  fill(0, COLOR, COLOR);
  rect(buttonX, 0, BUTTON_WIDTH, BUTTON_HEIGHT);
 
}

I thought it would work if i add this;我认为如果我添加它会起作用;

if(mouseX<BUTTON_WIDTH && mouseY<BUTTON_HEIGHT){
    COLOR = 255;
  }

It does not work though, i am trying to make the button highlight when the mouse is over it and the other buttons would remain unchanged.但它不起作用,我试图在鼠标悬停时突出显示按钮,而其他按钮将保持不变。

I guess you should check the mouseX and mouseY in the draw (before drawing the buttons) like that:我想您应该像这样检查绘图中的 mouseX 和 mouseY (在绘制按钮之前):

void draw() {
  for (int i=0; i<=8; i++) {

    if(mouseX<BUTTON_WIDTH && mouseY<BUTTON_HEIGHT){
      COLOR = 255;
    }

    drawButtons();
    buttonX = buttonX+BUTTON_WIDTH;
  }
}

But the problem is that the new value for COLOR will affect every buttons...但问题是 COLOR 的新值会影响每个按钮......

I understand you can't work with arrays (that would be much simpler though) but without arrays i suggest that you reset COLOR to the default value (150) after the call to drawButtons.我知道你不能使用 arrays (虽然这会更简单)但没有 arrays 我建议你在调用 drawButtons 后将 COLOR 重置为默认值(150)。 So it would look like that:所以它看起来像这样:

void draw() {
  for (int i=0; i<=8; i++) {

    if(mouseX<BUTTON_WIDTH && mouseY<BUTTON_HEIGHT){
      COLOR = 255;
    }

    drawButtons();
    COLOR = 150;
    buttonX = buttonX+BUTTON_WIDTH;
  }
}

I hope it's what you were expecting !我希望这是你所期待的!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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