下面列出了javax.swing.JComponent#getBackground ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected void installDefaults() {
if( group.getContentPane() instanceof JComponent ) {
// remove default SwingX content border, which may be still set when switching LaF
JComponent content = (JComponent) group.getContentPane();
Border contentBorder = content.getBorder();
if( contentBorder instanceof CompoundBorder &&
((CompoundBorder)contentBorder).getOutsideBorder() instanceof BasicTaskPaneUI.ContentPaneBorder &&
((CompoundBorder)contentBorder).getInsideBorder() instanceof EmptyBorder )
{
content.setBorder( null );
}
// set non-UIResource color to background to avoid that it is lost when switching LaF
background = UIManager.getColor( "TaskPane.background" );
Color bg = content.getBackground();
if( bg == null || bg instanceof UIResource ) {
content.setBackground( new Color( background.getRGB(), true ) );
}
}
roundHeight = FlatUIUtils.getUIInt( "TaskPane.roundHeight", UIManager.getInt( "Component.arc" ) );
super.installDefaults();
}
@Override
public void paint( Graphics g, JComponent c ) {
// fill background even if not opaque if
// - contentAreaFilled is true and
// - if background was explicitly set to a non-UIResource color
if( !c.isOpaque() &&
((AbstractButton)c).isContentAreaFilled() &&
!(c.getBackground() instanceof UIResource) )
{
g.setColor( c.getBackground() );
g.fillRect( 0, 0, c.getWidth(), c.getHeight() );
}
// small insets fix
int focusWidth = getIconFocusWidth( c );
if( focusWidth > 0 ) {
boolean ltr = c.getComponentOrientation().isLeftToRight();
Insets insets = c.getInsets( tempInsets );
int leftOrRightInset = ltr ? insets.left : insets.right;
if( focusWidth > leftOrRightInset ) {
// The left (or right) inset is smaller than the focus width, which may be
// the case if insets were explicitly reduced (e.g. with an EmptyBorder).
// In this case the width has been increased in getPreferredSize() and
// here it is necessary to fix icon and text painting location.
int offset = focusWidth - leftOrRightInset;
if( !ltr )
offset = -offset;
// move the graphics origin to the left (or right)
g.translate( offset, 0 );
super.paint( g, c );
g.translate( -offset, 0 );
return;
}
}
super.paint( FlatLabelUI.createGraphicsHTMLTextYCorrection( g, c ), c );
}
protected Color getBackgroundBase( JComponent c, boolean def ) {
// use component background if explicitly set
Color bg = c.getBackground();
if( isCustomBackground( bg ) )
return bg;
return def ? defaultBackground : bg;
}
/**
* Change background of given component to light gray on Mac look and feel
* when the component is in a tabbed container and its background hasn't been
* already changed (is instance of UIResource).
* @param c
*/
static void adjustBackground( JComponent c ) {
if( !isAquaLaF || useDefaultBackground )
return;
if( !isInTabbedContainer(c) )
return;
Color currentBackground = c.getBackground();
if( currentBackground instanceof UIResource ) {
c.setBackground(UIManager.getColor("NbExplorerView.background"));
}
}
/**
* adds painting of overall border
*/
@Override
public void paint(Graphics g, JComponent c) {
ColorUtil.setupAntialiasing(g);
Color col = c.getBackground();
if (col != null) {
g.setColor (col);
g.fillRect (0, 0, c.getWidth(), c.getHeight());
}
paintOverallBorder(g, c);
super.paint(g, c);
}
protected void formatLabelColors(JComponent jc, boolean isSelected,
int rowNumber) {
if (comboBox != null) {
if (isSelected) {
label.setBackground(UIManager
.getColor("ComboBox.selectionBackground"));
label.setForeground(UIManager
.getColor("ComboBox.selectionForeground"));
} else {
label.setBackground(UIManager.getColor("ComboBox.background"));
label.setForeground(UIManager.getColor("ComboBox.foreground"));
}
} else {
if (isSelected) {
if (jc instanceof JList) {
label.setBackground(UIManager
.getColor("List.selectionBackground"));
label.setForeground(UIManager
.getColor("List.selectionForeground"));
} else {
label.setBackground(UIManager
.getColor("Menu.selectionBackground"));
label.setForeground(UIManager
.getColor("Menu.selectionForeground"));
}
} else {
Color background;
if (jc != null) {
background = jc.getBackground();
if (rowNumber % 2 == 1 && isAlternatingBackround(jc)) {
background = darken(background);
}
} else {
background = UIManager.getColor("Menu.background");
}
label.setBackground(background);
label.setForeground(UIManager.getColor("Menu.foreground"));
}
}
}
@Override
protected Object[] getExtendedCacheKeys(JComponent c) {
return (c != null)
? new Object[] { c.getBackground() }
: null;
}
@Override
protected Object[] getExtendedCacheKeys(JComponent c) {
return (c != null)
? new Object[] { c.getBackground() }
: null;
}
@Override
protected Object[] getExtendedCacheKeys(JComponent c) {
return (c != null)
? new Object[] { c.getBackground() }
: null;
}
@Override
protected Object[] getExtendedCacheKeys(JComponent c) {
return (c != null)
? new Object[] { c.getBackground() }
: null;
}
@Override
protected Object[] getExtendedCacheKeys(JComponent c) {
return (c != null)
? new Object[] { c.getBackground() }
: null;
}
@Override
protected Object[] getExtendedCacheKeys(JComponent c) {
return (c != null)
? new Object[] { c.getBackground() }
: null;
}
/** Apply this coloring to component colors/font.
* The underline and strikeThrough line colors have no effect here.
*/
public void apply(JComponent c) {
// Possibly change font
if (font != null) {
if (fontMode == FONT_MODE_DEFAULT) {
c.setFont(font);
} else { // non-default font-mode
Font origFont = c.getFont();
if (origFont != null) {
synchronized (cacheLock) {
Font f = (Font)fontAndForeColorCache.get(origFont);
if (f == null) {
f = modifyFont(origFont);
fontAndForeColorCache.put(origFont, f);
}
c.setFont(f);
}
}
}
}
// Possibly change fore-color
if (foreColor != null) {
if (!hasAlpha(foreColor)) {
c.setForeground(foreColor);
} else { // non-default fore color-mode
Color origForeColor = c.getForeground();
if (origForeColor != null) {
synchronized (cacheLock) {
Color fc = (Color)fontAndForeColorCache.get(origForeColor);
if (fc == null) {
fc = modifyForeColor(origForeColor);
fontAndForeColorCache.put(origForeColor, fc);
}
c.setForeground(fc);
}
}
}
}
// Possibly change back-color
if (backColor != null) {
if (!hasAlpha(backColor)) {
c.setBackground(backColor);
} else { // non-default back color-mode
Color origBackColor = c.getBackground();
if (origBackColor != null) {
synchronized (cacheLock) {
Color bc = (Color)backColorCache.get(origBackColor);
if (bc == null) {
bc = modifyBackColor(origBackColor);
backColorCache.put(origBackColor, bc);
}
c.setBackground(bc);
}
}
}
}
}
@Override
protected Object[] getExtendedCacheKeys(JComponent c) {
return (c != null)
? new Object[] { c.getBackground() }
: null;
}
@Override
protected Object[] getExtendedCacheKeys(JComponent c) {
return (c != null)
? new Object[] { c.getBackground() }
: null;
}
@Override
protected Object[] getExtendedCacheKeys(JComponent c) {
return (c != null)
? new Object[] { c.getBackground() }
: null;
}
@Override
protected Object[] getExtendedCacheKeys(JComponent c) {
return (c != null)
? new Object[] { c.getBackground() }
: null;
}
@Override
protected Object[] getExtendedCacheKeys(JComponent c) {
return (c != null)
? new Object[] { c.getBackground() }
: null;
}
/**
* gets a worker that can make a given component flash
*
* @param flashMe component to make flash
* @param flashColor color to use for flashing
* @param isBack whether display color is background (rather than foreground)
* @return SwingWorker that will make given component flash if run
*/
public static SwingWorker getFlashWorker(final JComponent flashMe, final Color flashColor, final boolean isBack) {
// this will pop out in its own little thread...
return new SwingWorker() {
@Override
protected Object doInBackground() {
Color originColor;
if (isBack) {
originColor = flashMe.getBackground();
} else {
originColor = flashMe.getForeground();
}
Color requiredColor = flashColor.equals(originColor)
? Color.white : flashColor;
try {
for (int i = 0; i < PGTUtil.NUM_MENU_FLASHES; i++) {
if (isBack) {
flashMe.setBackground(requiredColor);
} else {
flashMe.setEnabled(false);
}
// suppression for this is broken. Super annoying.
Thread.sleep(PGTUtil.MENU_FLASH_SLEEP);
if (isBack) {
flashMe.setBackground(originColor);
} else {
flashMe.setEnabled(true);
}
Thread.sleep(PGTUtil.MENU_FLASH_SLEEP);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
// catch of thread interrupt not logworthy
// IOHandler.writeErrorLog(e);
}
return null;
}
};
}
/**
* Thie method create a new effect mouse hover static, not create a wake effect
* This method is util in all component for click, an example: The button for JSpinner, JCombobox, JScroolbar
* @param c is the component
* @param colorEffect is the color for effect, (For using color, look the MaterialColors class)
* @param colorOnClick is the color of the component on click on it
* @return a new MaterialUIStaticMovement this class implement a only MouseListner for moment
* @author https://github.com/vincenzopalazzo
*/
public static MaterialUIStaticMovement getStaticMovement(JComponent c, Color colorEffect, Color colorOnClick){
if(c == null || colorEffect == null || colorOnClick == null){
throw new IllegalArgumentException("Che input arguments is/are null");
}
return new MaterialUIStaticMovement(c.getBackground(), colorEffect, colorOnClick);
}