下面列出了怎么用javax.swing.LookAndFeel的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
protected void installDefaults() {
super.installDefaults();
LookAndFeel.installBorder( tree, "Tree.border" );
selectionBackground = UIManager.getColor( "Tree.selectionBackground" );
selectionForeground = UIManager.getColor( "Tree.selectionForeground" );
selectionInactiveBackground = UIManager.getColor( "Tree.selectionInactiveBackground" );
selectionInactiveForeground = UIManager.getColor( "Tree.selectionInactiveForeground" );
selectionBorderColor = UIManager.getColor( "Tree.selectionBorderColor" );
wideSelection = UIManager.getBoolean( "Tree.wideSelection" );
showCellFocusIndicator = UIManager.getBoolean( "Tree.showCellFocusIndicator" );
// scale
int rowHeight = FlatUIUtils.getUIInt( "Tree.rowHeight", 16 );
if( rowHeight > 0 )
LookAndFeel.installProperty( tree, "rowHeight", UIScale.scale( rowHeight ) );
setLeftChildIndent( UIScale.scale( getLeftChildIndent() ) );
setRightChildIndent( UIScale.scale( getRightChildIndent() ) );
}
protected void initComponentDefaults(UIDefaults table) {
super.initComponentDefaults( table );
// I'll have to copy some of the resource definitions here, since the
// original code in BasicLookAndFeel (from which we inherit) uses
// getClass() to find its resources. That will fail since my
// classloader does not have these resources.
//
// So, the trick is to replace getClass() with MetalLookAndFeel.class
Object[] defaults = {
"OptionPane.errorIcon", LookAndFeel.makeIcon(MetalLookAndFeel.class, "icons/Error.gif"),
"OptionPane.informationIcon", LookAndFeel.makeIcon(MetalLookAndFeel.class, "icons/Inform.gif"),
"OptionPane.warningIcon", LookAndFeel.makeIcon(MetalLookAndFeel.class, "icons/Warn.gif"),
"OptionPane.questionIcon", LookAndFeel.makeIcon(MetalLookAndFeel.class, "icons/Question.gif"),
"InternalFrame.icon", LookAndFeel.makeIcon(BasicLookAndFeel.class, "icons/JavaCup.gif"),
// Button margin slightly smaller than metal to save space
"Button.margin", new InsetsUIResource(1, 11, 1, 11),
};
table.putDefaults(defaults);
}
protected void uninstallClientDecorations() {
LookAndFeel.uninstallBorder( rootPane );
setTitlePane( null );
if( windowResizer != null ) {
windowResizer.uninstall();
windowResizer = null;
}
if( oldLayout != null ) {
rootPane.setLayout( oldLayout );
oldLayout = null;
}
if( rootPane.getWindowDecorationStyle() == JRootPane.NONE ) {
rootPane.revalidate();
rootPane.repaint();
}
}
@Override
protected void installDefaults() {
super.installDefaults();
LookAndFeel.installProperty( spinner, "opaque", false );
minimumWidth = UIManager.getInt( "Component.minimumWidth" );
buttonStyle = UIManager.getString( "Spinner.buttonStyle" );
arrowType = UIManager.getString( "Component.arrowType" );
isIntelliJTheme = UIManager.getBoolean( "Component.isIntelliJTheme" );
borderColor = UIManager.getColor( "Component.borderColor" );
disabledBorderColor = UIManager.getColor( "Component.disabledBorderColor" );
disabledBackground = UIManager.getColor( "Spinner.disabledBackground" );
disabledForeground = UIManager.getColor( "Spinner.disabledForeground" );
buttonBackground = UIManager.getColor( "Spinner.buttonBackground" );
buttonArrowColor = UIManager.getColor( "Spinner.buttonArrowColor" );
buttonDisabledArrowColor = UIManager.getColor( "Spinner.buttonDisabledArrowColor" );
buttonHoverArrowColor = UIManager.getColor( "Spinner.buttonHoverArrowColor" );
padding = UIManager.getInsets( "Spinner.padding" );
// scale
padding = scale( padding );
MigLayoutVisualPadding.install( spinner );
}
protected void installDefaults(JScrollPane scrollpane) {
LookAndFeel.installBorder(scrollpane, "ScrollPane.border");
LookAndFeel.installColorsAndFont(scrollpane, "ScrollPane.background", "ScrollPane.foreground", "ScrollPane.font");
Border vpBorder = scrollpane.getViewportBorder();
if ((vpBorder == null) || (vpBorder instanceof UIResource)) {
vpBorder = UIManager.getBorder("ScrollPane.viewportBorder");
scrollpane.setViewportBorder(vpBorder);
}
Object obj = UIManager.get("ScrollPane.cornerPainter");
if (obj != null && obj instanceof SeaGlassPainter) {
cornerPainter = (SeaGlassPainter) obj;
}
LookAndFeel.installProperty(scrollpane, "opaque", Boolean.TRUE);
updateStyle(scrollpane);
}
/**
* 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();
}
}
protected void addWindowsDefaults(LookAndFeelAddons addon, List defaults) {
super.addWindowsDefaults(addon, defaults);
defaults.add(JTipOfTheDay.uiClassID);
defaults.add(WindowsTipOfTheDayUI.class.getName());
defaults.add("TipOfTheDay.background");
defaults.add(new ColorUIResource(128, 128, 128));
defaults.add("TipOfTheDay.font");
defaults.add(UIManager.getFont("Label.font").deriveFont(13f));
defaults.add("TipOfTheDay.icon");
defaults.add(LookAndFeel.makeIcon(WindowsTipOfTheDayUI.class,
"tipoftheday.png"));
defaults.add("TipOfTheDay.border");
defaults
.add(new BorderUIResource(new WindowsTipOfTheDayUI.TipAreaBorder()));
addResource(defaults,
"com.l2fprod.common.swing.plaf.windows.resources.TipOfTheDay");
}
/**
* Overridden to message super and forward the method to the tree. Since
* the tree is not actually in the component hierarchy it will never receive
* this unless we forward it in this manner.
*/
//----------//
// updateUI //
//----------//
@Override
public void updateUI ()
{
super.updateUI();
if (tree != null) {
tree.updateUI();
}
// Use the tree's default foreground and background colors in the
// table.
LookAndFeel.installColorsAndFont(
this,
"Tree.background",
"Tree.foreground",
"Tree.font");
}
protected void installDefaults() {
group.setOpaque(true);
group.setBorder(createPaneBorder());
((JComponent)group.getContentPane()).setBorder(createContentPaneBorder());
LookAndFeel.installColorsAndFont(
group,
"TaskPaneGroup.background",
"TaskPaneGroup.foreground",
"TaskPaneGroup.font");
LookAndFeel.installColorsAndFont(
(JComponent)group.getContentPane(),
"TaskPaneGroup.background",
"TaskPaneGroup.foreground",
"TaskPaneGroup.font");
}
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
JLabel renderer = (JLabel)super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
if(value instanceof PluginTreeNode) {
renderer.setEnabled(true);
renderer.setIcon(pluginIcon);
} else if(value instanceof PluginResourceTreeNode) {
PluginResourceTreeNode node = (PluginResourceTreeNode)value;
if(!node.isLeaf() || ((PluginFileFilter)pluginFileFilterBox.getSelectedItem()).pattern.matcher(node.fullPath).find()) {
renderer.setEnabled(true);
} else {
// trick lifted from DefaultTreeCellRenderer to get the right disabled icon
Icon icon = renderer.getIcon();
LookAndFeel laf = UIManager.getLookAndFeel();
Icon disabledIcon = laf.getDisabledIcon(tree, icon);
if (disabledIcon != null) icon = disabledIcon;
renderer.setDisabledIcon(icon);
renderer.setEnabled(false);
}
}
return renderer;
}
protected void addBasicDefaults(LookAndFeelAddons addon, List defaults) {
defaults.add(JTipOfTheDay.uiClassID);
defaults.add(BasicTipOfTheDayUI.class.getName());
defaults.add("TipOfTheDay.font");
defaults.add(UIManager.getFont("TextPane.font"));
defaults.add("TipOfTheDay.tipFont");
defaults.add(UIManager.getFont("Label.font").deriveFont(Font.BOLD, 13f));
defaults.add("TipOfTheDay.background");
defaults.add(new ColorUIResource(Color.white));
defaults.add("TipOfTheDay.icon");
defaults.add(LookAndFeel.makeIcon(BasicTipOfTheDayUI.class,
"TipOfTheDay24.gif"));
defaults.add("TipOfTheDay.border");
defaults.add(new BorderUIResource(BorderFactory.createLineBorder(new Color(
117, 117, 117))));
addResource(defaults,
"com.l2fprod.common.swing.plaf.basic.resources.TipOfTheDay");
}
private static int getLFHeightAdjustment() {
LookAndFeel lf = UIManager.getLookAndFeel();
String lfID = lf.getID();
logger.fine("createSingleLineEditor(): current L&F = '"+lfID+"'");
if ("Metal".equals(lfID)) {
return 0;
}
if ("GTK".equals(lfID)) {
return 2;
}
if ("Motif".equals(lfID)) {
return 3;
}
if ("Nimbus".equals(lfID)) {
return 0;
}
if ("Aqua".equals(lfID)) {
return -2;
}
return 0;
}
/**
* Overridden to message super and forward the method to the tree.
* Since the tree is not actually in the component hieachy it will
* never receive this unless we forward it in this manner.
**/
@Override
public void updateUI()
{
super.updateUI();
if (tree != null)
{
tree.updateUI();
}
// Use the tree's default foreground and background
// colors in the table
LookAndFeel.installColorsAndFont(this, "Tree.background", //$NON-NLS-1$
"Tree.foreground", //$NON-NLS-1$
"Tree.font"); //$NON-NLS-1$
}
/**
* Add a set of MenuItems to the given JMenu, one for each possible L&F.
* if this platform doesnt support the L&F, disable the MenuItem.
*/
public void addToMenu(JMenu menu) {
UIManager.LookAndFeelInfo[] plafInfo = UIManager.getInstalledLookAndFeels();
for (UIManager.LookAndFeelInfo aPlafInfo : plafInfo) {
addToMenu(aPlafInfo.getName(), aPlafInfo.getClassName(), menu);
}
LookAndFeel current = UIManager.getLookAndFeel();
System.out.printf("current L&F=%s%n", current.getName());
}
static void applyNbSkin() {
LookAndFeel lookAndFeel = UIManager.getLookAndFeel();
String name = findLafName(lookAndFeel.getName());
if (name == null) {
return;
}
String resource = "nbres:/org/netbeans/modules/htmlui/css/wizard-" + name + ".css";
loadCss(resource);
}
/**
* Sets the parent of the passed in ActionMap to be the audio action
* map.
*/
static void installAudioActionMap(ActionMap map) {
LookAndFeel laf = UIManager.getLookAndFeel();
if (laf instanceof BasicLookAndFeel) {
map.setParent(((BasicLookAndFeel)laf).getAudioActionMap());
}
}
/**
* Installs the appropriate <code>Border</code> onto the <code>
* JRootPane</code>.
*
* @param root the root pane.
*/
public void installBorder(JRootPane root) {
int style = root.getWindowDecorationStyle();
if (style == JRootPane.NONE) {
LookAndFeel.uninstallBorder(root);
} else {
root.setBorder(new SeaGlassBorder(this, new Insets(0, 1, 1, 1)));
}
}
/**
* Sets the parent of the passed in ActionMap to be the audio action
* map.
*/
static void installAudioActionMap(ActionMap map) {
LookAndFeel laf = UIManager.getLookAndFeel();
if (laf instanceof BasicLookAndFeel) {
map.setParent(((BasicLookAndFeel)laf).getAudioActionMap());
}
}
/**
* Factory methods for obtaining the current <code>LayoutStyle</code> object
* appropriate for the current look and feel.
*
* @return the current LayoutStyle instance
*/
public static LayoutStyle getSharedInstance() {
Object layoutImpl = UIManager.get("LayoutStyle.instance");
if (layoutImpl != null && (layoutImpl instanceof LayoutStyle)) {
return (LayoutStyle) layoutImpl;
}
LookAndFeel currentLAF = UIManager.getLookAndFeel();
if (layoutStyle == null || currentLAF != laf) {
laf = currentLAF;
String lafID = laf.getID();
if (USE_CORE_LAYOUT_STYLE) {
if ("Aqua" == lafID) {
try {
currentLAF.getClass().getDeclaredMethod("getLayoutStyle", new Class[0]);
layoutStyle = new SwingLayoutStyle();
} catch (NoSuchMethodException nsfex) {
// getLayoutStyle() not overriden => use our own (issue
// 52)
layoutStyle = new AquaLayoutStyle();
}
} else {
layoutStyle = new SwingLayoutStyle();
}
} else if ("Metal" == lafID) {
layoutStyle = new MetalLayoutStyle();
} else if ("Windows" == lafID) {
layoutStyle = new WindowsLayoutStyle();
} else if ("GTK" == lafID) {
layoutStyle = new GnomeLayoutStyle();
} else if ("Aqua" == lafID) {
layoutStyle = new AquaLayoutStyle();
} else {
layoutStyle = new LayoutStyle();
}
}
return layoutStyle;
}
@Override
protected void installDefaults( JSlider slider ) {
super.installDefaults( slider );
LookAndFeel.installProperty( slider, "opaque", false );
trackWidth = UIManager.getInt( "Slider.trackWidth" );
thumbWidth = UIManager.getInt( "Slider.thumbWidth" );
trackColor = UIManager.getColor( "Slider.trackColor" );
thumbColor = UIManager.getColor( "Slider.thumbColor" );
focusColor = FlatUIUtils.getUIColor( "Slider.focusedColor", "Component.focusColor" );
hoverColor = FlatUIUtils.getUIColor( "Slider.hoverColor", focusColor );
disabledForeground = UIManager.getColor( "Slider.disabledForeground" );
}
@Override
protected void uninstallDefaults() {
super.uninstallDefaults();
LookAndFeel.uninstallBorder( tree );
selectionBackground = null;
selectionForeground = null;
selectionInactiveBackground = null;
selectionInactiveForeground = null;
selectionBorderColor = null;
}
@Override
public void installUI(JComponent c)
{
super.installUI(c);
LookAndFeel.installProperty(frame, "opaque", Boolean.FALSE);
}
@Override
protected void installDefaults() {
super.installDefaults();
LookAndFeel.installProperty( progressBar, "opaque", false );
arc = UIManager.getInt( "ProgressBar.arc" );
horizontalSize = UIManager.getDimension( "ProgressBar.horizontalSize" );
verticalSize = UIManager.getDimension( "ProgressBar.verticalSize" );
}
/**
* Helper method to play a named sound.
*
* @param c JComponent to play the sound for.
* @param actionKey Key for the sound.
*/
static void playSound(JComponent c, Object actionKey) {
LookAndFeel laf = UIManager.getLookAndFeel();
if (laf instanceof BasicLookAndFeel) {
ActionMap map = c.getActionMap();
if (map != null) {
Action audioAction = map.get(actionKey);
if (audioAction != null) {
// pass off firing the Action to a utility method
((BasicLookAndFeel)laf).playSound(audioAction);
}
}
}
}
@Override
public void installUI( JComponent c ) {
super.installUI( c );
int focusWidth = UIManager.getInt( "Component.focusWidth" );
LookAndFeel.installProperty( c, "opaque", focusWidth == 0 );
MigLayoutVisualPadding.install( scrollpane );
}
@Override
protected void installDefaults() {
super.installDefaults();
showHorizontalLines = UIManager.getBoolean( "Table.showHorizontalLines" );
showVerticalLines = UIManager.getBoolean( "Table.showVerticalLines" );
intercellSpacing = UIManager.getDimension( "Table.intercellSpacing" );
selectionBackground = UIManager.getColor( "Table.selectionBackground" );
selectionForeground = UIManager.getColor( "Table.selectionForeground" );
selectionInactiveBackground = UIManager.getColor( "Table.selectionInactiveBackground" );
selectionInactiveForeground = UIManager.getColor( "Table.selectionInactiveForeground" );
toggleSelectionColors();
int rowHeight = FlatUIUtils.getUIInt( "Table.rowHeight", 16 );
if( rowHeight > 0 )
LookAndFeel.installProperty( table, "rowHeight", UIScale.scale( rowHeight ) );
if( !showHorizontalLines ) {
oldShowHorizontalLines = table.getShowHorizontalLines();
table.setShowHorizontalLines( false );
}
if( !showVerticalLines ) {
oldShowVerticalLines = table.getShowVerticalLines();
table.setShowVerticalLines( false );
}
if( intercellSpacing != null ) {
oldIntercellSpacing = table.getIntercellSpacing();
table.setIntercellSpacing( intercellSpacing );
}
// checkbox is non-opaque in FlatLaf and therefore would not paint selection
// --> make checkbox renderer opaque (but opaque in Metal or Windows LaF)
TableCellRenderer booleanRenderer = table.getDefaultRenderer( Boolean.class );
if( booleanRenderer instanceof JCheckBox )
((JCheckBox)booleanRenderer).setOpaque( true );
}
/**
* Helper method to play a named sound.
*
* @param c JComponent to play the sound for.
* @param actionKey Key for the sound.
*/
static void playSound(JComponent c, Object actionKey) {
LookAndFeel laf = UIManager.getLookAndFeel();
if (laf instanceof BasicLookAndFeel) {
ActionMap map = c.getActionMap();
if (map != null) {
Action audioAction = map.get(actionKey);
if (audioAction != null) {
// pass off firing the Action to a utility method
((BasicLookAndFeel)laf).playSound(audioAction);
}
}
}
}
private static synchronized void setLookAndFeelProperties(
LookAndFeel lookAndFeel) {
if (lookAndFeel == null)
return;
String packagePrefix = "com.michaelbaranov.microba.";
// all L&F
UIManager.put("microba.CalendarPaneUI", packagePrefix
+ "calendar.ui.basic.BasicCalendarPaneUI");
UIManager.put("microba.DatePickerUI", packagePrefix
+ "calendar.ui.basic.BasicDatePickerUI");
UIManager.put("microba.GradientUI", packagePrefix
+ "gradient.ui.basic.BasicGradientUI");
UIManager.put("microba.GradientEditorUI", packagePrefix
+ "gradienteditor.ui.basic.BasicGradientEditorUI");
UIManager.put("microba.MarkerBarUI", packagePrefix
+ "marker.ui.basic.BasicMarkerBarUI");
// particular L&F
if (lookAndFeel.getID().equals("Windows")) {
UIManager.put("microba.MarkerBarUI", packagePrefix
+ "marker.ui.windows.WindowsMarkerBarUI");
} else if (lookAndFeel.getID().equals("Metal")) {
UIManager.put("microba.MarkerBarUI", packagePrefix
+ "marker.ui.metal.MetalMarkerBarUI");
} else if (lookAndFeel.getID().equals("Motif")) {
UIManager.put("microba.MarkerBarUI", packagePrefix
+ "marker.ui.motif.MotifMarkerBarUI");
}
}
/**
* Sets the parent of the passed in ActionMap to be the audio action
* map.
*/
static void installAudioActionMap(ActionMap map) {
LookAndFeel laf = UIManager.getLookAndFeel();
if (laf instanceof BasicLookAndFeel) {
map.setParent(((BasicLookAndFeel)laf).getAudioActionMap());
}
}
/**
* @see javax.swing.plaf.basic.BasicPanelUI#uninstallDefaults(javax.swing.JPanel)
*/
protected void uninstallDefaults(JPanel p) {
SeaGlassContext context = getContext(p, ENABLED);
style.uninstallDefaults(context);
context.dispose();
style = null;
// Restore original opacity if not changed by the code.
LookAndFeel.installProperty(p, "opaque", originalOpacity);
}