⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 officexpmenuui.java

📁 java lookandfeel office pack
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			paintSubmenuBackground(g, menu);
		}
		// Otherwise, this is a top-level menu.
		else {
			paintTopLevelMenuBackground(g, menu);
		}
    }


	protected void paintIcon(Graphics g, JMenuItem menuItem) {
		OfficeXPUtilities.paintMenuItemIcon(g, menuItem, iconRect);
	}


	protected void paintMenuItem(Graphics g, JComponent c,
                                     Icon checkIcon, Icon arrowIcon,
                                     Color background, Color foreground,
                                     int defaultTextIconGap) {

		JMenu menu = (JMenu)c;

		// For a top-level menu item, paint regularly (could be optimized...).
		if (menu.isTopLevelMenu())
			super.paintMenuItem(g, c, checkIcon, arrowIcon, background,
							foreground, defaultTextIconGap);

		// Otherwise, it must be painted like an OfficeXPMenuItem
		// (but optimized a tad).
		else {

			JMenuItem b = (JMenuItem) c;
			ButtonModel model = b.getModel();

			//   Dimension size = b.getSize();
			int menuWidth = b.getWidth();
			int menuHeight = b.getHeight();

			resetRects();

			viewRect.setBounds( 0, 0, menuWidth, menuHeight );

			Font holdf = g.getFont();
			Font f = c.getFont();
			g.setFont( f );
			FontMetrics fm = c.getFontMetrics( f );

			// layout the text and icon
			String text = layoutMenuItem(
				fm, b.getText(), b.getIcon(),
				arrowIcon, viewRect, iconRect, textRect, arrowIconRect,
				b.getText() == null ? 0 : defaultTextIconGap,
				defaultTextIconGap
			);

			// Paint background
			paintBackground(g, b, background);

			Color holdc = g.getColor();

			// Paint the Icon
			paintIcon(g, menuItem);

			// 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);

			}

			// Paint the Arrow
			if (arrowIcon != null) {
				arrowIcon.paintIcon(c, g, arrowIconRect.x, arrowIconRect.y);
			}

			g.setColor(holdc);
			g.setFont(holdf);

		}

	}


	/*
	 * (non-Javadoc) NOTE:  This is NOT the same as
	 * OfficeXPMenuItemUI.paintBackground() - see "if (model.isSelected())"
	 * line...
	 */
	protected void paintSubmenuBackground(Graphics g, JMenu menu) {

		ButtonModel model = menu.getModel();
		Color oldColor = g.getColor();
		int menuWidth = menu.getWidth();
		int menuHeight = menu.getHeight();

		paintUnarmedBackground(g, menu);

		//if (model.isArmed() || model.isSelected()) {
		if (model.isSelected()) {

			int width = menuWidth - 3;
			int height = menuHeight - 2;

			g.setColor(UIManager.getColor("OfficeLnF.HighlightBorderColor"));
			g.drawRect(1,0, width,height);
			g.setColor(UIManager.getColor("OfficeLnF.HighlightColor"));
			g.fillRect(2,1, width-1,height-1);
			g.setColor(oldColor);

		}
		else {
			// Do nothing; the background has already been painted above.
		}

		// Add the white line to the bottom item.  Note that this CANNOT be added as
		// a part of the popup menu's border because of Office XP's menu item design;
		// there's an empty line between each menu item, but the top and bottom empty
		// lines are pure background color (no "khaki" on the left).  If you can think
		// of a simpler way to do it, then by all means, go ahead.
		Component parent = menu.getParent();
		if (parent instanceof JPopupMenu) {
			JPopupMenu popupMenu = (JPopupMenu)parent;
			if (popupMenu.getComponentIndex(menu) ==
					popupMenu.getComponentCount()-1) {
				g.setColor(menu.getBackground());
				int y = menuHeight - 1;
				// Do whole line to cover both LTR and RTL.
				g.drawLine(0,y, menuWidth-1,y);
			}
		}

		g.setColor(oldColor);

	}


    /**
     * Method which renders the text of the current menu item.
     *
     * @param g Graphics context
     * @param menuItem Current menu item to render
     * @param textRect Bounding rectangle to render the text.
     * @param text String to render
     */
    protected void paintText(Graphics g, JMenuItem menuItem,
    							Rectangle textRect, String text) {

		ButtonModel model = menuItem.getModel();

		if(!model.isEnabled()) {
		    OfficeXPGraphicsUtils.paintText(g, menuItem, textRect, text, 0);
		}
		else {
		    FontMetrics fm = menuItem.getFontMetrics(g.getFont());
		    int mnemonicIndex = menuItem.getDisplayedMnemonicIndex();
	 	    OfficeXPGraphicsUtils.drawStringUnderlineCharAt(g, fm, text,
					mnemonicIndex, textRect.x, textRect.y+fm.getAscent());
		}

    }


	protected void paintTopLevelMenuBackground(Graphics g, JMenu menu) {

		ButtonModel model = menu.getModel();
		Color oldColor = g.getColor();
		int menuWidth = menu.getWidth();
		int menuHeight = menu.getHeight();

		if (model.isArmed() || model.isSelected()) {

			// Fill-in with color.
			g.setColor(UIManager.getColor("OfficeXPLnF.ChosenMenuColor"));
			g.fillRect(0,0, menuWidth,menuHeight);

			// Draw the "border."
			JPopupMenu popupMenu = menu.getPopupMenu();
			JMenuBar menuBar = (JMenuBar)menu.getParent();
			Point menuLocation = menu.getLocation();
			Point popupMenuLocation = SwingUtilities.convertPoint(popupMenu,
									popupMenu.getLocation(), menuBar);
			int newX = menuLocation.x - popupMenuLocation.x + 1;
			g.setColor(UIManager.getDefaults().getColor("OfficeLnF.MenuBorderColor"));
			// If the popup menu is below the menu bar or the popup isn't visible
			// (happens if the user presses "Alt" to activate the menus).
			if (menuLocation.y<popupMenuLocation.y || !menu.isPopupMenuVisible()) {

				g.drawLine(0,2, 0,menuHeight-1);
				g.drawLine(0,2, menuWidth-5,2);
				g.drawLine(menuWidth-5,2, menuWidth-5,menuHeight-1);
/*
				// Shadow - NOT ACCURATE!!
				g.setColor(new Color(225,225,225));
				g.fillRect(menuWidth-4,6,  4,menuHeight);
				g.setColor(new Color(210,210,210));
				g.fillRect(menuWidth-4,8,  3,menuHeight);
				g.setColor(new Color(180,180,180));
				g.drawLine(menuWidth-4,10,  menuWidth-4,menuHeight);
				g.setColor(new Color(195,195,195));
				g.drawLine(menuWidth-3,10, menuWidth-3,menuHeight);
*/
			}
			// If the popup menu is above the menu bar.
			else {

				g.drawLine(0,0, 0,menuHeight-2);
				g.drawLine(0,menuHeight-2, menuWidth-5,menuHeight-2);
				g.drawLine(menuWidth-5,0, menuWidth-5,menuHeight-2);

				// Shadow ... ???

			}

		}

		else {

			// Needed by both items below.
			g.setColor(menu.getBackground());
			g.fillRect(0,0, menuWidth, menuHeight);

			if (isMouseOver() && model.isEnabled()) {
				g.setColor(UIManager.getColor("OfficeLnF.HighlightBorderColor"));
				g.drawRect(0,2, menuWidth-5,menuHeight-3);
				g.setColor(UIManager.getColor("OfficeLnF.HighlightColor"));
				g.fillRect(1,3, menuWidth-6,menuHeight-4);
			}

			else {
				// Background filled in above.
			}

		}

		g.setColor(oldColor);

	}


	protected void paintUnarmedBackground(Graphics g, JMenuItem menuItem) {
		OfficeXPUtilities.paintMenuItemBackground(g, menuItem);
	}


    protected void resetRects() {
        iconRect.setBounds(zeroRect);
        textRect.setBounds(zeroRect);
        arrowIconRect.setBounds(zeroRect);
        viewRect.setBounds(0,0,Short.MAX_VALUE, Short.MAX_VALUE);
    }


    /**
     * Set the temporary flag to indicate if the mouse has entered the menu.
     */
    private void setMouseOver(boolean over) {
		isMouseOver = over;
    }


    /**
     * Overrides the handler in WindowsMenuUI for some GUI changes.
     */
    protected class MouseInputHandler extends WindowsMenuUI.MouseInputHandler {

		public void mouseEntered(MouseEvent e) {
			super.mouseEntered(e);
			if (!OfficeXPLookAndFeel.isClassicWindows()) {
				setMouseOver(true);
				menuItem.repaint();
			}
		}

		public void mouseExited(MouseEvent e) {
			super.mouseExited(e);
			if (!OfficeXPLookAndFeel.isClassicWindows()) {
				setMouseOver(false);
				menuItem.repaint();
			}
		}

    }


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -