【问题标题】:How do I set an Icon to a JButton without losing the animation when it's pressed?如何在按下时将图标设置为 JButton 而不会丢失动画?
【发布时间】:2021-04-06 02:10:46
【问题描述】:

我使用在线工具创建了一个自定义按钮图标,然后我下载了 .png 文件。 我已经在 J​​Button(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);

【问题讨论】:

    标签: java swing animation icons jbutton


    【解决方案1】:

    您必须根据您的要求使用不同的图标(图像)。 这意味着您必须使用另一个类似于使用原始图像按下按钮的图像

    见下文

    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
    

    【讨论】:

    • 非常感谢您的回复速度。我马上试试你的方法。祝你有美好的一天!
    • 所有建议的方法都以Icon 作为参数
    【解决方案2】:

    您可以禁用线条setBorder(null); setContentAreaFilled(false); 以享受默认动画。动画停留,图像在它上面。

    public final JButton SEARCH = new JButton();
    //SEARCH.setBorder(null);
    //SEARCH.setContentAreaFilled(false);
    
    ImageIcon searchb = new ImageIcon(getClass().getClassLoader().getResource("search.png"));
    SEARCH.setIcon(searchb);
    

    【讨论】:

      猜你喜欢
      • 2012-06-11
      • 2016-01-21
      • 2013-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多