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

📄 basicmenuitemui.java

📁 gcc的组建
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    // if menu item has accelerator then take accelerator's size into account    // when calculating preferred size.    KeyStroke accelerator = m.getAccelerator();    Rectangle rect;    if (accelerator != null)      {        rect = getAcceleratorRect(                                  accelerator,                                  m.getToolkit().getFontMetrics(acceleratorFont));        // add width of accelerator's text        d.width += rect.width + defaultAcceleratorLabelGap;        // adjust the heigth of the preferred size if necessary        if (d.height < rect.height)          d.height = rect.height;      }    if (checkIcon != null)      {        d.width += checkIcon.getIconWidth() + defaultTextIconGap;        if (checkIcon.getIconHeight() > d.height)          d.height = checkIcon.getIconHeight();      }    if (arrowIcon != null && (c instanceof JMenu))      {        int pWidth = m.getParent().getWidth();        if (!((JMenu)c).isTopLevelMenu() && d.width < pWidth)          d.width = pWidth          - m.getInsets().left - m.getInsets().right;        else          d.width += arrowIcon.getIconWidth() + MenuGap;                if (arrowIcon.getIconHeight() > d.height)          d.height = arrowIcon.getIconHeight();      }        return d;  }  /**   * Returns preferred size of the given component   *    * @param c   *          component for which to return preferred size   * @return $Dimension$ preferred size for the given component   */  public Dimension getPreferredSize(JComponent c)  {    return getPreferredMenuItemSize(c, checkIcon, arrowIcon, defaultTextIconGap);  }  /**   * Returns the prefix for entries in the {@link UIDefaults} table.   *    * @return "MenuItem"   */  protected String getPropertyPrefix()  {    return "MenuItem";  }  /**   * This method installs the components for this {@link JMenuItem}.   *    * @param menuItem   *          The {@link JMenuItem} to install components for.   */  protected void installComponents(JMenuItem menuItem)  {    // FIXME: Need to implement  }  /**   * This method installs the defaults that are defined in the Basic look and   * feel for this {@link JMenuItem}.   */  protected void installDefaults()  {    String prefix = getPropertyPrefix();    LookAndFeel.installBorder(menuItem, prefix + ".border");    LookAndFeel.installColorsAndFont(menuItem, prefix + ".background",                                     prefix + ".foreground", prefix + ".font");    menuItem.setMargin(UIManager.getInsets(prefix + ".margin"));    acceleratorFont = UIManager.getFont(prefix + ".acceleratorFont");    acceleratorForeground = UIManager.getColor(prefix + ".acceleratorForeground");    acceleratorSelectionForeground = UIManager.getColor(prefix + ".acceleratorSelectionForeground");    selectionBackground = UIManager.getColor(prefix + ".selectionBackground");    selectionForeground = UIManager.getColor(prefix + ".selectionForeground");    acceleratorDelimiter = UIManager.getString(prefix + ".acceleratorDelimiter");    checkIcon = UIManager.getIcon(prefix + ".checkIcon");        menuItem.setHorizontalTextPosition(SwingConstants.TRAILING);    menuItem.setHorizontalAlignment(SwingConstants.LEADING);  }  /**   * This method installs the keyboard actions for this {@link JMenuItem}.   */  protected void installKeyboardActions()  {    InputMap focusedWindowMap = SwingUtilities.getUIInputMap(menuItem, JComponent.WHEN_IN_FOCUSED_WINDOW);    if (focusedWindowMap == null)      focusedWindowMap = new ComponentInputMapUIResource(menuItem);    focusedWindowMap.put(menuItem.getAccelerator(), "doClick");    SwingUtilities.replaceUIInputMap(menuItem, JComponent.WHEN_IN_FOCUSED_WINDOW, focusedWindowMap);        ActionMap UIActionMap = SwingUtilities.getUIActionMap(menuItem);    if (UIActionMap == null)      UIActionMap = new ActionMapUIResource();    UIActionMap.put("doClick", new ClickAction());    SwingUtilities.replaceUIActionMap(menuItem, UIActionMap);  }  /**   * This method installs the listeners for the {@link JMenuItem}.   */  protected void installListeners()  {    menuItem.addMouseListener(mouseInputListener);    menuItem.addMouseMotionListener(mouseInputListener);    menuItem.addMenuDragMouseListener(menuDragMouseListener);    menuItem.addMenuKeyListener(menuKeyListener);    menuItem.addItemListener(itemListener);    menuItem.addPropertyChangeListener(propertyChangeListener);  }  /**   * Installs and initializes all fields for this UI delegate. Any properties of   * the UI that need to be initialized and/or set to defaults will be done now.   * It will also install any listeners necessary.   *    * @param c   *          The {@link JComponent} that is having this UI installed.   */  public void installUI(JComponent c)  {    super.installUI(c);    menuItem = (JMenuItem) c;    installDefaults();    installComponents(menuItem);    installListeners();    installKeyboardActions();  }  /**   * Paints given menu item using specified graphics context   *    * @param g   *          The graphics context used to paint this menu item   * @param c   *          Menu Item to paint   */  public void paint(Graphics g, JComponent c)  {    paintMenuItem(g, c, checkIcon, arrowIcon, c.getBackground(),                  c.getForeground(), defaultTextIconGap);  }  /**   * Paints background of the menu item   *    * @param g   *          The graphics context used to paint this menu item   * @param menuItem   *          menu item to paint   * @param bgColor   *          Background color to use when painting menu item   */  protected void paintBackground(Graphics g, JMenuItem menuItem, Color bgColor)  {    // Menu item is considered to be highlighted when it is selected.    // But we don't want to paint the background of JCheckBoxMenuItems    ButtonModel mod = menuItem.getModel();    if ((menuItem.isSelected() && checkIcon == null) || (mod != null &&         mod.isArmed())        && (menuItem.getParent() instanceof MenuElement))      {        if (menuItem.isContentAreaFilled())          {            g.setColor(selectionBackground);            g.fillRect(0, 0, menuItem.getWidth(), menuItem.getHeight());          }      }  }  /**   * Paints specified menu item   *    * @param g   *          The graphics context used to paint this menu item   * @param c   *          menu item to paint   * @param checkIcon   *          check icon to use when painting menu item   * @param arrowIcon   *          arrow icon to use when painting menu item   * @param background   *          Background color of the menu item   * @param foreground   *          Foreground color of the menu item   * @param defaultTextIconGap   *          space to use between icon and text when painting menu item   */  protected void paintMenuItem(Graphics g, JComponent c, Icon checkIcon,                               Icon arrowIcon, Color background,                               Color foreground, int defaultTextIconGap)  {    JMenuItem m = (JMenuItem) c;    Rectangle tr = new Rectangle(); // text rectangle    Rectangle ir = new Rectangle(); // icon rectangle    Rectangle vr = new Rectangle(); // view rectangle    Rectangle br = new Rectangle(); // border rectangle    Rectangle ar = new Rectangle(); // accelerator rectangle    Rectangle cr = new Rectangle(); // checkIcon rectangle    int vertAlign = m.getVerticalAlignment();    int horAlign = m.getHorizontalAlignment();    int vertTextPos = m.getVerticalTextPosition();    int horTextPos = m.getHorizontalTextPosition();        Font f = m.getFont();    g.setFont(f);    FontMetrics fm = g.getFontMetrics(f);    SwingUtilities.calculateInnerArea(m, br);    SwingUtilities.calculateInsetArea(br, m.getInsets(), vr);    paintBackground(g, m, m.getBackground());    /*     * MenuItems insets are equal to menuItems margin, space between text and     * menuItems border. We need to paint insets region as well.     */    Insets insets = m.getInsets();    br.x -= insets.left;    br.y -= insets.top;    br.width += insets.right + insets.left;    br.height += insets.top + insets.bottom;    // If this menu item is a JCheckBoxMenuItem then paint check icon    if (checkIcon != null)      {        SwingUtilities.layoutCompoundLabel(m, fm, null, checkIcon, vertAlign,                                           horAlign, vertTextPos, horTextPos,                                           vr, cr, tr, defaultTextIconGap);        checkIcon.paintIcon(m, g, cr.x, cr.y);        // We need to calculate position of the menu text and position of        // user menu icon if there exists one relative to the check icon.        // So we need to adjust view rectangle s.t. its starting point is at        // checkIcon.width + defaultTextIconGap.        vr.x = cr.x + cr.width + defaultTextIconGap;      }    // if this is a submenu, then paint arrow icon to indicate it.    if (arrowIcon != null && (c instanceof JMenu))      {        if (!((JMenu) c).isTopLevelMenu())          {            int width = arrowIcon.getIconWidth();            int height = arrowIcon.getIconHeight();            int offset = (vr.height - height) / 2;            arrowIcon.paintIcon(m, g, vr.width - width, vr.y + offset);          }      }    // paint text and user menu icon if it exists    Icon i = m.getIcon();    SwingUtilities.layoutCompoundLabel(c, fm, m.getText(), i, vertAlign,                                       horAlign, vertTextPos, horTextPos, vr,                                       ir, tr, defaultTextIconGap);    if (i != null)      i.paintIcon(c, g, ir.x, ir.y);    paintText(g, m, tr, m.getText());    // paint accelerator    String acceleratorText = "";    if (m.getAccelerator() != null)      {        acceleratorText = getAcceleratorText(m.getAccelerator());        fm = g.getFontMetrics(acceleratorFont);        ar.width = fm.stringWidth(acceleratorText);        ar.x = br.width - ar.width;        vr.x = br.width - ar.width - defaultTextIconGap;        SwingUtilities.layoutCompoundLabel(m, fm, acceleratorText, null,                                           vertAlign, horAlign, vertTextPos,                                           horTextPos, vr, ir, ar,                                           defaultTextIconGap);        paintAccelerator(g, m, ar, acceleratorText);      }  }  /**   * Paints label for the given menu item   *    * @param g   *          The graphics context used to paint this menu item   * @param menuItem   *          menu item for which to draw its label   * @param textRect   *          rectangle specifiying position of the text relative to the given   *          menu item   * @param text   *          label of the menu item   */  protected void paintText(Graphics g, JMenuItem menuItem, Rectangle textRect,                           String text)  {    Font f = menuItem.getFont();    g.setFont(f);    FontMetrics fm = g.getFontMetrics(f);    if (text != null && !text.equals(""))      {        if (menuItem.isEnabled())          {            // Menu item is considered to be highlighted when it is selected.            // But not if it's a JCheckBoxMenuItem            ButtonModel mod = menuItem.getModel();            if ((menuItem.isSelected() && checkIcon == null)                || (mod != null && mod.isArmed())                && (menuItem.getParent() instanceof MenuElement))              g.setColor(selectionForeground);            else              g.setColor(menuItem.getForeground());          }        else          // FIXME: should fix this to use 'disabledForeground', but its          // default value in BasicLookAndFeel is null.          // FIXME: should there be different foreground colours for selected          // or deselected, when disabled?          g.setColor(Color.gray);        int mnemonicIndex = menuItem.getDisplayedMnemonicIndex();        if (mnemonicIndex != -1)          BasicGraphicsUtils.drawStringUnderlineCharAt(g, text, mnemonicIndex,                                                       textRect.x,                                                       textRect.y                                                           + fm.getAscent());        else          BasicGraphicsUtils.drawString(g, text, 0, textRect.x,                                        textRect.y + fm.getAscent());      }  }  /**   * This method uninstalls the components for this {@link JMenuItem}.   *    * @param menuItem   *          The {@link JMenuItem} to uninstall components for.   */  protected void uninstallComponents(JMenuItem menuItem)  {    // FIXME: need to implement  }  /**   * This method uninstalls the defaults and sets any objects created during   * install to null   */  protected void uninstallDefaults()  {    menuItem.setForeground(null);    menuItem.setBackground(null);    menuItem.setBorder(null);    menuItem.setMargin(null);    menuItem.setBackground(null);    menuItem.setBorder(null);    menuItem.setFont(null);    menuItem.setForeground(null);    menuItem.setMargin(null);    acceleratorFont = null;    acceleratorForeground = null;

⌨️ 快捷键说明

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