簡體   English   中英

在鼠標懸停時更改 JButton 屬性

[英]Change JButton properties on mouse over

我正在用 swing 做一個計算器應用程序。 我已經更改了 JButton 的背景顏色,但是當單擊它時,它的顏色變為藍色。 我可以在鼠標懸停和單擊時手動設置顏色嗎? 像 windows 10 默認計算器?

嘗試使用這個:

buttonName.setFocusPainted(false);

當您單擊按鈕時,這將擺脫默認邊框。 要讓按鈕做一些事情,即改變它的背景,你可以為它實現 MouseListener 接口,但我認為對你來說最好的事情是閱讀 swing 中的事件處理:) 這是一個鏈接

您需要首先使用鼠標偵聽器檢測鼠標在按鈕上的移動。您可以使用它的實現版本,稱為MouseAdapter

 button.addMouseMotionListener(new MouseAdapter()//To 
 detect mouse motion
  {
   @override
   public void mouseEntered(MouseEvent m)//called when mouse 
enters first time into the button
  {
    button.setBackground(Color.blue);
   }

     @override
     public void mouseClicked(MouseEvent m)//called when mouse is clicked[pressed and then released]
      {   
     button.setBackground(//whatever you want);
      }
     });

注意鼠標點擊你可以改變你的動作監聽器內的顏色

暫無
暫無

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

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