1

我想为我的椭圆形图标创建 JMenuJMenuItem

所以我知道我应该使用:

JMenuItem item = new JMenuItem(title);
item.setIcon(icon)

但是如何创建那种图标:

在此处输入图像描述

4

2 回答 2

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;
    }
}
于 2013-05-25T17:16:40.847 回答
2

Playing With Shapes为您提供了一个ShapeIcon允许您创建许多不同形状的图标。例如:

Shape round = new Ellipse2D.Double(0, 0, 10, 10);
ShapeIcon red = new ShapeIcon(round, Color.RED);
ShapeIcon green = new ShapeIcon(round, Color.GREEN);
于 2013-05-25T19:10:59.267 回答