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

📄 menubar.java

📁 基于Junit的 功能和单元测试的的测试工具。只支持Swing.
💻 JAVA
字号:
package org.uispec4j;

import junit.framework.Assert;
import org.uispec4j.assertion.Assertion;
import org.uispec4j.utils.ArrayUtils;

import javax.swing.*;
import java.awt.*;

/**
 * Wrapper for JMenuBar components.<p/>
 * A MenuBar is a container for top-level menu items represented by {@link MenuItem} components.
 */
public class MenuBar extends AbstractUIComponent {
  public static final String TYPE_NAME = "menuBar";
  public static final Class[] SWING_CLASSES = {JMenuBar.class};

  private JMenuBar jMenuBar;

  public MenuBar(JMenuBar menuBar) {
    Assert.assertNotNull(menuBar);
    this.jMenuBar = menuBar;
  }

  public Component getAwtComponent() {
    return jMenuBar;
  }

  public String getDescriptionTypeName() {
    return TYPE_NAME;
  }

  /** Returns a {@link MenuItem} component representing a top-level menu (for instance File/Edit/etc.).
   * That MenuItem can be used then to access the individual menu commands, or other submenus.
   */
  public MenuItem getMenu(String menuName) {
    int menuIndex = getMenuIndex(menuName);
    Assert.assertFalse("Menu '" + menuName + "' does not exist", menuIndex == -1);
    JMenu menu = jMenuBar.getMenu(menuIndex);
    return new MenuItem(menu);
  }

  /** Checks the names displayed in the menu, ommiting separators. */
  public Assertion contentEquals(final String[] menuNames) {
    return new Assertion() {
      public void check() {
        String[] actual = new String[jMenuBar.getMenuCount()];
        for (int i = 0; i < actual.length; i++) {
          actual[i] = jMenuBar.getMenu(i).getText();
        }
        ArrayUtils.assertEquals(menuNames, actual);
      }
    };
  }

  /** Returns the index of a menu item given its name, or -1 if it was not found. */
  private int getMenuIndex(String menuName) {
    for (int i = 0; i < jMenuBar.getMenuCount(); i++) {
      JMenu menu = jMenuBar.getMenu(i);
      if (menu == null) {
        return -1;
      }
      String text = menu.getText();
      if (text == null) {
        continue;
      }
      if (text.equals(menuName)) {
        return i;
      }
    }
    return -1;
  }
}

⌨️ 快捷键说明

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