當前位置: 首頁>>代碼示例>>Java>>正文


Java JMenuItem.getPreferredSize方法代碼示例

本文整理匯總了Java中javax.swing.JMenuItem.getPreferredSize方法的典型用法代碼示例。如果您正苦於以下問題:Java JMenuItem.getPreferredSize方法的具體用法?Java JMenuItem.getPreferredSize怎麽用?Java JMenuItem.getPreferredSize使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在javax.swing.JMenuItem的用法示例。


在下文中一共展示了JMenuItem.getPreferredSize方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: testPresenters

import javax.swing.JMenuItem; //導入方法依賴的package包/類
@Test
public void testPresenters() {
    MyAction a = new MyAction(PRESENTER_ATTRS);
    JMenuItem menuPresenter = a.getMenuPresenter();
    menuPresenter.getPreferredSize(); // Ensure pending lazy operation gets completed
    assertEquals("Menu Text", menuPresenter.getText());
    String newMenuText = "Menu Text 2";
    a.putValue(AbstractEditorAction.MENU_TEXT_KEY, newMenuText);
    menuPresenter.getPreferredSize(); // Ensure pending lazy operation gets completed
    assertEquals(newMenuText, menuPresenter.getText());

    // Popup presenter
    JMenuItem popupPresenter = a.getPopupPresenter();
    popupPresenter.getPreferredSize(); // Ensure pending lazy operation gets completed
    assertEquals(newMenuText, popupPresenter.getText());
    String popupText = "Popup Text";
    a.putValue(AbstractEditorAction.POPUP_TEXT_KEY, popupText);
    popupPresenter.getPreferredSize(); // Ensure pending lazy operation gets completed
    assertEquals(popupText, popupPresenter.getText());
    
    // Check menu text translation
    a.putValue(AbstractEditorAction.MENU_TEXT_KEY, "A&BC");
    menuPresenter.getPreferredSize(); // Ensure pending lazy operation gets completed
    assertEquals("ABC", menuPresenter.getText());
    assertEquals('B', menuPresenter.getMnemonic());
    
    KeyStroke accel = KeyStroke.getKeyStroke('a');
    a.putValue(Action.ACCELERATOR_KEY, accel);
    menuPresenter.getPreferredSize(); // Ensure pending lazy operation gets completed
    assertEquals(accel, menuPresenter.getAccelerator());
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:32,代碼來源:AbstractEditorActionTest.java

示例2: main

import javax.swing.JMenuItem; //導入方法依賴的package包/類
public static void main(String[] args) {
    JMenu subMenu1 = new JMenu("Long-titled Sub Menu");
    subMenu1.add(new JMenuItem("SubMenu Item"));
    JMenuItem checkBoxMenuItem1 = new JCheckBoxMenuItem("CheckBox");

    JMenu menu1 = new JMenu("It works always");
    menu1.add(checkBoxMenuItem1);
    menu1.add(subMenu1);

    // Simulate DefaultMenuLayout calls.
    // The latest traversed menu item must be the widest.
    checkBoxMenuItem1.getPreferredSize();
    int width1 = subMenu1.getPreferredSize().width;
    System.out.println("width1 = " + width1);


    JMenu subMenu2 = new JMenu("Long-titled Sub Menu");
    subMenu2.add(new JMenuItem("SubMenu Item"));
    JMenuItem checkBoxMenuItem2 = new JCheckBoxMenuItem("CheckBox");

    JMenu menu2 = new JMenu("It did not work before the fix");
    menu2.add(subMenu2);
    menu2.add(checkBoxMenuItem2);

    // Simulate DefaultMenuLayout calls.
    // The latest traversed menu item must be the widest.
    subMenu2.getPreferredSize();
    int width2 = checkBoxMenuItem2.getPreferredSize().width;
    System.out.println("width2 = " + width2);

    if (width1 != width2) {
        throw new RuntimeException( "Submenu title and submenu indicator " +
                                    "overlap on JMenuItem!" );
    }

    System.out.println("Test passed");
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:38,代碼來源:bug6438430.java


注:本文中的javax.swing.JMenuItem.getPreferredSize方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。