下面列出了javax.swing.ButtonModel#isEnabled ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Returns the Icon to use in painting the button.
*
* @param b the button.
*
* @return the icon.
*/
protected Icon getIcon(AbstractButton b) {
Icon icon = b.getIcon();
ButtonModel model = b.getModel();
if (!model.isEnabled()) {
icon = getSynthDisabledIcon(b, icon);
} else if (model.isPressed() && model.isArmed()) {
icon = getPressedIcon(b, getSelectedIcon(b, icon));
} else if (b.isRolloverEnabled() && model.isRollover()) {
icon = getRolloverIcon(b, getSelectedIcon(b, icon));
} else if (model.isSelected()) {
icon = getSelectedIcon(b, icon);
} else {
icon = getEnabledIcon(b, icon);
}
if (icon == null) {
return getDefaultIcon(b);
}
return icon;
}
static void paintIcon(Graphics g, SeaGlassMenuItemLayoutHelper lh,
MenuItemLayoutHelper.LayoutResult lr) {
if (lh.getIcon() != null) {
Icon icon;
JMenuItem mi = lh.getMenuItem();
ButtonModel model = mi.getModel();
if (!model.isEnabled()) {
icon = mi.getDisabledIcon();
} else if (model.isPressed() && model.isArmed()) {
icon = mi.getPressedIcon();
if (icon == null) {
// Use default icon
icon = mi.getIcon();
}
} else {
icon = mi.getIcon();
}
if (icon != null) {
Rectangle iconRect = lr.getIconRect();
SynthIcon.paintIcon(icon, lh.getContext(), g, iconRect.x,
iconRect.y, iconRect.width, iconRect.height);
}
}
}
@Override
protected void paintBackground( Graphics g, Color selectionBackground ) {
ButtonModel model = menuItem.getModel();
if( model.isRollover() && !model.isArmed() && !model.isSelected() &&
model.isEnabled() && ((JMenu)menuItem).isTopLevelMenu() )
{
g.setColor( deriveBackground( hoverBackground ) );
g.fillRect( 0, 0, menuItem.getWidth(), menuItem.getHeight() );
} else
super.paintBackground( g, selectionBackground );
}
public void paint(Graphics g, JComponent c) {
AbstractButton button = (AbstractButton)c;
if (button.getModel().isSelected()) {
Color oldColor = g.getColor();
g.setColor(selectedBackground);
g.fillRoundRect(0, 0, c.getWidth() - 1, c.getHeight() - 1, 5, 5);
g.setColor(selectedBorder);
g.drawRoundRect(0, 0, c.getWidth() - 1, c.getHeight() - 1, 5, 5);
g.setColor(oldColor);
}
// this is a tweak to get the View with the color we expect it to be. We
// change directly the color of the button
if (c.getClientProperty(BasicHTML.propertyKey) != null) {
ButtonModel model = button.getModel();
if (model.isEnabled()) {
if (model.isSelected()) {
button.setForeground(selectedForeground);
} else {
button.setForeground(unselectedForeground);
}
} else {
button.setForeground(unselectedForeground.darker());
}
}
super.paint(g, c);
}
/**
* Create a ButtonState that captures a ButtonModel's current state.
*/
public Boolean(ButtonModel model) {
isArmed = model.isArmed();
isEnabled = model.isEnabled();
isPressed = model.isPressed();
isRollover = model.isRollover();
isSelected = model.isSelected();
}
/**
* Create a ButtonState that captures a ButtonModel's current state.
*/
public Float(ButtonModel model) {
isArmed = model.isArmed() ? 1 : 0;
isEnabled = model.isEnabled() ? 1 : 0;
isPressed = model.isPressed() ? 1 : 0;
isRollover = model.isRollover() ? 1 : 0;
isSelected = model.isSelected() ? 1 : 0;
}
@Override
protected void paintBackground(Graphics g, JMenuItem menuItem, Color bgColor) {
JMenu menu = (JMenu) menuItem;
ButtonModel buttonmodel = menu.getModel();
int w = menu.getWidth();
int h = menu.getHeight();
Color oldColor = g.getColor();
if (!menu.isContentAreaFilled() || !menu.isOpaque()) {
// do nothing
} else {
if (menu.isTopLevelMenu()) {
if (buttonmodel.isSelected()) {
CachedPainter.drawMenuBackground(menuItem, g, 0, 0, w, h);
} else if (buttonmodel.isRollover() && buttonmodel.isEnabled()) {
g.setColor(Colors.MENUBAR_BACKGROUND_HIGHLIGHT);
g.fillRect(0, 0, w, h);
} else {
if (menuItem.getParent() instanceof JMenuBar) {
((MenuBarUI) ((JMenuBar) menuItem.getParent()).getUI()).update(g, menuItem);
}
}
} else {
if (!menuItem.getModel().isSelected()) {
RapidLookTools.drawMenuItemFading(menuItem, g);
} else {
RapidLookTools.drawMenuItemBackground(g, menuItem);
}
}
}
g.setColor(oldColor);
}
@Override
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
g2.setFont(getFont());
FontMetrics fm = g2.getFontMetrics();
ButtonModel mdl = getModel();
String s = getText();
g2.setComposite(AlphaComposite.SrcOver.derive(0.85f));
if (!mdl.isEnabled()) {
g2.setColor(new Color(0x808080));
g2.fillRoundRect(0, 0, getWidth(), getHeight(), 10, 10);
g2.setColor(new Color(0x000000));
} else
if (mdl.isPressed() || mdl.isSelected()) {
if (mdl.isRollover()) {
g2.setColor(new Color(0xE0E0E0));
} else {
g2.setColor(new Color(0xFFFFFF));
}
g2.fillRoundRect(0, 0, getWidth(), getHeight(), 10, 10);
g2.setColor(new Color(0x000000));
} else {
if (mdl.isRollover()) {
g2.setColor(new Color(0x000000));
} else {
g2.setColor(new Color(0x202020));
}
g2.fillRoundRect(0, 0, getWidth(), getHeight(), 10, 10);
g2.setColor(new Color(0xFFFFFFFF));
}
int x = (getWidth() - fm.stringWidth(s)) / 2;
int y = (getHeight() - fm.getHeight()) / 2 + fm.getAscent() + fm.getLeading();
g2.drawString(s, x, y);
}
protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text) {
AbstractButton b = (AbstractButton) c;
ButtonModel model = b.getModel();
FontMetrics fm = g.getFontMetrics();
int mnemIndex = b.getDisplayedMnemonicIndex();
if (model.isEnabled()) {
g.setColor(b.getForeground());
} else {
g.setColor(getDisabledTextColor());
}
BasicGraphicsUtils.drawStringUnderlineCharAt(g, text, mnemIndex, textRect.x, textRect.y + fm.getAscent());
}
/**
* As of Java 2 platform v 1.4 this method should not be used or overriden.
* Use the paintText method which takes the AbstractButton argument.
*
* @param g the g
* @param c the c
* @param textRect the text rect
* @param text the text
*/
protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text) {
AbstractButton b = (AbstractButton) c;
ButtonModel model = b.getModel();
FontMetrics fm = //SwingUtilities2
MySwingUtilities2.getFontMetrics(c, g);
int mnemonicIndex = b.getDisplayedMnemonicIndex();
/* Draw the Text */
if(model.isEnabled())
{
//=================== modified by jb2011 START
if(model.isSelected())//选中时使用不同的颜色
g.setColor(UIManager.getColor(getPropertyPrefix()+"focus"));
else
/*** paint the text normally */
g.setColor(b.getForeground());
//=================== modified by jb2011 END
// SwingUtilities2 *不要直接调用该类(因为它是sun未公开api,1.5里与1.6及以后版它放在不同的包里,某天它会消失也说不好)
MySwingUtilities2.drawStringUnderlineCharAt(c, g,text, mnemonicIndex,
textRect.x + getTextShiftOffset(),
textRect.y + fm.getAscent() + getTextShiftOffset());
}
else
{
/*** paint the text disabled ***/
g.setColor(b.getBackground().brighter());
//SwingUtilities2 *不要直接调用该类(因为它是sun未公开api,1.5里与1.6及以后版它放在不同的包里,某天它会消失也说不好)
MySwingUtilities2.drawStringUnderlineCharAt(c, g,text, mnemonicIndex,
textRect.x, textRect.y + fm.getAscent());
g.setColor(b.getBackground().darker());
//SwingUtilities2 *不要直接调用该类(因为它是sun未公开api,1.5里与1.6及以后版它放在不同的包里,某天它会消失也说不好)
MySwingUtilities2.drawStringUnderlineCharAt(c, g,text, mnemonicIndex,
textRect.x - 1, textRect.y + fm.getAscent() - 1);
}
}
public void paintIcon(Component c, Graphics g, int x, int y)
{
JCheckBox cb = (JCheckBox) c;
ButtonModel model = cb.getModel();
//选中时
if(model.isSelected())
{
//处于禁用状态
if(!model.isEnabled())
g.drawImage(__IconFactory__.getInstance().getCheckBoxButtonIcon_disable().getImage(), x, y, null);
else
{
//处于被按住状态
if(model.isPressed())
g.drawImage(__IconFactory__.getInstance().getCheckBoxButtonIcon_pressed().getImage(), x, y, null);
else
g.drawImage(__IconFactory__.getInstance().getCheckBoxButtonIcon_normal().getImage(), x, y, null);
}
}
//未选中时
else
{
//处于禁用状态
if(!model.isEnabled())
g.drawImage(__IconFactory__.getInstance().getCheckBoxButtonIcon_unchecked_disable().getImage(), x, y, null);
else
{
//处于被按住状态
if(model.isPressed())
g.drawImage(__IconFactory__.getInstance().getCheckBoxButtonIcon_unchecked_pressed().getImage(), x, y, null);
else
g.drawImage(__IconFactory__.getInstance().getCheckBoxButtonIcon_unchecked_normal().getImage(), x, y, null);
}
}
}
public void paintIcon(Component c, Graphics g, int x, int y) {
AbstractButton b = (AbstractButton) c;
ButtonModel model = b.getModel();
//选中时
if(model.isSelected())
{
//处于禁用状态
if(!model.isEnabled())
g.drawImage(__IconFactory__.getInstance().getRadioButtonIcon_disable().getImage(), x, y, null);
else
{
//处于被按住状态
if(model.isPressed())
g.drawImage(__IconFactory__.getInstance().getRadioButtonIcon_pressed().getImage(), x, y, null);
else
g.drawImage(__IconFactory__.getInstance().getRadioButtonIcon_normal().getImage(), x, y, null);
}
}
//未选中时
else
{
//处于禁用状态
if(!model.isEnabled())
g.drawImage(__IconFactory__.getInstance().getRadioButtonIcon_unchecked_disable().getImage(), x, y, null);
else
{
//处于被按住状态
if(model.isPressed())
g.drawImage(__IconFactory__.getInstance().getRadioButtonIcon_unchecked_pressed().getImage(), x, y, null);
else
g.drawImage(__IconFactory__.getInstance().getRadioButtonIcon_unchecked_normal().getImage(), x, y, null);
}
}
}
protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text) {
AbstractButton b = (AbstractButton) c;
ButtonModel model = b.getModel();
FontMetrics fm = g.getFontMetrics();
int mnemIndex = b.getDisplayedMnemonicIndex();
if (model.isEnabled()) {
g.setColor(b.getForeground());
} else {
g.setColor(getDisabledTextColor());
}
BasicGraphicsUtils.drawStringUnderlineCharAt(g, text, mnemIndex, textRect.x, textRect.y + fm.getAscent());
}
@Messages("FileChooserAccessory.noSuitableVariable=<no suitable variable>")
private void update(List<File> files) {
StringBuilder absolute = new StringBuilder();
StringBuilder relative = new StringBuilder();
StringBuilder variable = new StringBuilder();
boolean isRelative = true;
for (File file : files) {
String varPath = getVariablesModel().getRelativePath(file, true);
if (absolute.length() != 0) {
absolute.append(", ");
relative.append(", ");
if (varPath != null) {
variable.append(", ");
}
}
absolute.append(file.getAbsolutePath());
String s = PropertyUtils.relativizeFile(baseFolder, file);
if (s == null) {
isRelative = false;
}
relative.append(s);
if (varPath != null) {
variable.append(varPath);
}
}
rbRelative.setEnabled(isRelative && rbRelative.isEnabled());
relativePath.setText(relative.toString());
relativePath.setCaretPosition(0);
relativePath.setToolTipText(relative.toString());
if (!isRelative) {
relativePath.setText("");
relativePath.setToolTipText("");
}
absolutePath.setText(absolute.toString());
absolutePath.setCaretPosition(0);
absolutePath.setToolTipText(absolute.toString());
if (variable.length() == 0) {
variable.append(FileChooserAccessory_noSuitableVariable());
}
variablePath.setText(variable.toString());
variablePath.setCaretPosition(0);
variablePath.setToolTipText(variable.toString());
//when deciding on predefined file, we can assume certain options to be preferable.
ButtonModel selection = buttonGroup1.getSelection();
if(selection == null || !selection.isEnabled() || !userSelection){
if (areCollocated(baseFolder, files)) {
rbRelative.setSelected(true);
} else if (areVarRelated(files)) {
rbVariable.setSelected(true);
} else if (copyAllowed) {
rbCopy.setSelected(true);
} else {
rbAbsolute.setSelected(true);
}
if (selection != buttonGroup1.getSelection()){
userSelection = false;
}
}
}
private static String toString(ButtonModel model) {
return "isArmed = " + model.isArmed()
+ ", isEnabled = " + model.isEnabled()
+ ", isPressed = " + model.isPressed()
+ ", isSelected = " + model.isSelected();
}
protected void paintText(
Graphics g,
AbstractButton b,
Rectangle textRect,
String text) {
ButtonModel model = b.getModel();
FontMetrics fm = g.getFontMetrics();
int mnemonicIndex = b.getDisplayedMnemonicIndex();
Color oldColor = g.getColor();
/* Draw the Text */
if (model.isEnabled()) {
/** * paint the text normally */
if (model.isSelected()) {
g.setColor(selectedForeground);
} else {
g.setColor(unselectedForeground);
}
} else {
g.setColor(unselectedForeground.darker());
}
//
BasicGraphicsUtils.drawStringUnderlineCharAt(
g,
text,
mnemonicIndex,
textRect.x + getTextShiftOffset(),
textRect.y + fm.getAscent() + getTextShiftOffset());
//
// } else {
// g.setColor(b.getParent().getBackground().brighter());
// BasicGraphicsUtils.drawStringUnderlineCharAt(
// g,
// text,
// mnemonicIndex,
// textRect.x,
// textRect.y + fm.getAscent());
// g.setColor(b.getParent().getBackground().darker());
// BasicGraphicsUtils.drawStringUnderlineCharAt(
// g,
// text,
// mnemonicIndex,
// textRect.x - 1,
// textRect.y + fm.getAscent() - 1);
// }
g.setColor(oldColor);
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
JMenuItem b = (JMenuItem) c;
ButtonModel bm = b.getModel();
g.translate(x, y);
boolean isSelected = bm.isSelected();
boolean isEnabled = bm.isEnabled();
boolean isPressed = bm.isPressed();
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
// drawing background section
if (!isEnabled) {
g2.setColor(Colors.RADIOBUTTON_BORDER_DISABLED);
} else {
if (isPressed) {
g2.setColor(Colors.RADIOBUTTON_BORDER_FOCUS);
} else {
g2.setColor(Colors.RADIOBUTTON_BORDER);
}
}
g2.setStroke(RADIO_STROKE);
Shape circle = new Ellipse2D.Double(0, 0, 9, 9);
g2.draw(circle);
// drawing sphere
if (isSelected) {
if (isEnabled) {
g2.setColor(Colors.RADIOBUTTON_CHECKED);
} else {
g2.setColor(Colors.RADIOBUTTON_CHECKED_DISABLED);
}
circle = new Ellipse2D.Double(3, 3, 4, 4);
g2.fill(circle);
}
g.translate(-x, -y);
}
protected void paintText(Graphics g, JComponent com, Rectangle rect,
String s) {
JLinkButton bn = (JLinkButton) com;
ButtonModel bnModel = bn.getModel();
// Color color = bn.getForeground();
// Object obj = null;
if (bnModel.isEnabled()) {
if (bnModel.isPressed()) {
bn.setForeground(bn.getActiveLinkColor());
} else if (bn.isLinkVisited()) {
bn.setForeground(bn.getVisitedLinkColor());
} else {
bn.setForeground(bn.getLinkColor());
}
} else {
if (bn.getDisabledLinkColor() != null) {
bn.setForeground(bn.getDisabledLinkColor());
}
}
super.paintText(g, com, rect, s);
int behaviour = bn.getLinkBehavior();
boolean drawLine = false;
if (behaviour == JLinkButton.HOVER_UNDERLINE) {
if (bnModel.isRollover()) {
drawLine = true;
}
} else if (behaviour == JLinkButton.ALWAYS_UNDERLINE || behaviour == JLinkButton.SYSTEM_DEFAULT) {
drawLine = true;
}
if (!drawLine) {
return;
}
FontMetrics fm = g.getFontMetrics();
int x = rect.x + getTextShiftOffset();
int y = (rect.y + fm.getAscent() + fm.getDescent() + getTextShiftOffset()) - 1;
if (bnModel.isEnabled()) {
g.setColor(bn.getForeground());
g.drawLine(x, y, (x + rect.width) - 1, y);
} else {
g.setColor(bn.getBackground().brighter());
g.drawLine(x, y, (x + rect.width) - 1, y);
}
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
JRadioButton radioButton = (JRadioButton) c;
ButtonModel bm = radioButton.getModel();
int w = c.getWidth();
int h = c.getHeight();
if (h < 0 || w < 0) {
return;
}
g.translate(x, y);
boolean isSelected = bm.isSelected();
boolean isEnabled = bm.isEnabled();
boolean isPressed = bm.isPressed();
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
// drawing background section
if (!isEnabled) {
g2.setColor(Colors.RADIOBUTTON_BACKGROUND_DISABLED);
} else {
g2.setColor(Colors.RADIOBUTTON_BACKGROUND);
}
Shape circle = new Ellipse2D.Double(2, 2, 12, 12);
g2.fill(circle);
if (!isEnabled) {
g2.setColor(Colors.RADIOBUTTON_BORDER_DISABLED);
} else {
if (isPressed) {
g2.setColor(Colors.RADIOBUTTON_BORDER_FOCUS);
} else {
g2.setColor(Colors.RADIOBUTTON_BORDER);
}
}
g2.setStroke(RADIO_STROKE);
circle = new Ellipse2D.Double(1, 1, 14, 14);
g2.draw(circle);
// drawing sphere
if (isSelected) {
if (isEnabled) {
g2.setColor(Colors.RADIOBUTTON_CHECKED);
} else {
g2.setColor(Colors.RADIOBUTTON_CHECKED_DISABLED);
}
circle = new Ellipse2D.Double(4, 4, 9, 9);
g2.fill(circle);
}
g.translate(-x, -y);
}
void paintInternally(Component c, Graphics g, int x, int y, ButtonModel bm) {
boolean isSelected = bm.isSelected();
boolean isEnabled = bm.isEnabled();
boolean isPressed = bm.isPressed();
int w = c.getWidth();
int h = c.getHeight();
if (h < 0 || w < 0) {
return;
}
g.translate(x, y);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// drawing outer
if (!isEnabled) {
g2.setColor(Colors.CHECKBOX_BORDER_DISABLED);
} else {
if (isPressed) {
g2.setColor(Colors.CHECKBOX_BORDER_FOCUS);
} else {
g2.setColor(Colors.CHECKBOX_BORDER);
}
}
g2.drawRect(1, 1, 13, 13);
// drawing background section
if (!isEnabled) {
g.setColor(Colors.CHECKBOX_BACKGROUND_DISABLED);
} else {
g2.setColor(Colors.CHECKBOX_BACKGROUND);
}
g2.fillRect(2, 2, 12, 12);
// draw check mark
if (isSelected) {
g2.translate(2, 3);
g2.setStroke(CHECKBOX_STROKE);
if (isEnabled) {
g2.setColor(Colors.CHECKBOX_CHECKED);
} else {
g2.setColor(Colors.CHECKBOX_CHECKED_DISABLED);
}
g2.drawLine(2, 6, 5, 8);
g2.drawLine(5, 8, 9, 1);
g2.translate(-2, -3);
}
g.translate(-x, -y);
}