[英]How to set the minimum height and icon with of a text menu or context menu item?
我在Swing的文本菜單中使用大圖標。 如果為菜單項設置了圖標,則這些圖標將導致菜單行展開。 如果存在不帶圖標或帶有較小尺寸圖標的菜單項,則會導致以下標簽的位置不均勻,並且菜單項之間的距離也不同。
我現在可以:
還有其他方法可以為文本菜單中的無圖標菜單項設置菜單項圖標間隔的最小大小嗎?
最佳做法是為菜單項添加不帶圖標的空/透明圖標。 您可以使用以下方法合並圖標(以創建相同的寬度/高度):
private static Icon mergeIcons(Icon icon1, Icon icon2, int x, int y, Component c) {
int w = 0, h = 0;
if (icon1 != null) {
w = icon1.getIconWidth();
h = icon1.getIconHeight();
}
if (icon2 != null) {
w = icon2.getIconWidth() + x > w ? icon2.getIconWidth() + x : w;
h = icon2.getIconHeight() + y > h ? icon2.getIconHeight() + y : h;
}
if (w < 1) w = 16;
if (h < 1) h = 16;
java.awt.image.ColorModel model = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment ().
getDefaultScreenDevice ().getDefaultConfiguration ().
getColorModel (java.awt.Transparency.BITMASK);
java.awt.image.BufferedImage buffImage = new java.awt.image.BufferedImage (model,
model.createCompatibleWritableRaster (w, h), model.isAlphaPremultiplied (), null);
java.awt.Graphics g = buffImage.createGraphics ();
if (icon1 != null) {
icon1.paintIcon(c, g, 0, 0);
}
if (icon2 != null) {
icon2.paintIcon(c, g, x, y);
}
g.dispose();
return new ImageIcon(buffImage);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.