public abstract class LookAndFeel extends Object
LookAndFeel ,顾名思义,封装外观。
除了安装外观,大多数开发人员不需要直接与LookAndFeel进行交互。
一般来说,创建自定义外观的开发人员只需要关注这个类。
Swing建立在每个JComponent子类的基础上,具有特定的ComponentUI子类的实现。 ComponentUI通常被称为“ui”,“ui”或“look-feel delegate”。 ComponentUI子类负责提供组件的外观和特定功能。 例如, JTree要求执行ComponentUI子类TreeUI 。 具体ComponentUI子类的实现由LookAndFeel提供。 每个JComponent子类通过JComponent方法getUIClassID ComponentUI子类。
每个LookAndFeel实现必须通过为从getDefaults返回的UIDefaults对象中的每个Swing的ui类ID指定一个值来提供适当的ComponentUI子类的getDefaults 。 例如, BasicLookAndFeel使用BasicTreeUI作为具体实施TreeUI 。 这是通过BasicLookAndFeel完成的,提供了键值对"TreeUI"-"javax.swing.plaf.basic.BasicTreeUI" ,在UIDefaults从getDefaults返回。 有关如何获得ComponentUI子类的实现的详细信息,请参阅UIDefaults.getUI(JComponent) 。
当安装LookAndFeel时, UIManager不会检查所有ui类ID是否存在条目。 因此,如果当前的外观和感觉没有为特定的ui类id提供值,并且创建JComponent子类的实例,则会发生随机异常。
UIManager所述,每个LookAndFeel都有机会提供一组与开发人员和系统默认值分层的默认值。
一些Swing的组件需要外观和感觉来提供一组特定的默认值。
这些记录在需要特定默认值的类中。
ComponentUIs通常需要在设置各种属性JComponent的ComponentUI为其提供外观和感觉。
当ComponentUI在JComponent上时, ComponentUI会执行此JComponent 。
只有开发人员没有设置属性,才能设置属性。
对于非原始值,建议的ComponentUI仅改变在属性JComponent如果当前值是null或实现UIResource 。
如果当前值为null或实现UIResource则表明该属性尚未由开发人员设置,并且您可以自由更改该属性。
例如, BasicButtonUI.installDefaults仅改变对字体JButton如果从返回值button.getFont()是null或实现UIResource 。
另一方面,如果button.getFont()返回一个没有实现UIResource的non-null值,那么BasicButtonUI.installDefaults将不会更改JButton的字体。
对于原始值,如opaque ,应调用方法installProperty 。 installProperty只有在开发人员尚未更改该值时才更改相应的属性。
ComponentUI实现应该使用此类提供的各种安装方法,因为它们处理必要的检查并使用推荐的指导原则安装该属性。
null或UIResource提供的所有安装方法LookAndFeel需要访问默认值。
例如,安装该字体将执行以下操作:
JComponent c;
Font font = c.getFont();
if (font == null || (font instanceof UIResource)) {
c.setFont(UIManager.getFont("fontKey"));
}
如果字体是null或UIResource ,则使用键fontKey查询默认表。
所有UIDefault's获取方法抛出一个NullPointerException如果通过在null 。
因此,除非另有说明各个不同的安装方法LookAndFeel抛出NullPointerException如果当前值是null或UIResource和提供的默认密钥是null 。
此外,除非另有说明,所有的install方法抛出一个NullPointerException如果null组件中通过。
| Constructor and Description |
|---|
LookAndFeel() |
| Modifier and Type | Method and Description |
|---|---|
UIDefaults |
getDefaults()
返回外观和默认值。
|
abstract String |
getDescription()
返回这个外观的一行描述实现,例如
|
static Object |
getDesktopPropertyValue(String systemPropertyName, Object fallbackValue)
通过调用
Toolkit.getDefaultToolkit().getDesktopProperty()返回指定的系统桌面属性的
Toolkit.getDefaultToolkit().getDesktopProperty() 。
|
Icon |
getDisabledIcon(JComponent component, Icon icon)
返回一个
Icon具有禁用外观。
|
Icon |
getDisabledSelectedIcon(JComponent component, Icon icon)
返回一个
Icon供被还被选中的禁用组件使用。
|
abstract String |
getID()
返回一个识别这种外观和感觉的字符串。
|
LayoutStyle |
getLayoutStyle()
返回
LayoutStyle为这种外观和感觉。
|
abstract String |
getName()
返回一个标识这种外观的短字符串,例如
|
boolean |
getSupportsWindowDecorations()
返回
true如果
LookAndFeel返回
RootPaneUI实例支持提供
Window在装饰
JRootPane 。
|
void |
initialize()
初始化外观和感觉。
|
static void |
installBorder(JComponent c, String defaultBorderName)
使用默认值设置组件边框属性的便利方法。
|
static void |
installColors(JComponent c, String defaultBgName, String defaultFgName)
使用默认值设置组件的前景和背景颜色属性的便利方法。
|
static void |
installColorsAndFont(JComponent c, String defaultBgName, String defaultFgName, String defaultFontName)
使用默认值设置组件的前景,背景和字体属性的便利方法。
|
static void |
installProperty(JComponent c, String propertyName, Object propertyValue)
如果开发人员尚未设置该属性,则在方法上安装具有指定名称和值的属性。
|
abstract boolean |
isNativeLookAndFeel()
如果底层平台具有“本机”的外观,而这是一个实现方法,请返回
true 。
|
abstract boolean |
isSupportedLookAndFeel()
如果底层平台支持和/或允许这种外观,返回
true 。
|
static void |
loadKeyBindings(InputMap retMap, Object[] keys)
填充一个
InputMap与指定的绑定。
|
static ComponentInputMap |
makeComponentInputMap(JComponent c, Object[] keys)
创建一个
ComponentInputMapUIResource从
keys 。
|
static Object |
makeIcon(类<?> baseClass, String gifFile)
创建并返回一个加载图像的
UIDefault.LazyValue 。
|
static InputMap |
makeInputMap(Object[] keys)
创建一个
InputMapUIResource从
keys 。
|
static JTextComponent.KeyBinding[] |
makeKeyBindings(Object[] keyBindingList)
方便构建
KeyBindings数组的
KeyBindings 。
|
void |
provideErrorFeedback(Component component)
当用户尝试无效操作时调用,例如粘贴到具有焦点的不可
JTextField 。
|
String |
toString()
返回显示和标识此对象属性的字符串。
|
void |
uninitialize()
初始化外观和感觉。
|
static void |
uninstallBorder(JComponent c)
方便卸载边框的方法。
|
public static void installColors(JComponent c, String defaultBgName, String defaultFgName)
null或UIResource时才会设置UIResource 。
c - 设置颜色的组件
defaultBgName - 背景的钥匙
defaultFgName -
defaultFgName键
NullPointerException - 如exceptions
所述
installColorsAndFont(javax.swing.JComponent, java.lang.String, java.lang.String, java.lang.String) ,
UIManager.getColor(java.lang.Object)
public static void installColorsAndFont(JComponent c, String defaultBgName, String defaultFgName, String defaultFontName)
null或UIResource时才设置UIResource 。
c - 组件设置为颜色和字体
defaultBgName - 背景的关键
defaultFgName -
defaultFgName键
defaultFontName - 字体的键
NullPointerException - 如exceptions
所述
installColors(javax.swing.JComponent, java.lang.String, java.lang.String) ,
UIManager.getColor(java.lang.Object) ,
UIManager.getFont(java.lang.Object)
public static void installBorder(JComponent c, String defaultBorderName)
null或实例UIResource 。
c - 设置边框的组件
defaultBorderName - 指定边框的键
NullPointerException - 如exceptions
所述
public static void uninstallBorder(JComponent c)
UIResource ,则设置为null 。
c - 卸载边框的组件
NullPointerException - 如果
c是
null
public static void installProperty(JComponent c, String propertyName, Object propertyValue)
UIResource标记包装,因此此方法使用私有状态来确定该属性是否已由客户端设置。
c - 设置属性的目标组件
propertyName - 要设置的属性的名称
propertyValue - 物业的价值
IllegalArgumentException - 如果指定的属性不是可以使用此方法设置的属性
ClassCastException - 如果属性值尚未由开发人员设置,并且类型与属性的类型不匹配
NullPointerException - 如果
c是
null ,或者指定的属性尚未由开发者设置,并且
propertyValue是
null
public static JTextComponent.KeyBinding[] makeKeyBindings(Object[] keyBindingList)
KeyBindings数组的KeyBindings 。
虽然此方法不被弃用,但开发人员应改为使用ActionMap和InputMap提供密钥绑定。
该方法返回一个KeyBindings的数组,每个交替的key-action对在keyBindingList 。 甲key可以是一个String在由指定的格式KeyStroke.getKeyStroke法,或KeyStroke 。 所述action的双部分是String对应于名称Action 。
以下示例说明从六个交替的key-action对创建KeyBinding阵列:
JTextComponent.KeyBinding[] multilineBindings = makeKeyBindings( new Object[] {
"UP", DefaultEditorKit.upAction,
"DOWN", DefaultEditorKit.downAction,
"PAGE_UP", DefaultEditorKit.pageUpAction,
"PAGE_DOWN", DefaultEditorKit.pageDownAction,
"ENTER", DefaultEditorKit.insertBreakAction,
"TAB", DefaultEditorKit.insertTabAction
});
如果keyBindingList's长度为奇数,则忽略最后一个元素。
供给null为任一值key或action的一部分key-action在创建一对结果KeyBinding与相应的值null 。 由于Swing的其他部分预计在non-null中有KeyBinding ,因此您应避免提供null作为key-action对的key或action 。
keyBindingList - 一组
key-action对
KeyBindings的数组
NullPointerException - 如果
keyBindingList是
null
ClassCastException - 如果
key部分对不是
KeyStroke或
String ,或
action部分对不是
String
ActionMap , InputMap , KeyStroke.getKeyStroke(char)
public static InputMap makeInputMap(Object[] keys)
InputMapUIResource从keys 。
这是创建一个新的InputMapUIResource ,调用loadKeyBindings(map, keys)并返回InputMapUIResource的InputMapUIResource 。
keys - 交替的
keystroke-action key对,如
loadKeyBindings(javax.swing.InputMap, java.lang.Object[])
所述
InputMapUIResource
loadKeyBindings(javax.swing.InputMap, java.lang.Object[])
public static ComponentInputMap makeComponentInputMap(JComponent c, Object[] keys)
ComponentInputMapUIResource从keys 。
这是创建一个新的ComponentInputMapUIResource ,调用loadKeyBindings(map, keys)并返回ComponentInputMapUIResource的ComponentInputMapUIResource 。
c - 创建
ComponentInputMapUIResource
keys - 交替的
keystroke-action key对,如
loadKeyBindings(javax.swing.InputMap, java.lang.Object[])
所述
InputMapUIResource
IllegalArgumentException - 如果
c是
null
loadKeyBindings(javax.swing.InputMap, java.lang.Object[]) , ComponentInputMapUIResource
public static void loadKeyBindings(InputMap retMap, Object[] keys)
InputMap与指定的绑定。
绑定作为交替keystroke-action key对的列表提供。
所述keystroke是任一的一个实例KeyStroke ,或String标识KeyStroke的结合。
具体格式请参考KeyStroke.getKeyStroke(String) 。
action key一部分是InputMap的InputMap的KeyStroke 。
以下说明如何加载一个InputMap与两个key-action对:
LookAndFeel.loadKeyBindings(inputMap, new Object[] {
"control X", "cut",
"control V", "paste"
});
提供null绑定列表( keys )不会以任何方式更改retMap 。
指定null action key导致从InputMap删除keystroke's条目。 A null keystroke被忽略。
retMap -
InputMap将
key-action对添加到
keys - 绑定添加到
retMap
NullPointerException - 如果
keys是
non-null ,不是空的,而
retMap是
null
KeyStroke.getKeyStroke(String) , InputMap
public static Object makeIcon(类<?> baseClass, String gifFile)
UIDefault.LazyValue 。
返回的值是UIDefaults.LazyValue的UIDefaults.LazyValue 。
当对返回的对象调用createValue时,将加载映像。
如果图像是non-null ,那么它将被包裹在一个Icon ,它实现了UIResource 。
图像使用Class.getResourceAsStream(gifFile)加载。
此方法不会以任何方式检查参数。 强烈建议提供non-null值,否则可能会在返回的对象上调用createValue时发生createValue 。
baseClass -
类用于加载资源
gifFile - 加载图像的路径
UIDefaults.LazyValue ;
当解析时, LazyValue加载指定的图像
UIDefaults.LazyValue , Icon , Class.getResourceAsStream(String)
public LayoutStyle getLayoutStyle()
LayoutStyle为这种外观和感觉。
这从来没有返回null 。
一般不要使用LayoutStyle的外观和感觉,而是使用LayoutStyle方法getInstance 。
LayoutStyle为这种外观和感觉
LayoutStyle.getInstance()
public void provideErrorFeedback(Component component)
JTextField 。
默认实现哔声。
希望不同行为的子类应该覆盖此,并提供其他反馈。
component -
Component发生错误,可能是
null指示错误情况与Component没有直接
Component
public static Object getDesktopPropertyValue(String systemPropertyName, Object fallbackValue)
Toolkit.getDefaultToolkit().getDesktopProperty()返回指定的系统桌面属性的Toolkit.getDefaultToolkit().getDesktopProperty() 。
如果指定属性的值为null ,则返回fallbackValue 。
systemPropertyName - 要查询的系统桌面属性的名称
fallbackValue - 如果系统值为null,则返回值作为值
Toolkit.getDesktopProperty(java.lang.String)
public Icon getDisabledIcon(JComponent component, Icon icon)
Icon具有禁用外观。
这种方法被用于产生一个禁用Icon时,未指定的一个。
例如,如果您创建一个JButton ,并且仅通过setIcon指定一个Icon , setIcon此方法来生成禁用的Icon 。
如果null传递为icon此方法返回null 。
有些看起来和感觉可能不会使残疾人Icon ,在这种情况下他们会忽略这一点。
component -
JComponent将显示
Icon ,可能是
null
icon -
Icon生成禁用图标
Icon ,或
null如果合适
Icon不能生成
public Icon getDisabledSelectedIcon(JComponent component, Icon icon)
Icon供被还被选中的禁用组件使用。
此方法用于为禁用和选定状态的Icon生成Icon,但对于该状态不具有特定的Icon 。
例如,如果您创建了一个JButton ,并且只通过setIcon指定了一个Icon , setIcon此方法来生成禁用和选择的Icon 。
如果null传递为icon此方法返回null 。
有些看起来和感觉可能不会渲染残疾人并选择Icon ,在这种情况下,他们将忽略这一点。
component -
JComponent将显示
Icon ,可能是
null
icon -
Icon生成禁用和选定的图标
null如果合适
Icon不能生成。
public abstract String getName()
public abstract String getID()
public abstract String getDescription()
public boolean getSupportsWindowDecorations()
true如果LookAndFeel返回RootPaneUI实例支持提供Window在装饰JRootPane 。
默认实现返回false ,支持Window装饰的子类应该覆盖此并返回true 。
true如果由这种外观创建的
RootPaneUI实例支持客户端装饰
JDialog.setDefaultLookAndFeelDecorated(boolean) ,
JFrame.setDefaultLookAndFeelDecorated(boolean) ,
JRootPane.setWindowDecorationStyle(int)
public abstract boolean isNativeLookAndFeel()
true 。
例如,当底层平台是Solaris运行CDE时,CDE / Motif的外观实现将返回true 。
true如果这种外观和感觉代表底层平台的外观和感觉
public abstract boolean isSupportedLookAndFeel()
true如果底层平台支持和/或允许这种外观和感觉。
如果外观和风格取决于未为当前平台定义的特殊资源或法律协议,则此方法返回false 。
true如果这是支持的外观和感觉
UIManager.setLookAndFeel(javax.swing.LookAndFeel)
public void initialize()
UIManager调用。
在UIManager调用getDefaults之前调用此方法。
该方法旨在对外观进行任何初始化。
子类应该在这里进行任何一次性设置,而不是在静态初始化器中进行任何一次性设置,因为可以加载外观和感觉类对象,以发现isSupportedLookAndFeel()返回false 。
public void uninitialize()
UIManager调用。
例如,当外观和感觉改变时, UIManager.setLookAndFeel调用它。
子类可以选择在这里释放一些资源。
public UIDefaults getDefaults()
UIManager当外观被设置为当前的外观和后initialize已被调用。
initialize() ,
uninitialize() ,
UIManager.setLookAndFeel(javax.swing.LookAndFeel)
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.