简体   繁体   English

如何在按下时将图标设置为 JButton 而不会丢失 animation?

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

I have created a custom button icon using an online tool, and then I have downloaded the.png file.我使用在线工具创建了一个自定义按钮图标,然后我下载了 .png 文件。 I have already set the icon in the JButton (Swing Application), but, when the button is pressed, it doesn't show the regular animation when a button is clicked.我已经在 JButton(Swing 应用程序)中设置了图标,但是当按下按钮时,单击按钮时不会显示常规的 animation。 Is it possible to set an Icon without losing this effect?是否可以设置一个图标而不会失去这种效果? Thanks (here's the code)谢谢(这是代码)

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

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

You have to use different icons (images) based on your requirement.您必须根据您的要求使用不同的图标(图像)。 that means you have to use another image which resemblance pressed button using your original image这意味着您必须使用另一个类似于使用原始图像按下按钮的图像

see below见下文

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

在此处输入图像描述

You may disable lines setBorder(null); setContentAreaFilled(false);您可以禁用行setBorder(null); setContentAreaFilled(false); setBorder(null); setContentAreaFilled(false); in order to enjoy the default animation.为了享受默认的 animation。 The animation stays, and the image is over it. 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