[英]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.