我使用在线工具创建了自定义按钮图标,然后下载了 .png 文件。 我已经在 JButton(Swing 应用程序)中设置了图标,但是,当按下按钮时,它不会在单击按钮时显示常规动画。 是否可以在不失去这种效果的情况下设置图标? 谢谢(这是代码)
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
关于java - 如何将 Icon 设置为 JButton 而不会在按下时丢失动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65492282/