下面列出了javax.swing.ButtonModel#isSelected ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected Color getBackground( JComponent c ) {
ButtonModel model = ((AbstractButton)c).getModel();
if( model.isSelected() ) {
// in toolbar use same colors for disabled and enabled because
// we assume that toolbar icon is shown disabled
boolean toolBarButton = isToolBarButton( c );
return buttonStateColor( c,
toolBarButton ? toolbarSelectedBackground : selectedBackground,
toolBarButton ? toolbarSelectedBackground : disabledSelectedBackground,
null, null,
toolBarButton ? toolbarPressedBackground : pressedBackground );
}
return super.getBackground( c );
}
public Image getPreImg(Component c, ButtonModel model)
{
JMenu menu = (JMenu) c;
if (menu.getItemCount() > 0)
{
if (model.isSelected())
{
return getRollverImg();
}
else
{
return getNormalImg();
}
}
return null;
}
@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 );
}
@Override
protected void paintBackground(Graphics g, JMenuItem menuItem, Color bgColor) {
ButtonModel model = menuItem.getModel();
Color oldColor = g.getColor();
int menuWidth = menuItem.getWidth();
int menuHeight = menuItem.getHeight();
Color bgc = (Color) menuItem.getClientProperty("bgColor");
if (bgc != null) {
g.setColor(bgc);
} else {
g.setColor(colorBg);
}
g.fillRect(0, 0, menuWidth, menuHeight);
if (model.isArmed()
|| (menuItem instanceof JMenu && model.isSelected())) {
paintButtonPressed(g, menuItem);
} else {
// if (menuItem.getIcon() != null) {
// int gap = menuItem.getIcon().getIconWidth() + 2;
// g.setColor(this.darkColor);
// g.drawLine(gap, 0, gap, menuItem.getHeight());
// g.setColor(this.lightColor);
// g.drawLine(gap + 1, 0, gap + 1, menuItem.getHeight());
// }
}
if (menuItem instanceof JCheckBoxMenuItem) {
if (((JCheckBoxMenuItem) menuItem).isSelected()) {
// chkIcon.paintIcon(menuItem, g, 5, 5);
}
}
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);
}
/** Stores all models created in the StoreGroup into given
* EditableProperties.
* @param editableProperties The properties where to store the
* values.
*/
public void store( EditableProperties editableProperties ) {
for (Map.Entry<String,Object[]> entry : models.entrySet()) {
String key = entry.getKey();
Object[] params = entry.getValue();
if ( params[0] instanceof ButtonModel ) {
ButtonModel model = (ButtonModel)params[0];
boolean value = model.isSelected();
if ( params[2] == Boolean.TRUE ) {
value = !value;
}
editableProperties.setProperty( key, encodeBoolean( value, (Integer)params[1] ) );
}
else if ( params[0] instanceof Document && modifiedDocuments.contains(params[0])) {
Document doc = (Document)params[0];
String txt;
try {
txt = doc.getText(0, doc.getLength());
} catch (BadLocationException e) {
txt = ""; // NOI18N
}
editableProperties.setProperty( key, txt );
}
}
}
private static Product[] getSelectedProducts(Map<Product, ButtonModel> buttonMap) {
List<Product> selectedProducts = new ArrayList<Product>(buttonMap.size());
for (Map.Entry<Product, ButtonModel> entry : buttonMap.entrySet()) {
Product product = entry.getKey();
ButtonModel buttonModel = entry.getValue();
if (buttonModel.isSelected()) {
selectedProducts.add(product);
}
}
return selectedProducts.toArray(new Product[selectedProducts.size()]);
}
/**
* 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);
}
}
/**
* Draws the background of the menu item.
*
* @param g the paint graphics
* @param menuItem menu item to be painted
* @param bgColor selection background color
* @since 1.4
*/
protected void paintBackground(Graphics g, JMenuItem menuItem, Color bgColor)
{
ButtonModel model = menuItem.getModel();
Color oldColor = g.getColor();
int menuWidth = menuItem.getWidth();
int menuHeight = menuItem.getHeight();
if(menuItem.isOpaque())
{
if (model.isArmed()|| (menuItem instanceof JMenu && model.isSelected()))
{
g.setColor(bgColor);
g.fillRect(0,0, menuWidth, menuHeight);
}
else
{
g.setColor(menuItem.getBackground());
g.fillRect(0,0, menuWidth, menuHeight);
}
g.setColor(oldColor);
}
else if (model.isArmed() || (menuItem instanceof JMenu &&
model.isSelected()))
{
// g.setColor(bgColor);
// g.fillRect(0,0, menuWidth, menuHeight);
// g.setColor(oldColor);
//由jb2011改用NinePatch图来填充
__Icon9Factory__.getInstance().getBgIcon_ItemSelected()
.draw((Graphics2D)g, 0, 0, menuWidth, menuHeight);
}
}
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);
}
}
}
public void paintIcon(Component c, Graphics g, int x, int y)
{
AbstractButton cb = (AbstractButton) c;
ButtonModel model = cb.getModel();
boolean isPressed = (model.isArmed() && model.isPressed());
boolean isRollver = (model.isRollover() && cb.isRolloverEnabled());
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
drawOval(g2d, x, y, (isRollver || isPressed));
if(model.isSelected())
{
fillOval(g2d, x, y);
}
else if(isRollver && isPressed)
{
drawOvalShadow(g2d, x, y);
}
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
}
/**
* DOCUMENT ME!
*
* @param b DOCUMENT ME!
* @param defaultIcon DOCUMENT ME!
*
* @return DOCUMENT ME!
*/
private Icon getSynthDisabledIcon(AbstractButton b, Icon defaultIcon) {
ButtonModel model = b.getModel();
Icon icon;
if (model.isSelected()) {
icon = getIcon(b, b.getDisabledSelectedIcon(), defaultIcon, SynthConstants.DISABLED | SynthConstants.SELECTED);
} else {
icon = getIcon(b, b.getDisabledIcon(), defaultIcon, SynthConstants.DISABLED);
}
return icon;
}
@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 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);
}
private static String toString(ButtonModel model) {
return "isArmed = " + model.isArmed()
+ ", isEnabled = " + model.isEnabled()
+ ", isPressed = " + model.isPressed()
+ ", isSelected = " + model.isSelected();
}
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);
}
public void paintIcon(Component c, Graphics g, int x, int y)
{
AbstractButton cb = (AbstractButton) c;
ButtonModel model = cb.getModel();
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
y = y + 2;
drawOval(g2d, x, y, model.isArmed());
if(model.isSelected())
{
fillOval(g2d, x, y, model.isArmed());
}
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
}
/**
* Method which renders the text of the current menu item.
* <p>
* @param g Graphics context
* @param menuItem Current menu item to render
* @param textRect Bounding rectangle to render the text.
* @param text String to render
* @since 1.4
*/
protected void paintText(Graphics g, JMenuItem menuItem,
Rectangle textRect, String text)
{
//================= commet by Jack Jiang START
// if (WindowsMenuItemUI.isVistaPainting()) {
// WindowsMenuItemUI.paintText(accessor, g, menuItem, textRect, text);
// return;
// }
//================= commet by Jack Jiang END
JMenu menu = (JMenu)menuItem;
ButtonModel model = menuItem.getModel();
Color oldColor = g.getColor();
// Only paint rollover if no other menu on menubar is selected
boolean paintRollover = model.isRollover();
if (paintRollover && menu.isTopLevelMenu()) {
MenuElement[] menus = ((JMenuBar)menu.getParent()).getSubElements();
for (int i = 0; i < menus.length; i++) {
if (((JMenuItem)menus[i]).isSelected()) {
paintRollover = false;
break;
}
}
}
if ((model.isSelected() &&
(
// WindowsLookAndFeel.isClassicWindows() ||
!menu.isTopLevelMenu()))
||
(
// BEXPStyle.getXP() != null &&
(paintRollover ||model.isArmed() ||model.isSelected())
)
)
{
g.setColor(selectionForeground); // Uses protected field.
}
//================= add by Jack Jiang START
//特殊处理顶级菜单项(就是直接放在JMenuBar上的那一层),使之在被选中或rover等状态时保持黑色(或其它颜色)
//,目的是为了配合整个菜单项的L&F效果,并没有过多的用途,此颜色可提取作为UIManager的自定义属性哦
if(menu.isTopLevelMenu())
g.setColor(new Color(35,35,35));//用MaxOS X的经典黑
//================= add by Jack Jiang END
// WindowsGraphicsUtils.paintText(g, menuItem, textRect, text, 0);
WinUtils.paintText(g, menuItem, textRect, text, 0);
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);
}
/**
* Returns the current state of the passed in <code>AbstractButton</code>.
*
* @param c the button component.
*
* @return the button's state.
*/
private int getComponentState(JComponent c) {
int state = ENABLED;
if (!c.isEnabled()) {
state = DISABLED;
}
if (SeaGlassLookAndFeel.selectedUI == this) {
return SeaGlassLookAndFeel.selectedUIState | SynthConstants.ENABLED;
}
AbstractButton button = (AbstractButton) c;
ButtonModel model = button.getModel();
if (model.isPressed()) {
if (model.isArmed()) {
state = PRESSED;
} else {
state = MOUSE_OVER;
}
}
if (model.isRollover()) {
state |= MOUSE_OVER;
}
if (model.isSelected()) {
state |= SELECTED;
}
if (c.isFocusOwner() && button.isFocusPainted()) {
state |= FOCUSED;
}
if ((c instanceof JButton) && ((JButton) c).isDefaultButton()) {
state |= DEFAULT;
}
return state;
}