本文整理汇总了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());
}
示例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");
}