簡體   English   中英

java - 當鼠標懸停在它上面時,獲取一個改變顏色的按鈕

[英]java - get a button to change color when the mouse hovers over it

所以我想為一個簡單的java游戲制作一個菜單,當鼠標懸停在它們上面時按鈕會改變顏色。 我沒有使用JButton,而是使用mouselistener來檢測點擊的按鈕圖片。 當我將鼠標懸停在按鈕所在的特定區域時,如何進行鼠標調用?

public void mouseEntered(MouseEvent e) {
    if(e.getX() < 950 && e.getX() > 350 && e.getY() < 300 && e.getY() > 200){
        menuImage2 = true;
        menuImage1 = false;
    }
}

這就是我到目前為止所擁有的

這就是我做到的。 它也播放聲音。 你只需使用旗幟。 請注意,我使用了mouseMoved而不是mouseEntered。

類MouseInput

@Override
public void mouseMoved(MouseEvent e) {
        int x=e.getX();
        int y=e.getY();

        if (x>100&&x<200&&y>150&&y<200) {
            if (mouseInStart==false) {     <---- if this line is true, means mouse entered for first time.
                Sound.playSound(soundEnum.BUTTONHOVER);
            }
            mouseInStart=true;
        } else {
            mouseInStart=false;
        }
}

public boolean mouseInStart() {    <--use this in your update method
    return mouseInStart;
}

而在我的其他課堂(課程菜單)

public void render(Graphics2D g) {
    ....
    ....
    gradient = new GradientPaint(100, 150, setStartColor(), 200, 200,        Color.gray);
    g.setPaint(gradient);
    g.fill(startButton);

} 

public Color setStartColor() {
    if (mouseInStart) {
        return Color.red;
    } else {
        return Color.white;
    }
}


public void update() {     <--- and this is to keep checking if your mouse is in start.  This is part of the giant game loop.
    mouseInStart=mouseInput.mouseInStart();
    mouseInLoad=mouseInput.mouseInLoad();
    mouseInQuit=mouseInput.mouseInQuit();   
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM