下面列出了javax.swing.JComponent#setBackground ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected void prepareRenderer(JComponent comp) {
comp.setBackground(getBackground());
comp.setForeground(getForeground());
comp.setBounds(0, 0, getWidth(), getHeight());
JComponent innermost;
if ((innermost = findInnermostRenderer(comp)) instanceof JComboBox) {
if (comp.getLayout() != null) {
comp.getLayout().layoutContainer(comp);
}
}
if (!isTableUI() && ((InplaceEditor) comp).supportsTextEntry()) {
innermost.setBackground(PropUtils.getTextFieldBackground());
innermost.setForeground(PropUtils.getTextFieldForeground());
}
}
public void add(JComponent comp) {
comp.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 2));
comp.getInsets().bottom = 10;
comp.getInsets().top = 10;
comp.getInsets().right = 10;
comp.getInsets().left = 10;
if (zebra) {
comp.setBackground(Color.GRAY);
}
else {
comp.setBackground(Color.LIGHT_GRAY);
}
zebra = !zebra;
gbc.gridy++;
super.add(comp, gbc);
}
@Override
public void installUI(JComponent c) {
super.installUI(c);
if (c instanceof JButton) {
JButton btn = (JButton) c;
if (!(c instanceof CustomButton)) {
c.setForeground(Color.WHITE);
c.setBackground(ColorResource.getButtonBackColor());
btn.setBorderPainted(false);
}
}
}
@Override
public void uninstallUI(JComponent c) {
c.setFont (null);
c.setBackground (null);
c.setForeground (null);
c.setBorder (null);
c.setCursor(null);
this.dockingColor = null;
this.floatingColor = null;
super.uninstallUI(c);
}
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
setValue(value, row); // NOTE: should use table.convertRowIndexToModel(row)
JComponent comp = getComponent();
comp.setOpaque(false);
if (tree != null) {
comp.setForeground(tree.getForeground());
comp.setBackground(tree.getBackground());
}
return comp;
}
public static void setPanelBackground(JComponent panel, Color color) {
if (panel!=null){
panel.setBackground(color);
for (Component c : panel.getComponents()) {
if (c instanceof JPanel) {
setPanelBackground((JPanel) c, color);
}
}
}
}
/**
* {@inheritDoc}
*/
public void reset() {
for (JComponent c : colorMap.keySet()) {
c.setBackground(colorMap.get(c));
c.setToolTipText(null);
}
colorMap.clear();
}
/**
* Verifies the input with the side effect that the background color of {@code input} to {@code background} if
* returns {@code true}, or {@code warningBackground} otherwise
*
* @param input component
* @return if input is valid
*/
public boolean shouldYieldFocus(JComponent input) {
boolean isValidInput = verify(input);
if (isValidInput) {
input.setBackground(background);
} else {
input.setBackground(warningBackground);
}
return isValidInput;
}
public DiagramScene(Action[] actions, DiagramViewModel model) {
this.actions = actions;
selectedWidgets = new ArrayList<FigureWidget>();
content = new InstanceContent();
lookup = new AbstractLookup(content);
this.setCheckClipping(true);
this.getInputBindings().setZoomActionModifiers(0);
JComponent comp = this.createView();
comp.setDoubleBuffered(true);
comp.setBackground(Color.WHITE);
comp.setOpaque(true);
this.setBackground(Color.WHITE);
this.setOpaque(true);
scrollPane = new JScrollPane(comp);
scrollPane.setBackground(Color.WHITE);
scrollPane.getVerticalScrollBar().setUnitIncrement(SCROLL_UNIT_INCREMENT);
scrollPane.getVerticalScrollBar().setBlockIncrement(SCROLL_BLOCK_INCREMENT);
scrollPane.getHorizontalScrollBar().setUnitIncrement(SCROLL_UNIT_INCREMENT);
scrollPane.getHorizontalScrollBar().setBlockIncrement(SCROLL_BLOCK_INCREMENT);
scrollPane.getViewport().addChangeListener(scrollChangeListener);
hoverAction = this.createWidgetHoverAction();
blockLayer = new LayerWidget(this);
this.addChild(blockLayer);
startLayer = new LayerWidget(this);
this.addChild(startLayer);
// TODO: String startLabelString = "Loading graph with " + originalDiagram.getFigures().size() + " figures and " + originalDiagram.getConnections().size() + " connections...";
String startLabelString = "";
LabelWidget w = new LabelWidget(this, startLabelString);
scrollChangeListener.register(w, new Point(10, 10));
w.setAlignment(LabelWidget.Alignment.CENTER);
startLabel = w;
startLayer.addChild(w);
mainLayer = new LayerWidget(this);
this.addChild(mainLayer);
topLeft = new Widget(this);
topLeft.setPreferredLocation(new Point(-BORDER_SIZE, -BORDER_SIZE));
this.addChild(topLeft);
bottomRight = new Widget(this);
bottomRight.setPreferredLocation(new Point(-BORDER_SIZE, -BORDER_SIZE));
this.addChild(bottomRight);
slotLayer = new LayerWidget(this);
this.addChild(slotLayer);
connectionLayer = new LayerWidget(this);
this.addChild(connectionLayer);
LayerWidget selectionLayer = new LayerWidget(this);
this.addChild(selectionLayer);
this.setLayout(LayoutFactory.createAbsoluteLayout());
this.getActions().addAction(hoverAction);
zoomAction = new BoundedZoomAction(1.1, false);
zoomAction.setMaxFactor(ZOOM_MAX_FACTOR);
zoomAction.setMinFactor(ZOOM_MIN_FACTOR);
this.getActions().addAction(ActionFactory.createMouseCenteredZoomAction(1.1));
panAction = new ExtendedPanAction();
this.getActions().addAction(panAction);
this.getActions().addAction(ActionFactory.createPopupMenuAction(popupMenuProvider));
LayerWidget selectLayer = new LayerWidget(this);
this.addChild(selectLayer);
this.getActions().addAction(ActionFactory.createRectangularSelectAction(rectangularSelectDecorator, selectLayer, rectangularSelectProvider));
blockWidgets = new HashMap<InputBlock, BlockWidget>();
boolean b = this.getUndoRedoEnabled();
this.setUndoRedoEnabled(false);
this.setNewModel(model);
this.setUndoRedoEnabled(b);
}
/**
*
* @param c
*/
protected final void setOpaqueWithLightGray(JComponent c) {
c.setOpaque(true);
c.setBackground(ReduxConstants.dataModelGray);
}
public DiagramScene(Action[] actions, DiagramViewModel model) {
this.actions = actions;
selectedWidgets = new ArrayList<FigureWidget>();
content = new InstanceContent();
lookup = new AbstractLookup(content);
this.setCheckClipping(true);
this.getInputBindings().setZoomActionModifiers(0);
JComponent comp = this.createView();
comp.setDoubleBuffered(true);
comp.setBackground(Color.WHITE);
comp.setOpaque(true);
this.setBackground(Color.WHITE);
this.setOpaque(true);
scrollPane = new JScrollPane(comp);
scrollPane.setBackground(Color.WHITE);
scrollPane.getVerticalScrollBar().setUnitIncrement(SCROLL_UNIT_INCREMENT);
scrollPane.getVerticalScrollBar().setBlockIncrement(SCROLL_BLOCK_INCREMENT);
scrollPane.getHorizontalScrollBar().setUnitIncrement(SCROLL_UNIT_INCREMENT);
scrollPane.getHorizontalScrollBar().setBlockIncrement(SCROLL_BLOCK_INCREMENT);
scrollPane.getViewport().addChangeListener(scrollChangeListener);
hoverAction = this.createWidgetHoverAction();
blockLayer = new LayerWidget(this);
this.addChild(blockLayer);
startLayer = new LayerWidget(this);
this.addChild(startLayer);
// TODO: String startLabelString = "Loading graph with " + originalDiagram.getFigures().size() + " figures and " + originalDiagram.getConnections().size() + " connections...";
String startLabelString = "";
LabelWidget w = new LabelWidget(this, startLabelString);
scrollChangeListener.register(w, new Point(10, 10));
w.setAlignment(LabelWidget.Alignment.CENTER);
startLabel = w;
startLayer.addChild(w);
mainLayer = new LayerWidget(this);
this.addChild(mainLayer);
topLeft = new Widget(this);
topLeft.setPreferredLocation(new Point(-BORDER_SIZE, -BORDER_SIZE));
this.addChild(topLeft);
bottomRight = new Widget(this);
bottomRight.setPreferredLocation(new Point(-BORDER_SIZE, -BORDER_SIZE));
this.addChild(bottomRight);
slotLayer = new LayerWidget(this);
this.addChild(slotLayer);
connectionLayer = new LayerWidget(this);
this.addChild(connectionLayer);
LayerWidget selectionLayer = new LayerWidget(this);
this.addChild(selectionLayer);
this.setLayout(LayoutFactory.createAbsoluteLayout());
this.getActions().addAction(hoverAction);
zoomAction = new BoundedZoomAction(1.1, false);
zoomAction.setMaxFactor(ZOOM_MAX_FACTOR);
zoomAction.setMinFactor(ZOOM_MIN_FACTOR);
this.getActions().addAction(ActionFactory.createMouseCenteredZoomAction(1.1));
panAction = new ExtendedPanAction();
this.getActions().addAction(panAction);
this.getActions().addAction(ActionFactory.createPopupMenuAction(popupMenuProvider));
LayerWidget selectLayer = new LayerWidget(this);
this.addChild(selectLayer);
this.getActions().addAction(ActionFactory.createRectangularSelectAction(rectangularSelectDecorator, selectLayer, rectangularSelectProvider));
blockWidgets = new HashMap<InputBlock, BlockWidget>();
boolean b = this.getUndoRedoEnabled();
this.setUndoRedoEnabled(false);
this.setNewModel(model);
this.setUndoRedoEnabled(b);
}
/** Set the background on a component. */
private JComponent setBackground(JComponent comp, Color c) {
comp.setOpaque(true);
comp.setBackground(c);
return comp;
}
/** Set the background on a component. */
private JComponent setBackground(JComponent comp, Color c) {
comp.setOpaque(true);
comp.setBackground(c);
return comp;
}
public DiagramScene(Action[] actions, DiagramViewModel model) {
this.actions = actions;
selectedWidgets = new ArrayList<FigureWidget>();
content = new InstanceContent();
lookup = new AbstractLookup(content);
this.setCheckClipping(true);
this.getInputBindings().setZoomActionModifiers(0);
JComponent comp = this.createView();
comp.setDoubleBuffered(true);
comp.setBackground(Color.WHITE);
comp.setOpaque(true);
this.setBackground(Color.WHITE);
this.setOpaque(true);
scrollPane = new JScrollPane(comp);
scrollPane.setBackground(Color.WHITE);
scrollPane.getVerticalScrollBar().setUnitIncrement(SCROLL_UNIT_INCREMENT);
scrollPane.getVerticalScrollBar().setBlockIncrement(SCROLL_BLOCK_INCREMENT);
scrollPane.getHorizontalScrollBar().setUnitIncrement(SCROLL_UNIT_INCREMENT);
scrollPane.getHorizontalScrollBar().setBlockIncrement(SCROLL_BLOCK_INCREMENT);
scrollPane.getViewport().addChangeListener(scrollChangeListener);
hoverAction = this.createWidgetHoverAction();
blockLayer = new LayerWidget(this);
this.addChild(blockLayer);
startLayer = new LayerWidget(this);
this.addChild(startLayer);
// TODO: String startLabelString = "Loading graph with " + originalDiagram.getFigures().size() + " figures and " + originalDiagram.getConnections().size() + " connections...";
String startLabelString = "";
LabelWidget w = new LabelWidget(this, startLabelString);
scrollChangeListener.register(w, new Point(10, 10));
w.setAlignment(LabelWidget.Alignment.CENTER);
startLabel = w;
startLayer.addChild(w);
mainLayer = new LayerWidget(this);
this.addChild(mainLayer);
topLeft = new Widget(this);
topLeft.setPreferredLocation(new Point(-BORDER_SIZE, -BORDER_SIZE));
this.addChild(topLeft);
bottomRight = new Widget(this);
bottomRight.setPreferredLocation(new Point(-BORDER_SIZE, -BORDER_SIZE));
this.addChild(bottomRight);
slotLayer = new LayerWidget(this);
this.addChild(slotLayer);
connectionLayer = new LayerWidget(this);
this.addChild(connectionLayer);
LayerWidget selectionLayer = new LayerWidget(this);
this.addChild(selectionLayer);
this.setLayout(LayoutFactory.createAbsoluteLayout());
this.getActions().addAction(hoverAction);
zoomAction = new BoundedZoomAction(1.1, false);
zoomAction.setMaxFactor(ZOOM_MAX_FACTOR);
zoomAction.setMinFactor(ZOOM_MIN_FACTOR);
this.getActions().addAction(ActionFactory.createMouseCenteredZoomAction(1.1));
panAction = new ExtendedPanAction();
this.getActions().addAction(panAction);
this.getActions().addAction(ActionFactory.createPopupMenuAction(popupMenuProvider));
LayerWidget selectLayer = new LayerWidget(this);
this.addChild(selectLayer);
this.getActions().addAction(ActionFactory.createRectangularSelectAction(rectangularSelectDecorator, selectLayer, rectangularSelectProvider));
blockWidgets = new HashMap<InputBlock, BlockWidget>();
boolean b = this.getUndoRedoEnabled();
this.setUndoRedoEnabled(false);
this.setNewModel(model);
this.setUndoRedoEnabled(b);
}
/** Set the background on a component. */
private JComponent setBackground(JComponent comp, Color c) {
comp.setOpaque(true);
comp.setBackground(c);
return comp;
}
/** Set the background on a component. */
private JComponent setBackground(JComponent comp, Color c) {
comp.setOpaque(true);
comp.setBackground(c);
return comp;
}
@Override
public void installUI(JComponent separator) {
super.installUI(separator);
separator.setBackground(style.getHighLightColor());
separator.setForeground(style.getShadowColor());
}
/** Paint the expandable sets. These are painted double width,
* across the entire width of the table. */
private void paintExpandableSets(Graphics g) {
int start = 0;
int end = getRowCount();
Insets ins = getInsets();
boolean canBeSelected = isKnownComponent(
KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner()
);
for (int i = 0; i < end; i++) {
int idx = start + i;
Object value = getValueAt(idx, 0);
if (value instanceof PropertySet) {
Rectangle r = getCellRect(idx, 0, false);
r.x = ins.left;
r.width = getWidth() - (ins.left + ins.right);
if (g.hitClip(r.x, r.y, r.width, r.height)) {
PropertySet ps = (PropertySet) value;
String txt = ps.getHtmlDisplayName();
boolean isHtml = txt != null;
if (!isHtml) {
txt = ps.getDisplayName();
}
if (htmlrenderer == null) {
htmlrenderer = HtmlRenderer.createRenderer();
}
JComponent painter = (JComponent) htmlrenderer.getTableCellRendererComponent(
this, txt, false, false, idx, 0
);
htmlrenderer.setHtml(isHtml);
htmlrenderer.setParentFocused(true);
htmlrenderer.setIconTextGap(2);
htmlrenderer.setIcon(
getPropertySetModel().isExpanded(ps) ? PropUtils.getExpandedIcon() : PropUtils.getCollapsedIcon()
);
boolean selected = canBeSelected && (getSelectedRow() == idx);
if (!selected) {
painter.setBackground(PropUtils.getSetRendererColor());
painter.setForeground(PropUtils.getSetForegroundColor());
} else {
painter.setBackground(PropUtils.getSelectedSetRendererColor());
painter.setForeground(PropUtils.getSelectedSetForegroundColor());
}
if( PropUtils.isAqua ) {
painter.setOpaque(false);
Graphics2D g2d = (Graphics2D) g;
Paint oldPaint = g2d.getPaint();
g2d.setPaint( new GradientPaint(r.x,r.y, Color.white, r.x, r.y+r.height/2, painter.getBackground()) );
g2d.fillRect(r.x, r.y, r.width, r.height);
g2d.setPaint(oldPaint);
} else {
painter.setOpaque(true);
}
paintComponent(g, painter, r.x, r.y, r.width, r.height);
}
}
}
}
/** 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);
}
}
}
}
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) {
// value is data column name
String realname = (value==null) ? "" : value.toString(); //$NON-NLS-1$
String name = realname;
if (OSPRuntime.isMac()) {
name = TeXParser.removeSubscripting(name);
}
Component c = renderer.getTableCellRendererComponent(table, name, isSelected, hasFocus, row, col);
if (headerFont==null) headerFont = c.getFont();
int labelCol = convertColumnIndexToView(0);
int xCol = (labelCol==0) ? 1 : 0;
int yCol = (labelCol<2) ? 2 : 1;
if(unselectedBG==null) {
unselectedBG = c.getBackground();
}
// backup color in case c has none
if(unselectedBG==null) {
unselectedBG = javax.swing.UIManager.getColor("Panel.background"); //$NON-NLS-1$
}
rowBG = dataToolTab.plot.getBackground();
Color bgColor = (col==xCol)? DataToolTable.xAxisColor:
(col==yCol)? DataToolTable.yAxisColor: rowBG;
if(!(c instanceof JComponent)) {
return c;
}
JComponent comp = (JComponent) c;
java.awt.Dimension dim = comp.getPreferredSize();
dim.height += 1;
dim.height = Math.max(getRowHeight()+2, dim.height);
panel.setPreferredSize(dim);
javax.swing.border.Border border = comp.getBorder();
if(border instanceof javax.swing.border.EmptyBorder) {
border = BorderFactory.createLineBorder(Color.LIGHT_GRAY);
}
panel.setBorder(border);
// determine font: italics if undeletable, bold if sorted column
Font font;
Dataset data = getDataset(realname);
if(!dataToolTab.isDeletable(data)) {
font = getSortedColumn()!=convertColumnIndexToModel(col)?
headerFont.deriveFont(Font.PLAIN+Font.ITALIC) : headerFont.deriveFont(Font.BOLD+Font.ITALIC);
} else {
font = getSortedColumn()!=convertColumnIndexToModel(col)?
headerFont.deriveFont(Font.PLAIN) : headerFont.deriveFont(Font.BOLD);
}
int[] cols = getSelectedColumns();
boolean selected = false;
for(int i = 0; i<cols.length; i++) {
selected = selected||(cols[i]==col);
}
selected = selected&&(convertColumnIndexToModel(col)>0);
bgColor = selected? selectedHeaderBG: bgColor;
// special case: textline doesn't work on OSX
if (OSPRuntime.isMac()) {
comp.setFont(font);
comp.setBackground(bgColor);
comp.setForeground(selected ? selectedHeaderFG : comp.getForeground());
if (comp instanceof JLabel) {
((JLabel)comp).setHorizontalAlignment(SwingConstants.CENTER);
}
return comp;
}
textLine.setText(name);
textLine.setFont(font);
textLine.setColor(selected ? selectedHeaderFG : comp.getForeground());
textLine.setBackground(bgColor);
panel.setBackground(bgColor);
return panel;
}