【问题标题】:How to create and set oval icon for the JMenuItem如何为 JMenuItem 创建和设置椭圆形图标
【发布时间】:2013-05-25 16:51:37
【问题描述】:

我想为我的JMenuItem 创建一个带有椭圆形图标的 JMenu

所以我知道我应该使用:

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

但是如何创建那种图标:

【问题讨论】:

  • 只是谷歌点的图像?

标签: java swing icons jmenuitem


【解决方案1】:

您可以创建自己的Icon-implementation:

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;
    }
}

【讨论】:

    【解决方案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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-22
      • 2021-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-06
      • 1970-01-01
      相关资源
      最近更新 更多