【问题标题】:Self created JToggleButton: change icons自创 JToggleButton:更改图标
【发布时间】:2012-07-18 13:21:24
【问题描述】:

我编写了一个扩展 JToggleButton 的类。

一切正常,除了我无法更改按钮的图标。

这是我的课程代码:

package be.blauweregen.lichtsturing;

import javax.swing.*;
import java.awt.*;

class MyToggleButton extends JToggleButton {
    private static final long serialVersionUID = 1L;
    String s;

    public MyToggleButton(String str) {
        super(str);
        s = str;
    }

    public MyToggleButton(String str, Boolean sel) {
        super(str, sel);
        s = str;
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (this.isSelected() && this.isEnabled()) { // manueel en aan
            int w = getWidth();
            int h = getHeight();
            g.setColor(Color.green); // selected color
            g.fillRect(0, 0, w, h);
            g.setColor(Color.black); // selected foreground color
            g.drawString(s, (w - g.getFontMetrics().stringWidth(s)) / 2 + 1,
                    (h + g.getFontMetrics().getAscent()) / 2 - 1);
            setFont(new Font("Tahoma", Font.BOLD, 11));
        } else if (this.isSelected() && !this.isEnabled()) { // automatisch en
                                                                // aan
            int w = getWidth();
            int h = getHeight();

            g.setColor(Color.green); // selected color
            g.fillRect(0, 0, w, h);
            g.setColor(Color.black); // selected foreground color
            g.drawString(s, (w - g.getFontMetrics().stringWidth(s)) / 2 + 1,
                    (h + g.getFontMetrics().getAscent()) / 2 - 1);
            setFont(new Font("Tahoma", Font.PLAIN, 11));
        } else if (!this.isSelected() && this.isEnabled()) { // manueel en uit
            int w = getWidth();
            int h = getHeight();
            g.setColor(Color.red); // selected color
            g.fillRect(0, 0, w, h);
            g.setColor(Color.black); // selected foreground color
            g.drawString(s, (w - g.getFontMetrics().stringWidth(s)) / 2 + 1,
                    (h + g.getFontMetrics().getAscent()) / 2 - 1);
            setFont(new Font("Tahoma", Font.BOLD, 11));
        } else if (!this.isSelected() && !this.isEnabled()) { // automatisch en
                                                                // uit
            int w = getWidth();
            int h = getHeight();
            g.setColor(Color.red); // selected color
            g.fillRect(0, 0, w, h);
            g.setColor(Color.black); // selected foreground color
            g.drawString(s, (w - g.getFontMetrics().stringWidth(s)) / 2 + 1,
                    (h + g.getFontMetrics().getAscent()) / 2 - 1);
            setFont(new Font("Tahoma", Font.PLAIN, 11));
        }

    }
}

我在我的程序中这样使用代码:

btnGangSanitair = new MyToggleButton("Gang sanitair");
btnGangSanitair.setSelectedIcon(new ImageIcon("Z:\\Development\\Java\\BlauweRegen\\famfamfam_silk_icons_v013\\icons\\application_edit.png"));
btnGangSanitair.setIcon(new ImageIcon(Client.class.getResource("/be.blauweregen.icons/arrow_refresh.png")));

我做错了什么?

图标不会出现在程序中。

【问题讨论】:

  • 如需尽快获得更好的帮助,请发帖SSCCE

标签: java swing icons jtogglebutton


【解决方案1】:
  1. 不要在运行时重新创建和重新加载ImageIcons,将所有ImageIcons 作为局部变量加载一次且仅一次,

  2. 使用setBackground() 代替paintComponent()

  3. 使用proper setXxxIcon methods for JToggleButton

【讨论】:

    【解决方案2】:

    除了 mKorbel 的 cmets,您所有精彩的自定义绘画都超过了 super 调用所做的绘画,因此绘画在您的图标上

    【讨论】:

    • 感谢您的评论。只是打字说我使用了这种方法。你太快了 10 秒。谢谢你的回答。
    猜你喜欢
    • 2014-09-24
    • 1970-01-01
    • 2020-05-10
    • 2011-12-14
    • 2011-08-14
    • 2012-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多