簡體   English   中英

如何在按下時將圖標設置為 JButton 而不會丟失 animation?

[英]How do I set an Icon to a JButton without losing the animation when it's pressed?

我使用在線工具創建了一個自定義按鈕圖標,然后我下載了 .png 文件。 我已經在 JButton(Swing 應用程序)中設置了圖標,但是當按下按鈕時,單擊按鈕時不會顯示常規的 animation。 是否可以設置一個圖標而不會失去這種效果? 謝謝(這是代碼)

public final JButton SEARCH = new JButton();
SEARCH.setBorder(null);
SEARCH.setContentAreaFilled(false);

ImageIcon searchb = new ImageIcon(getClass().getClassLoader().getResource("search.png"));
SEARCH.setIcon(searchb);

您必須根據您的要求使用不同的圖標(圖像)。 這意味着您必須使用另一個類似於使用原始圖像按下按鈕的圖像

見下文

JButton b = new JButton();
b.setPressedIcon(); // shows when button is pressed 
b.setSelectedIcon(); // shows when button is selected(applicable for toggle buttons) 
b.setDisabledIcon(); // shows when button is disabled 
b.setRolloverIcon(); // shows when mouse pointer hovering on the button

在此處輸入圖像描述

您可以禁用行setBorder(null); setContentAreaFilled(false); setBorder(null); setContentAreaFilled(false); 為了享受默認的 animation。 animation 保留,圖像在它上面。

public final JButton SEARCH = new JButton();
//SEARCH.setBorder(null);
//SEARCH.setContentAreaFilled(false);

ImageIcon searchb = new ImageIcon(getClass().getClassLoader().getResource("search.png"));
SEARCH.setIcon(searchb);

暫無
暫無

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

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