1

我编写了一个扩展 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")));

我究竟做错了什么?

图标不会出现在程序中。

4

2 回答 2

3
  1. 不要ImageIcon在运行时重新创建和重新加载 s,将所有ImageIcons 作为局部变量加载一次且仅一次,

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

  3. 为 JToggleButton使用正确的 setXxxIcon 方法

于 2012-07-18T14:19:48.197 回答
2

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

于 2012-07-18T14:50:06.830 回答