下面列出了javax.swing.JComponent#setFont ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
static void setStyleBasedOnThresholds(JComponent target, Integer duration,
CounterRequestAggregation aggregation) {
if (duration < aggregation.getWarningThreshold() || duration == 0) {
// si cette moyenne est < à la moyenne globale + 1 écart-type (paramétrable), c'est bien
// (si severeThreshold ou warningThreshold sont à 0 et mean à 0, c'est "info" et non "severe")
target.setForeground(DARKER_GREEN);
target.setFont(LABEL_PLAIN_FONT);
} else if (duration < aggregation.getSevereThreshold()) {
// sinon, si cette moyenne est < à la moyenne globale + 2 écart-types (paramétrable),
// attention à cette requête qui est plus longue que les autres
target.setForeground(Color.ORANGE);
target.setFont(LABEL_BOLD_FONT);
} else {
// sinon, (cette moyenne est > à la moyenne globale + 2 écart-types),
// cette requête est très longue par rapport aux autres ;
// il peut être opportun de l'optimiser si possible
target.setForeground(Color.RED);
target.setFont(LABEL_BOLD_FONT);
}
}
@Override
public void installUI(JComponent c) {
super.installUI(c);
c.setForeground(foregroundColor);
c.setBorder(border);
c.setFont(UIDefaults.FONT);
}
public void updateComponentSelectedState(JComponent c, boolean isSelected, JTable table, int row, int column, boolean hasFocus) {
Color fg = null;
Color bg = null;
JTable.DropLocation dropLocation = table.getDropLocation();
if (dropLocation != null
&& !dropLocation.isInsertRow()
&& !dropLocation.isInsertColumn()
&& dropLocation.getRow() == row
&& dropLocation.getColumn() == column) {
isSelected = true;
}
if (isSelected) {
c.setForeground(fg == null ? table.getSelectionForeground() : fg);
c.setBackground(bg == null ? table.getSelectionBackground() : bg);
} else {
Color background = unselectedBackground != null
? unselectedBackground
: table.getBackground();
c.setForeground(unselectedForeground != null
? unselectedForeground
: table.getForeground());
c.setBackground(background);
}
c.setFont(table.getFont());
}
private void updateFieldDecorations (String key, JComponent fieldLabel) {
boolean fieldDirty = unsavedFields.contains(key);
if (fieldLabel != null) {
if (fieldDirty) {
fieldLabel.setFont(fieldLabel.getFont().deriveFont(fieldLabel.getFont().getStyle() | Font.BOLD));
} else {
fieldLabel.setFont(fieldLabel.getFont().deriveFont(fieldLabel.getFont().getStyle() & ~Font.BOLD));
}
}
}
@Override
public void render(JComponent comp, TestStep step, Object value) {
comp.setForeground(getColor(step));
if (step.isCommented() || step.hasBreakPoint()) {
comp.setFont(new Font("Default", Font.BOLD, 11));
}
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) {
// value is column name
String name = (value==null) ? "" : value.toString(); //$NON-NLS-1$
textLine.setText(name);
if (OSPRuntime.isMac()) {
name = TeXParser.removeSubscripting(name);
}
Component c = renderer.getTableCellRendererComponent(table, name, isSelected, hasFocus, row, col);
if (!(c instanceof JComponent)) {
return c;
}
JComponent comp = (JComponent) c;
int sortCol = decorator.getSortedColumn();
Font font = comp.getFont();
if (OSPRuntime.isMac()) {
// textline doesn't work on OSX
comp.setFont((sortCol!=convertColumnIndexToModel(col))?
font.deriveFont(Font.PLAIN) :
font.deriveFont(Font.BOLD));
if (comp instanceof JLabel) {
((JLabel)comp).setHorizontalAlignment(SwingConstants.CENTER);
}
return comp;
}
java.awt.Dimension dim = comp.getPreferredSize();
dim.height += 1;
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);
// set font: bold if sorted column
textLine.setFont((sortCol!=convertColumnIndexToModel(col)) ? font : font.deriveFont(Font.BOLD));
textLine.setColor(comp.getForeground());
textLine.setBackground(comp.getBackground());
panel.setBackground(comp.getBackground());
return panel;
}
@Override
public void installUI(JComponent table) {
super.installUI(table);
table.setForeground(style.getForeground());
table.setBackground(style.getPlainColor());
table.setFont(style.getFont().deriveFont(Font.BOLD));
this.table.setGridColor(style.getShadowColor());
}
@Override
public void uninstallUI(JComponent c) {
c.setFont (null);
c.setBackground (null);
c.setForeground (null);
c.setBorder (null);
c.setCursor(null);
JRadioButton radioButton = (JRadioButton) c;
radioButton.setIcon(null);
radioButton.setSelectedIcon(null);
super.uninstallUI(c);
}
@Override
public void uninstallUI(JComponent c) {
c.setFont (null);
c.setBackground (null);
c.setForeground (null);
c.setBorder (null);
c.setCursor(null);
super.uninstallUI(c);
}
@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);
}
@Override
public void uninstallUI(JComponent c) {
c.setFont (null);
c.setBackground (null);
c.setForeground (null);
c.setBorder (null);
c.setCursor(null);
super.uninstallUI(c);
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean selected, int row, int column) {
JComponent c = (JComponent) super.getTableCellEditorComponent(table, value, selected, row, column);
if (!type.isOptional() && (type.getDefaultValue() == null)) {
c.setFont(c.getFont().deriveFont(Font.BOLD, c.getFont().getSize()));
}
if (selected) {
c.setBackground(SwingTools.LIGHTEST_BLUE);
} else {
c.setBackground(Color.WHITE);
}
return c;
}
public static void main(String[] args) {
MyFrame myFrame = new MyFrame();
myFrame.panel1.setBackground(Color.YELLOW);
myFrame.panel2.setBackground(Color.RED);
JComponent components[] = new JComponent[] {myFrame.textArea, myFrame.button1, myFrame.textField};
for (JComponent component : components) {
component.setFont(new Font("Times New Roman", 1, 20));
//.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
}
public void installUI(JComponent c) {
super.installUI(c);
//Fetch the "no properties" string - it's not going to change
//for the life of the session
// noPropsString = NbBundle.getMessage(MarginViewportUI.class,
// "CTL_NoProperties"); //NOI18N
//Set an appropriate font and color. Only really relevant on OS-X to
//keep the font consistent with other NB fonts
Color fg = UIManager.getColor("controlShadow"); //NOI18N
if (fg == null) {
fg = Color.LIGHT_GRAY;
}
c.setForeground(fg);
Color bg = UIManager.getColor("Tree.background"); //NOI18N
if (bg == null) {
bg = Color.WHITE;
}
c.setBackground(bg);
Font f = UIManager.getFont("Tree.font"); //NOI18N
if (f == null) {
f = UIManager.getFont("controlFont"); //NOI18N
}
if (f != null) {
c.setFont(f);
}
c.addContainerListener(this);
Component[] kids = c.getComponents();
for (int i = 0; i < kids.length; i++) {
//Should almost always be empty anyway, if not only one component,
//but for completeness...
kids[i].addComponentListener(this);
}
}
/**
* To override font, override createToolTip() on component creating tooltip.
* On creation of ToolTip, change to desired font.
* @param g
* @param c
*/
@Override
public void paint(Graphics g, JComponent c) {
String tipText = ((JToolTip)c).getTipText();
tipText = tipText == null ? "" : tipText;
String[] tipLines = tipText.split("\n");
Font font = c.getFont();
g.setFont(font);
FontMetrics metrics = g.getFontMetrics();
int fontHeight = metrics.getHeight();
int height = (fontHeight * tipLines.length) + 2;
int width = this.getWidestStringText(tipLines, metrics) + 10;
int fontSize = font.getSize();
fontSize = fontSize == 0 ? PGTUtil.DEFAULT_FONT_SIZE.intValue() : fontSize;
c.setFont(font.deriveFont(fontSize));
((JToolTip)c).setTipText(tipText);
Dimension size = new Dimension(width, height);
c.setSize(size);
c.getParent().setSize(size);
Insets insets = c.getInsets();
Rectangle paintTextR = new Rectangle(
insets.left,
insets.top,
size.width - (insets.left + insets.right),
size.height - (insets.top + insets.bottom));
((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.black);
g.fillRect(insets.left,
insets.top,
size.width - (insets.left + insets.right),
size.height - (insets.top + insets.bottom));
g.setColor(Color.white);
for (int i = 0 ; i < tipLines.length; i++) {
g.drawString(tipLines[i], paintTextR.x + 5,
paintTextR.y + metrics.getAscent() + (i * (fontHeight)));
}
}
/** 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 public void installUI(JComponent c) {
super.installUI(c);
c.setFont( c.getFont().deriveFont(0) );
}
public static void makeBold(JComponent component) {
component.setFont(component.getFont().deriveFont(Font.BOLD));
}
/**
* Changes the font of a component to BOLD.
* @param component component whose font to be changed to BOLD
* @return the component
*/
public static JComponent changeFontToBold( final JComponent component ) {
component.setFont( component.getFont().deriveFont( Font.BOLD ) );
return component;
}
/**
* Changes the font of a component to ITALIC.
* @param component component whose font to be changed to ITALIC
* @return the component
*/
public static JComponent changeFontToItalic( final JComponent component ) {
component.setFont( component.getFont().deriveFont( Font.ITALIC ) );
return component;
}