📄 componentfactory.java
字号:
* @param id prefix of all resource ids * @param alistener action listener to attach, or null * @return a menuitem with all found attributes, possibly none * @see JMenuItem */ public static JMenuItem getMenuItem( FormatResourceBundle bundle, String id, ActionListener alistener ) { String xprefix = id + ".menuitem"; return getMenuItemByPrefix( bundle, xprefix, alistener ); } private static JMenuItem getMenuItemByPrefix( FormatResourceBundle bundle, String prefix, ActionListener alistener ) { JMenuItem xitem = new JMenuItem(); String xresource = null; // locate checkbox state xresource = null; try { xresource = bundle.getString( prefix + ".checkbox" ); } catch ( MissingResourceException xex ) { } if ( xresource != null ) { if ( xresource.equalsIgnoreCase( "true" ) ) { JCheckBoxMenuItem xcbitem = new JCheckBoxMenuItem(); xcbitem.setState( true ); xitem = xcbitem; } if ( xresource.equalsIgnoreCase( "false" ) ) { JCheckBoxMenuItem xcbitem = new JCheckBoxMenuItem(); xcbitem.setState( false ); xitem = xcbitem; } } // locate icon xresource = null; try { xresource = bundle.getString( prefix + ".icon" ); } catch ( MissingResourceException xex ) { } if ( xresource != null ) { try { Image xicon = AWTUtilities.getImageResource( xresource ); xitem.setIcon( new ImageIcon( xicon ) ); } catch ( IOException ex ) { } } // locate text xresource = null; try { xresource = bundle.getString( prefix + ".text" ); } catch ( MissingResourceException xex ) { } if ( xresource != null ) { xitem.setText( xresource ); } // locate accelerator xresource = null; try { xresource = bundle.getString( prefix + ".accel" ); } catch ( MissingResourceException xex ) { } if ( xresource != null ) { KeyEvent xkeyevent = new KeyEvent( xitem, KeyEvent.KEY_PRESSED, 0, 0, KeyEvent.VK_UNDEFINED ); xkeyevent.setKeyCode( xresource.charAt( xresource.length()-1 ) ); int xmodifiers = 0; if ( xresource.indexOf( "Alt" ) >= 0 ) { xmodifiers += InputEvent.ALT_MASK; } if ( xresource.indexOf( "Ctrl" ) >= 0 ) { xmodifiers += InputEvent.CTRL_MASK; } if ( xresource.indexOf( "Meta" ) >= 0 ) { xmodifiers += InputEvent.META_MASK; } if ( xresource.indexOf( "Shift" ) >= 0 ) { xmodifiers += InputEvent.SHIFT_MASK; } xkeyevent.setModifiers( xmodifiers ); xitem.setAccelerator( KeyStroke.getKeyStrokeForEvent( xkeyevent ) ); } // setup the controls xresource = null; try { xresource = bundle.getString( prefix + ".command" ); } catch ( MissingResourceException xex ) { } if ( xresource != null ) { xitem.setActionCommand( xresource ); } if ( alistener != null ) { xitem.addActionListener( alistener ); } return xitem; } /** * Produce a JCheckBox using the given resource bundle, and attaching the given listeners. * <p> * The checkbox is produced with the attributes found in the resource bundle. If no * attributes are found for the checkbox, then the checkbox is returned void of any * useful attributes. * <p> * Attributes are found using the following resource ids: * <pre> * [id].checkbox.icon - image resource name of the checkbox * [id].checkbox.text - text of the checkbox * [id].checkbox.command - command string of the checkbox when activated * [id].checkbox.tip - tip text of the checkbox * [id].checkbox.foreground - foreground color of the checkbox * [id].checkbox.background - background color of the checkbox * [id].checkbox.font - font of the checkbox * </pre> * * @param bundle resource bundle used in obtaining resource strings * @param id prefix of all resource ids * @param selected set selected state of checkbox * @return a checkbox with all found attributes, possibly none * @see JCheckBox */ public static JCheckBox getCheckBox( FormatResourceBundle bundle, String id, boolean selected ) { JCheckBox xbox = new JCheckBox(); String xprefix = id + ".checkbox"; String xresource; // locate default icon xresource = null; try { xresource = bundle.getString( xprefix + ".icon" ); } catch ( MissingResourceException xex ) { } if ( xresource != null ) { try { Image ximage = AWTUtilities.getImageResource( xresource ); xbox.setIcon( new ImageIcon( ximage ) ); } catch ( IOException ex ) { } } // locate text xresource = null; try { xresource = bundle.getString( xprefix + ".text" ); } catch ( MissingResourceException xex ) { } if ( xresource != null ) { xbox.setText( xresource ); } // setBorderPainted( boolean ) // setContentAreaFilled(boolean) // locate disabled icon xresource = null; try { xresource = bundle.getString( xprefix + ".disabledicon" ); } catch ( MissingResourceException xex ) { } if ( xresource != null ) { try { Image ximage = AWTUtilities.getImageResource( xresource ); xbox.setDisabledIcon( new ImageIcon( ximage ) ); } catch ( IOException ex ) { } } // setDisableSelectedIcon( Icon ) // setPressedIcon( Icon ) // setRolloverIcon( Icon ) // setRolloverSelectedIcon( Icon ) // locate selected icon xresource = null; try { xresource = bundle.getString( xprefix + ".selectedicon" ); } catch ( MissingResourceException xex ) { } if ( xresource != null ) { try { Image ximage = AWTUtilities.getImageResource( xresource ); xbox.setSelectedIcon( new ImageIcon( ximage ) ); } catch ( IOException ex ) { } } // locate component controls setComponent( bundle, xprefix, xbox ); xbox.setSelected( selected ); return xbox; } /** * Produce a JPopupMenu with menuitems using the given resource bundle, * and attaching the given listeners. * <p> * The popup menu is produced with the attributes found in the resource bundle. Separators * and MenuItems are added in succession from sequences resource ids. If no * attributes are found for the menu, then the menu is returned void of any * useful attributes. * <p> * Attributes are found using the same resource ids as getMenu(). * However, MenuItem resource ids are a sequence of: * <pre> * [id].menuitem[#].[attribute] * </pre> * By supplying these appropriately, the contents and order of a menuitems can be changed * by only modifying the resource bundle. * <p> * A separator can also be specified for a menuitem by supplying the following id: * [id].menuitem[#].separator * * @param bundle resource bundle used in obtaining resource strings * @param id prefix of all resource ids * @param alistener action listener to attach, or null * @return a popup menu with all found attributes, possibly none * @see #getMenu() */ public static JPopupMenu getPopupMenuAndItems( FormatResourceBundle bundle, String id, ActionListener alistener ) { JPopupMenu xmenu = getPopupMenu( bundle, id ); int xitems = 0; while ( 1 == 1 ) { // generate a prefix for the resource name String xprefix = id + ".menuitem" + xitems; // look for a separator String xresource = null; try { xresource = bundle.getString( xprefix + ".separator" ); } catch ( MissingResourceException xex ) { } if ( xresource != null ) { xmenu.addSeparator(); } // get the menuitem from the resources JMenuItem xitem = getMenuItemByPrefix( bundle, xprefix, alistener ); // and add to menu if valid if ( ( xitem.getText() == null || xitem.getText().length() == 0 ) && xitem.getIcon() == null ) { // no menu item must be the end of the menu break; } else { xmenu.add( xitem ); } // next item please xitems++; } return xmenu; } /** * Produce a JPopupMenu using the given resource bundle. * <p> * The popup menu is produced with the attributes found in the resource bundle. If no * attributes are found for the menu, then the menu is returned void of any * useful attributes. * <p> * Attributes are found using the following resource ids: * <pre> * [id].popupmenu.label - label of the menu * [id].popupmenu.tip - tip text of the popupmenu * [id].popupmenu.foreground - foreground color of the popupmenu * [id].popupmenu.background - background color of the popupmenu * [id].popupmenu.font - font of the popupmenu * </pre> * * @param bundle resource bundle used in obtaining resource strings * @param id prefix of all resource ids * @return a popup menu with all found attributes, possibly none * @see JPopupMenu */ public static JPopupMenu getPopupMenu( FormatResourceBundle bundle, String id ) { JPopupMenu xmenu = new JPopupMenu(); String xprefix = id + ".popupmenu"; String xresource; // locate label xresource = null; try { xresource = bundle.getString( xprefix + ".label" ); } catch ( MissingResourceException xex ) { } if ( xresource != null ) { xmenu.setLabel( xresource ); } // locate component controls setComponent( bundle, xprefix, xmenu ); return xmenu; } /** * Set component level controls from the resource bundle * * @param bundle resource bundle used in obtaining resource strings * @param id prefix of all resource ids * @param component the component to modify */ private static void setComponent( FormatResourceBundle bundle, String id, JComponent component ) { // locate tool tip text String xresource = null; try { xresource = bundle.getString( id + ".tip" ); } catch ( MissingResourceException xex ) { } if ( xresource != null ) { component.setToolTipText( xresource ); } // locate foreground color try { xresource = bundle.getString( id + ".foreground" ); } catch ( MissingResourceException xex ) { } if ( xresource != null ) { Color xcolor = Color.getColor( xresource ); if ( xcolor != null ) { component.setForeground( xcolor ); } } // locate background color try { xresource = bundle.getString( id + ".background" ); } catch ( MissingResourceException xex ) { } if ( xresource != null ) { Color xcolor = Color.getColor( xresource ); if ( xcolor != null ) { component.setForeground( xcolor ); } } // locate font try { xresource = bundle.getString( id + ".font" ); } catch ( MissingResourceException xex ) { } if ( xresource != null ) { Font xfont = Font.getFont( xresource ); if ( xfont != null ) { component.setFont( xfont ); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -