📄 menuitemui.java
字号:
if (accelerator != null)
{
int modifiers= accelerator.getModifiers();
if (modifiers > 0)
{
acceleratorText= KeyEvent.getKeyModifiersText(modifiers);
//acceleratorText += "-";
acceleratorText += acceleratorDelimiter;
}
int keyCode= accelerator.getKeyCode();
if (keyCode != 0)
{
acceleratorText += KeyEvent.getKeyText(keyCode);
}
else
{
acceleratorText += accelerator.getKeyChar();
}
}
Font font= b.getFont();
FontMetrics fm= b.getToolkit().getFontMetrics(font);
FontMetrics fmAccel= b.getToolkit().getFontMetrics(acceleratorFont);
resetRects();
int maxTextWidth=calcTextWidth(b);
int maxAccWidth=calcAcceleratorWidth(b);
boolean arrowUsed=isArrowUsed(b);
boolean iconUsed=isIconUsed(b);
layoutMenuItem(
fm,
text,
fmAccel,
acceleratorText,
icon,
checkIcon,
arrowIcon,
b.getVerticalAlignment(),
b.getHorizontalAlignment(),
b.getVerticalTextPosition(),
b.getHorizontalTextPosition(),
viewRect,
iconRect,
textRect,
acceleratorRect,
checkIconRect,
arrowIconRect,
text == null ? 0 : defaultTextIconGap,
defaultTextIconGap,
maxTextWidth,
maxAccWidth,
arrowUsed,
iconUsed);
w=viewRect.width;
h=viewRect.height;
return new Dimension(w,h);
}
/** Updates the visual appearance of the specified component.
* Invokes paint().
*
* @see #paint(Graphics, JComponent)
*/
public void update(Graphics g, JComponent c)
{
paint(g, c);
}
/** Returns true if any icon is used in this menu item or other menu items
* at the same hierarchy level in the menu tree.
*/
private boolean isIconUsed(JMenuItem item)
{
Object p=item.getParent();
if(p!=null)
{
if(p instanceof JMenu)
{
JMenu menu=(JMenu)p;
for(int i=0; i<menu.getMenuComponentCount(); i++)
{
JMenuItem currItem=(JMenuItem)menu.getMenuComponent(i);
if(currItem.getIcon()!=null || (currItem instanceof JCheckBoxMenuItem) ||
(currItem instanceof JRadioButtonMenuItem))
return true;
}
}
else if(p instanceof JPopupMenu)
{
JPopupMenu menu=(JPopupMenu)p;
for(int i=0; i<menu.getComponentCount(); i++)
{
Object o=menu.getComponent(i);
if(o instanceof JMenuItem)
{
JMenuItem currItem=(JMenuItem)o;
if(currItem.getIcon()!=null || (currItem instanceof JCheckBoxMenuItem) ||
(currItem instanceof JRadioButtonMenuItem))
return true;
}
}
}
}
return false;
}
/** Returns true, if any arrow is used in this item or any item on the
* same hierarchy level in the menu tree.
*/
private boolean isArrowUsed(JMenuItem item)
{
Object p=item.getParent();
if(p!=null)
{
if(p instanceof JMenu)
{
JMenu menu=(JMenu)p;
for(int i=0; i<menu.getMenuComponentCount(); i++)
{
JMenuItem currItem=(JMenuItem)menu.getMenuComponent(i);
if(currItem instanceof JMenu)
return true;
}
}
else if(p instanceof JPopupMenu)
{
JPopupMenu menu=(JPopupMenu)p;
for(int i=0; i<menu.getComponentCount(); i++)
{
Object o=menu.getComponent(i);
if(o instanceof JMenu)
return true;
}
}
}
return false;
}
/** Calculates and returns the width of the text displayed on this
* menu item.
*/
private int calcTextWidth(JMenuItem item)
{
Object p=item.getParent();
FontMetrics fm=item.getFontMetrics(item.getFont());
int maxTextWidth=fm.stringWidth(item.getText());
if(p!=null)
{
if(p instanceof JMenu)
{
JMenu menu=(JMenu)p;
for(int i=0; i<menu.getMenuComponentCount(); i++)
{
JMenuItem currItem=(JMenuItem)menu.getMenuComponent(i);
fm=currItem.getFontMetrics(currItem.getFont());
maxTextWidth=Math.max(maxTextWidth, fm.stringWidth(currItem.getText()));
}
}
else if(p instanceof JPopupMenu)
{
JPopupMenu menu=(JPopupMenu)p;
for(int i=0; i<menu.getComponentCount(); i++)
{
Object o=menu.getComponent(i);
if(o instanceof JMenuItem)
{
JMenuItem currItem=(JMenuItem)o;
fm=currItem.getFontMetrics(currItem.getFont());
maxTextWidth=Math.max(maxTextWidth, fm.stringWidth(currItem.getText()));
}
}
}
}
return maxTextWidth;
}
/** Returns the width of the accelerator text displayed on the specified
* menu item.
*/
private int calcAcceleratorWidth(JMenuItem item)
{
Object p=item.getParent();
FontMetrics fm=item.getFontMetrics(acceleratorFont);
int maxTextWidth=fm.stringWidth(getAcceleratorText(item.getAccelerator()));
if(p!=null)
{
if(p instanceof JMenu)
{
JMenu menu=(JMenu)p;
for(int i=0; i<menu.getMenuComponentCount(); i++)
{
JMenuItem currItem=(JMenuItem)menu.getMenuComponent(i);
maxTextWidth=Math.max(maxTextWidth, fm.stringWidth(getAcceleratorText(item.getAccelerator())));
}
}
else if(p instanceof JPopupMenu)
{
JPopupMenu menu=(JPopupMenu)p;
for(int i=0; i<menu.getComponentCount(); i++)
{
Object o=menu.getComponent(i);
if(o instanceof JMenuItem)
{
JMenuItem currItem=(JMenuItem)o;
maxTextWidth=Math.max(maxTextWidth, fm.stringWidth(getAcceleratorText(currItem.getAccelerator())));
}
}
}
}
return maxTextWidth;
}
/** Paints the specified component */
public void paint(Graphics g, JComponent c)
{
JMenuItem item=(JMenuItem)c;
int maxTextWidth=calcTextWidth(item);
int maxAccWidth=calcAcceleratorWidth(item);
boolean arrowUsed=isArrowUsed(item);
boolean iconUsed=isIconUsed(item);
paintMenuItem(
g,
c,
checkIcon,
arrowIcon,
selectionBackground,
selectionForeground,
defaultTextIconGap,
maxTextWidth,
maxAccWidth,
arrowUsed,
iconUsed);
}
/** Returns the accelerator text for the specified key stroke */
protected String getAcceleratorText(KeyStroke accelerator)
{
String acceleratorText= "";
if (accelerator != null)
{
int modifiers= accelerator.getModifiers();
if (modifiers > 0)
{
acceleratorText= KeyEvent.getKeyModifiersText(modifiers);
//acceleratorText += "-";
acceleratorText += acceleratorDelimiter;
}
int keyCode= accelerator.getKeyCode();
if (keyCode != 0)
{
acceleratorText += KeyEvent.getKeyText(keyCode);
}
else
{
acceleratorText += accelerator.getKeyChar();
}
}
return acceleratorText;
}
/** Paints the specified menu item.
*
* @param g The graphics context into which to paint
* @param c The component which to paint
* @param checkIcon The check mark icon (if any)
* @param arrowIcon The arrow icon (if any)
* @param background The background color to be used
* @param foreground The foreground color to be used
* @param defaultTextIconGap The default width in pixels between the
* icon and the text
* @param maxTextWidth The maximum width in pixels of the text
* @param maxAccWidth The maximum width in pixels of the accelerator
* @param arrowUsed If true, the arrow will be painted
* @param iconUsed If true, the icon will be painted
*/
protected void paintMenuItem(Graphics g, JComponent c, Icon checkIcon,
Icon arrowIcon, Color background, Color foreground, int defaultTextIconGap, int maxTextWidth,
int maxAccWidth, boolean arrowUsed, boolean iconUsed)
{
JMenuItem b= (JMenuItem) c;
ButtonModel model= b.getModel();
// Dimension size = b.getSize();
int menuWidth= b.getWidth();
int menuHeight= b.getHeight();
Insets i= c.getInsets();
resetRects();
viewRect.setBounds(0, 0, menuWidth, menuHeight);
// Add a left inset if menu item is not top-level menu
// Container parentC= menuItem.getParent();
// if (parentC != null && parentC instanceof JComponent
// && !(menuItem instanceof JMenu && ((JMenu) menuItem).isTopLevelMenu()))
// {
// viewRect.x += 16;
// }
viewRect.x += i.left;
viewRect.y += i.top;
viewRect.width -= (i.right + viewRect.x);
viewRect.height -= (i.bottom + viewRect.y);
Font holdf= g.getFont();
Font f= c.getFont();
g.setFont(f);
FontMetrics fm= g.getFontMetrics(f);
FontMetrics fmAccel= g.getFontMetrics(acceleratorFont);
// get Accelerator text
KeyStroke accelerator= b.getAccelerator();
String acceleratorText=getAcceleratorText(accelerator);
// layout the text and icon
String text=
layoutMenuItem(
fm,
b.getText(),
fmAccel,
acceleratorText,
b.getIcon(),
checkIcon,
arrowIcon,
b.getVerticalAlignment(),
b.getHorizontalAlignment(),
b.getVerticalTextPosition(),
b.getHorizontalTextPosition(),
viewRect,
iconRect,
textRect,
acceleratorRect,
checkIconRect,
arrowIconRect,
b.getText() == null ? 0 : defaultTextIconGap,
defaultTextIconGap,
maxTextWidth, maxAccWidth, arrowUsed, iconUsed);
// Paint background
paintBackground(g, b, background);
Color holdc= g.getColor();
// Paint the Check
if (checkIcon != null)
{
if (model.isArmed() || (c instanceof JMenu && model.isSelected()))
{
g.setColor(foreground);
}
else
{
g.setColor(holdc);
}
if (useCheckAndArrow())
{
if(b.isSelected())
checkIcon.paintIcon(c, g, checkIconRect.x, checkIconRect.y);
else
uncheckIcon.paintIcon(c, g, checkIconRect.x, checkIconRect.y);
}
g.setColor(holdc);
}
// Paint the Icon
if (b.getIcon() != null)
{
Icon icon;
if (!model.isEnabled())
{
icon= (Icon) b.getDisabledIcon();
}
else if (model.isPressed() && model.isArmed())
{
icon= (Icon) b.getPressedIcon();
if (icon == null)
{
// Use default icon
icon= (Icon) b.getIcon();
}
}
else
{
icon= (Icon) b.getIcon();
}
if (icon != null)
icon.paintIcon(c, g, iconRect.x, iconRect.y);
}
// Draw the Text
if (text != null)
{
View v= (View) c.getClientProperty(BasicHTML.propertyKey);
if (v != null)
{
v.paint(g, textRect);
}
else
{
paintText(g, b, textRect, text);
}
}
// Draw the Accelerator Text
if (acceleratorText != null && !acceleratorText.equals(""))
{
//Get the maxAccWidth from the parent to calculate the offset.
int accOffset= 0;
Container parent= menuItem.getParent();
if (parent != null && parent instanceof JComponent)
{
JComponent p= (JComponent) parent;
Integer maxValueInt= (Integer) p.getClientProperty(MAX_ACC_WIDTH);
int maxValue=
maxValueInt != null
? maxValueInt.intValue()
: acceleratorRect.width;
//Calculate the offset, with which the accelerator texts will be drawn with.
accOffset= maxValue - acceleratorRect.width;
}
g.setFont(acceleratorFont);
if (!model.isEnabled())
{
// *** paint the acceleratorText disabled
if (disabledForeground != null)
{
g.setColor(disabledForeground);
BasicGraphicsUtils.drawString(
g,
acceleratorText,
0,
acceleratorRect.x - accOffset,
acceleratorRect.y + fmAccel.getAscent());
}
else
{
g.setColor(b.getBackground().brighter());
BasicGraphicsUtils.drawString(
g,
acceleratorText,
0,
acceleratorRect.x - accOffset,
acceleratorRect.y + fmAccel.getAscent());
g.setColor(b.getBackground().darker());
BasicGraphicsUtils.drawString(
g,
acceleratorText,
0,
acceleratorRect.x - accOffset - 1,
acceleratorRect.y + fmAccel.getAscent() - 1);
}
}
else
{
// *** paint the acceleratorText normally
if (model.isArmed() || (c instanceof JMenu && model.isSelected()))
{
g.setColor(acceleratorSelectionForeground);
}
else
{
g.setColor(acceleratorForeground);
}
BasicGraphicsUtils.drawString(
g,
acceleratorText,
0,
acceleratorRect.x - accOffset,
acceleratorRect.y + fmAccel.getAscent());
}
}
// Paint the Arrow
if (arrowIcon != null)
{
g.setColor(Color.RED);
if (useCheckAndArrow())
{
if (model.isArmed() || (c instanceof JMenu && model.isSelected()))
{
if(invArrowIcon!=null)
invArrowIcon.paintIcon(c, g, arrowIconRect.x, arrowIconRect.y);
}
else
{
if(arrowIcon!=null)
arrowIcon.paintIcon(c, g, arrowIconRect.x, arrowIconRect.y);
}
}
}
g.setColor(holdc);
g.setFont(holdf);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -