如何为JMenuItem创建和设置椭圆形图标

Yod*_*oda 1 java icons swing jmenuitem

我想用我的椭圆形图标创建JMenu JMenuItem

所以我知道我应该使用:

JMenuItem item = new JMenuItem(title);
item.setIcon(icon)
Run Code Online (Sandbox Code Playgroud)

但是如何创建这种图标:

在此输入图像描述

Lon*_*ula 6

你可以自己Icon做 - 实现:

public class OvalIcon implements Icon {
    private int width;
    private int height;
    private Color color;

    public OvalIcon(int w, int h, Color color) {
        if((w | h) < 0) {
            throw new IllegalArgumentException("Illegal dimensions: "
                    + "(" + w + ", " + h + ")");
        }
        this.width  = w;
        this.height = h;
        this.color  = (color == null) ? Color.BLACK : color;
    }
    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {
        Color temp = g.getColor();
        g.setColor(color);
        g.fillOval(x, y, getIconWidth(), getIconHeight());
        g.setColor(temp);
    }
    @Override
    public int getIconWidth() {
        return width;
    }
    @Override
    public int getIconHeight() {
        return height;
    }
}
Run Code Online (Sandbox Code Playgroud)