下面列出了怎么用javax.swing.UIDefaults的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
protected void installKeyboardActions() {
super.installKeyboardActions();
JTextComponent comp = getComponent();
UIDefaults uidefaults = XToolkit.getUIDefaults();
String prefix = getPropertyPrefix();
InputMap map = (InputMap)uidefaults.get(prefix + ".focusInputMap");
if (map != null) {
SwingUtilities.replaceUIInputMap(comp, JComponent.WHEN_FOCUSED,
map);
}
}
/**
* Creates the value retrieved from the <code>UIDefaults</code> table.
* The object is created each time it is accessed.
*
* @param table a <code>UIDefaults</code> table
* @return the created <code>Object</code>
*/
@Override
public Object createValue(UIDefaults table) {
Object obj = null;
// check specified state
if (state!=null){
obj = uiDefaults.get(prefix+"["+state+"]."+suffix);
}
// check enabled state
if (obj==null){
obj = uiDefaults.get(prefix+"[Enabled]."+suffix);
}
// check for defaults
if (obj==null){
if (isFont) {
obj = uiDefaults.get("defaultFont");
} else {
obj = uiDefaults.get(suffix);
}
}
return obj;
}
/**
* Creates the value retrieved from the <code>UIDefaults</code> table.
* The object is created each time it is accessed.
*
* @param table a <code>UIDefaults</code> table
* @return the created <code>Object</code>
*/
@Override
public Object createValue(UIDefaults table) {
Object obj = null;
// check specified state
if (state!=null){
obj = uiDefaults.get(prefix+"["+state+"]."+suffix);
}
// check enabled state
if (obj==null){
obj = uiDefaults.get(prefix+"[Enabled]."+suffix);
}
// check for defaults
if (obj==null){
if (isFont) {
obj = uiDefaults.get("defaultFont");
} else {
obj = uiDefaults.get(suffix);
}
}
return obj;
}
@Override
protected void installKeyboardActions() {
super.installKeyboardActions();
JTextComponent comp = getComponent();
UIDefaults uidefaults = XToolkit.getUIDefaults();
String prefix = getPropertyPrefix();
InputMap map = (InputMap)uidefaults.get(prefix + ".focusInputMap");
if (map != null) {
SwingUtilities.replaceUIInputMap(comp, JComponent.WHEN_FOCUSED,
map);
}
}
@Override
protected void installKeyboardActions() {
super.installKeyboardActions();
JTextComponent comp = getComponent();
UIDefaults uidefaults = XToolkit.getUIDefaults();
String prefix = getPropertyPrefix();
InputMap map = (InputMap)uidefaults.get(prefix + ".focusInputMap");
if (map != null) {
SwingUtilities.replaceUIInputMap(comp, JComponent.WHEN_FOCUSED,
map);
}
}
/**
* http://www.jetbrains.org/intellij/sdk/docs/reference_guide/ui_themes/themes_customize.html#defining-named-colors
*/
private void loadNamedColors( UIDefaults defaults ) {
if( colors == null )
return;
namedColors = new HashMap<>();
for( Map.Entry<String, String> e : colors.entrySet() ) {
String value = e.getValue();
ColorUIResource color = UIDefaultsLoader.parseColor( value );
if( color != null ) {
String key = e.getKey();
namedColors.put( key, color );
defaults.put( "ColorPalette." + key, color );
}
}
}
private void initBorderDefaults(UIDefaults table) {
Object[] borderDefaults = { "TextField.border", Borders.getTextFieldBorder(), "PasswordField.border",
Borders.getTextFieldBorder(), "FilterTextField.border", Borders.getTextFieldBorder(),
"FormattedTextField.border", Borders.getTextFieldBorder(), "TextArea.border", Borders.getTextFieldBorder(),
"SplitPane.border", Borders.getSplitPaneBorder(), "ScrollPane.border", Borders.getScrollPaneBorder(),
"InternalFrame.border", Borders.getInternalFrameBorder(), "Table.scrollPaneBorder", null, // removed
// table
// border,
// original:
// Borders.getSplitPaneBorder()
"Table.tabbedPaneBorder", null, // remove double borders
"ToolBar.border", Borders.getToolBarBorder(), "Spinner.border", Borders.getSpinnerBorder(),
"ComboBox.border", Borders.getComboBoxBorder(), "Button.border", Borders.getEmptyButtonBorder(),
"ToggleButton.border", Borders.getEmptyButtonBorder(), "ProgressBar.border", Borders.getProgressBarBorder(),
"PopupMenu.border", Borders.getPopupMenuBorder(), "CheckBox.border", Borders.getCheckBoxBorder(),
"RadioButton.border", Borders.getCheckBoxBorder(), "ToolTip.border", Borders.getToolTipBorder(),
"MenuBar.border", Borders.getMenuBarBorder(), "CheckBox.icon", IconFactory.getCheckBoxIcon(),
"RadioButton.icon", IconFactory.getRadioButtonIcon(), "ComboBox.focusCellHighlightBorder",
Borders.getComboBoxListCellRendererFocusBorder(), };
table.putDefaults(borderDefaults);
}
public static void main(final String[] args)
throws InvocationTargetException, InterruptedException {
SwingUtilities.invokeAndWait(() -> {
UIDefaults ui = UIManager.getDefaults();
ui.put("control", new ColorUIResource(54, 54, 54));
final JDialog dialog = new JDialog();
final JFrame frame = new JFrame();
frame.pack();
dialog.pack();
final Color dialogBackground = dialog.getBackground();
final Color frameBackground = frame.getBackground();
frame.dispose();
dialog.dispose();
if (!dialogBackground.equals(frameBackground)) {
System.err.println("Expected:" + frameBackground);
System.err.println("Actual:" + dialogBackground);
throw new RuntimeException("Wrong background color");
}
});
}
/**
* Creates the value retrieved from the <code>UIDefaults</code> table.
* The object is created each time it is accessed.
*
* @param table a <code>UIDefaults</code> table
* @return the created <code>Object</code>
*/
@Override
public Object createValue(UIDefaults table) {
Object obj = null;
// check specified state
if (state!=null){
obj = uiDefaults.get(prefix+"["+state+"]."+suffix);
}
// check enabled state
if (obj==null){
obj = uiDefaults.get(prefix+"[Enabled]."+suffix);
}
// check for defaults
if (obj==null){
if (isFont) {
obj = uiDefaults.get("defaultFont");
} else {
obj = uiDefaults.get(suffix);
}
}
return obj;
}
@Override
public void addCustomEntriesToTable(UIDefaults table) {
Border blackLineBorder =
new BorderUIResource(new LineBorder(getBlack()));
Border whiteLineBorder =
new BorderUIResource(new LineBorder(getWhite()));
Object textBorder = new BorderUIResource(new CompoundBorder(
blackLineBorder,
new BasicBorders.MarginBorder()));
table.put("ToolTip.border", blackLineBorder);
table.put("TitledBorder.border", blackLineBorder);
table.put("Table.focusCellHighlightBorder", whiteLineBorder);
table.put("Table.focusCellForeground", getWhite());
table.put("TextField.border", textBorder);
table.put("PasswordField.border", textBorder);
table.put("TextArea.border", textBorder);
table.put("TextPane.font", textBorder);
}
/**
* Creates the value retrieved from the <code>UIDefaults</code> table.
* The object is created each time it is accessed.
*
* @param table a <code>UIDefaults</code> table
* @return the created <code>Object</code>
*/
@Override
public Object createValue(UIDefaults table) {
Object obj = null;
// check specified state
if (state!=null){
obj = uiDefaults.get(prefix+"["+state+"]."+suffix);
}
// check enabled state
if (obj==null){
obj = uiDefaults.get(prefix+"[Enabled]."+suffix);
}
// check for defaults
if (obj==null){
if (isFont) {
obj = uiDefaults.get("defaultFont");
} else {
obj = uiDefaults.get(suffix);
}
}
return obj;
}
/**
* Use Aqua settings for some properties if we're on a Mac.
*
* @param d the UI defaults map.
*/
private void defineAquaSettings(UIDefaults d) {
try {
// Instantiate Aqua but don't install it.
Class<?> lnfClass = Class.forName(UIManager.getSystemLookAndFeelClassName(), true,
Thread.currentThread().getContextClassLoader());
LookAndFeel aqua = (LookAndFeel) lnfClass.newInstance();
UIDefaults aquaDefaults = aqua.getDefaults();
// Use Aqua for any menu UI classes.
d.put("MenuBarUI", aquaDefaults.get("MenuBarUI"));
d.put("MenuUI", aquaDefaults.get("MenuUI"));
} catch (Exception e) {
// TODO Should we do something with this exception?
e.printStackTrace();
}
}
/**
* Creates the value retrieved from the <code>UIDefaults</code> table.
* The object is created each time it is accessed.
*
* @param table a <code>UIDefaults</code> table
* @return the created <code>Object</code>
*/
@Override
public Object createValue(UIDefaults table) {
Object obj = null;
// check specified state
if (state!=null){
obj = uiDefaults.get(prefix+"["+state+"]."+suffix);
}
// check enabled state
if (obj==null){
obj = uiDefaults.get(prefix+"[Enabled]."+suffix);
}
// check for defaults
if (obj==null){
if (isFont) {
obj = uiDefaults.get("defaultFont");
} else {
obj = uiDefaults.get(suffix);
}
}
return obj;
}
public static void main(final String[] args)
throws InvocationTargetException, InterruptedException {
SwingUtilities.invokeAndWait(() -> {
UIDefaults ui = UIManager.getDefaults();
ui.put("control", new ColorUIResource(54, 54, 54));
final JDialog dialog = new JDialog();
final JFrame frame = new JFrame();
frame.pack();
dialog.pack();
final Color dialogBackground = dialog.getBackground();
final Color frameBackground = frame.getBackground();
frame.dispose();
dialog.dispose();
if (!dialogBackground.equals(frameBackground)) {
System.err.println("Expected:" + frameBackground);
System.err.println("Actual:" + dialogBackground);
throw new RuntimeException("Wrong background color");
}
});
}
@Override
protected void installKeyboardActions() {
super.installKeyboardActions();
JTextComponent comp = getComponent();
UIDefaults uidefaults = XToolkit.getUIDefaults();
String prefix = getPropertyPrefix();
InputMap map = (InputMap)uidefaults.get(prefix + ".focusInputMap");
if (map != null) {
SwingUtilities.replaceUIInputMap(comp, JComponent.WHEN_FOCUSED,
map);
}
}
@Override
public void addCustomEntriesToTable(UIDefaults table) {
Border blackLineBorder =
new BorderUIResource(new LineBorder(getBlack()));
Border whiteLineBorder =
new BorderUIResource(new LineBorder(getWhite()));
Object textBorder = new BorderUIResource(new CompoundBorder(
blackLineBorder,
new BasicBorders.MarginBorder()));
table.put("ToolTip.border", blackLineBorder);
table.put("TitledBorder.border", blackLineBorder);
table.put("Table.focusCellHighlightBorder", whiteLineBorder);
table.put("Table.focusCellForeground", getWhite());
table.put("TextField.border", textBorder);
table.put("PasswordField.border", textBorder);
table.put("TextArea.border", textBorder);
table.put("TextPane.font", textBorder);
}
@Override
public void addCustomEntriesToTable(UIDefaults table) {
Border blackLineBorder =
new BorderUIResource(new LineBorder(getBlack()));
Border whiteLineBorder =
new BorderUIResource(new LineBorder(getWhite()));
Object textBorder = new BorderUIResource(new CompoundBorder(
blackLineBorder,
new BasicBorders.MarginBorder()));
table.put("ToolTip.border", blackLineBorder);
table.put("TitledBorder.border", blackLineBorder);
table.put("Table.focusCellHighlightBorder", whiteLineBorder);
table.put("Table.focusCellForeground", getWhite());
table.put("TextField.border", textBorder);
table.put("PasswordField.border", textBorder);
table.put("TextArea.border", textBorder);
table.put("TextPane.font", textBorder);
}
@Override
public void addCustomEntriesToTable(UIDefaults table) {
super.addCustomEntriesToTable(table);
final int internalFrameIconSize = 22;
table.put("InternalFrame.closeIcon", MetalIconFactory.
getInternalFrameCloseIcon(internalFrameIconSize));
table.put("InternalFrame.maximizeIcon", MetalIconFactory.
getInternalFrameMaximizeIcon(internalFrameIconSize));
table.put("InternalFrame.iconifyIcon", MetalIconFactory.
getInternalFrameMinimizeIcon(internalFrameIconSize));
table.put("InternalFrame.minimizeIcon", MetalIconFactory.
getInternalFrameAltMaximizeIcon(internalFrameIconSize));
table.put("ScrollBar.width", 21);
}
@Override
protected void installKeyboardActions() {
super.installKeyboardActions();
JTextComponent comp = getComponent();
UIDefaults uidefaults = XToolkit.getUIDefaults();
String prefix = getPropertyPrefix();
InputMap map = (InputMap)uidefaults.get(prefix + ".focusInputMap");
if (map != null) {
SwingUtilities.replaceUIInputMap(comp, JComponent.WHEN_FOCUSED,
map);
}
}
@Override
protected void installKeyboardActions() {
super.installKeyboardActions();
JTextComponent comp = getComponent();
UIDefaults uidefaults = XToolkit.getUIDefaults();
String prefix = getPropertyPrefix();
InputMap map = (InputMap)uidefaults.get(prefix + ".focusInputMap");
if (map != null) {
SwingUtilities.replaceUIInputMap(comp, JComponent.WHEN_FOCUSED,
map);
}
}
/**
* Initializes the module. Use this method to perform all initial setup operations. This method is called only once in
* a modules lifetime. If the initializing cannot be completed, throw a ModuleInitializeException to indicate the
* error,. The module will not be available to the system.
*
* @param subSystem
* the subSystem.
* @throws ModuleInitializeException
* if an error ocurred while initializing the module.
*/
public void initialize( final SubSystem subSystem ) throws ModuleInitializeException {
if ( subSystem.getExtendedConfig().getBoolProperty(
"org.pentaho.reporting.engine.classic.core.modules.gui.base.SwingDialogTranslation" ) ) { //$NON-NLS-1$
final ResourceBundle resources = ResourceBundle.getBundle( SwingCommonModule.BUNDLE_NAME );
final UIDefaults defaults = UIManager.getDefaults();
final Enumeration en = resources.getKeys();
while ( en.hasMoreElements() ) {
try {
final String keyName = (String) en.nextElement();
defaults.put( keyName, resources.getObject( keyName ) );
} catch ( Exception e ) {
// Ignored; if it happens, we would not care that much ..
}
}
}
}
public static void main(String[] args) {
UIDefaults table= new MetalLookAndFeel().getDefaults();
test(table.get("ToolBar.rolloverBorder"),
"javax.swing.plaf.metal.MetalBorders$ButtonBorder",
"javax.swing.plaf.metal.MetalBorders$RolloverMarginBorder");
test(table.get("ToolBar.nonrolloverBorder"),
"javax.swing.plaf.metal.MetalBorders$ButtonBorder",
"javax.swing.plaf.metal.MetalBorders$RolloverMarginBorder");
test(table.get("RootPane.frameBorder"),
"javax.swing.plaf.metal.MetalBorders$FrameBorder");
test(table.get("RootPane.plainDialogBorder"),
"javax.swing.plaf.metal.MetalBorders$DialogBorder");
test(table.get("RootPane.informationDialogBorder"),
"javax.swing.plaf.metal.MetalBorders$DialogBorder");
test(table.get("RootPane.errorDialogBorder"),
"javax.swing.plaf.metal.MetalBorders$ErrorDialogBorder");
test(table.get("RootPane.colorChooserDialogBorder"),
"javax.swing.plaf.metal.MetalBorders$QuestionDialogBorder");
test(table.get("RootPane.fileChooserDialogBorder"),
"javax.swing.plaf.metal.MetalBorders$QuestionDialogBorder");
test(table.get("RootPane.questionDialogBorder"),
"javax.swing.plaf.metal.MetalBorders$QuestionDialogBorder");
test(table.get("RootPane.warningDialogBorder"),
"javax.swing.plaf.metal.MetalBorders$WarningDialogBorder");
}
/**
* @inheritDoc
*/
@Override
public Object createValue(UIDefaults defaults) {
Font f = defaults.getFont(parentKey);
if (f != null) {
// always round size for now so we have exact int font size
// (or we may have lame looking fonts)
float size = Math.round(f.getSize2D() * sizeOffset);
int style = f.getStyle();
if (bold != null) {
if (bold.booleanValue()) {
style = style | Font.BOLD;
} else {
style = style & ~Font.BOLD;
}
}
if (italic != null) {
if (italic.booleanValue()) {
style = style | Font.ITALIC;
} else {
style = style & ~Font.ITALIC;
}
}
return f.deriveFont(style, size);
} else {
return null;
}
}
private Painter getPainter(Map<String, Object> defaults, String key) {
Object p = defaults.get(key);
if (p instanceof UIDefaults.LazyValue) {
p = ((UIDefaults.LazyValue)p).createValue(UIManager.getDefaults());
}
return (p instanceof Painter ? (Painter)p : null);
}
/**
* Set the default font in all UI elements.
*
* @param defaultFont A {@code Font} to use by default.
*/
public static void installFont(Font defaultFont) {
UIDefaults u = UIManager.getDefaults();
java.util.Enumeration<Object> keys = u.keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
if (u.get(key) instanceof javax.swing.plaf.FontUIResource) {
u.put(key, defaultFont);
}
}
}
@Override
protected void installOther(UIDefaults table)
{
// 注: 下列三个属性决定JMenu在弹出菜单中的布局
table.put(AFTERCHECKICONGAP, 4);
table.put(MINIMUMTEXTOFFSET, 0);
table.put(CHECKICONOFFSET, 4);
}
@Override
protected void loadImages(UIDefaults table)
{
table.put(ARROW_NORMAL_IMG, LuckRes.getImage("menu/arrow_normal.png"));
table.put(ARROW_ROLLVER_IMG, LuckRes.getImage("menu/arrow_rollver.png"));
table.put(ARROW_ICON, getIconRes(new LuckArrowIcon()));
table.put(CHECK_ICON, getIconRes(new LuckIcon(16, 0)));
}
@Override
protected void installBorder(UIDefaults table)
{
table.put(MENUBAR_BORDER, getBorderRes(BorderFactory.createEmptyBorder(3, 0, 3, 0)));
table.put(MENU_BORDER, getBorderRes(BorderFactory.createEmptyBorder()));
}
@Override
public Object createValue( UIDefaults table ) {
InputMap inputMap = new InputMapUIResource();
for( Object[] bindings : bindingsArray )
LookAndFeel.loadKeyBindings( inputMap, bindings );
return inputMap;
}
private void endInputMap() throws SAXException {
if (_inputMapID != null) {
register(_inputMapID, new UIDefaults.LazyInputMap(
_inputMapBindings.toArray(new Object[_inputMapBindings.
size()])));
}
_inputMapBindings.clear();
_inputMapID = null;
}