📄 xpcomboboxbutton.java
字号:
// Beta
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* XP Look and Feel *
* *
* (C) Copyright 2002, by Stefan Krause *
* *
* *
* This library is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License along *
* with this program; if not, write to the Free Software Foundation, Inc., *
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
package com.stefankrause.xplookandfeel;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.TexturePaint;
import java.awt.image.BufferedImage;
import javax.swing.ButtonModel;
import javax.swing.CellRendererPane;
import javax.swing.DefaultButtonModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
import javax.swing.UIManager;
import com.stefankrause.xplookandfeel.skin.Skin;
import com.stefankrause.xplookandfeel.skin.SkinSimpleButtonIndexModel;
public class XPComboBoxButton extends JButton {
protected JComboBox comboBox;
protected JList listBox;
protected CellRendererPane rendererPane;
protected Icon comboIcon;
protected boolean iconOnly = false;
BufferedImage focusImg;
public final JComboBox getComboBox() {
return comboBox;
}
public final void setComboBox(JComboBox cb) {
comboBox = cb;
}
public final Icon getComboIcon() {
return comboIcon;
}
public final void setComboIcon(Icon i) {
comboIcon = i;
}
public final boolean isIconOnly() {
return iconOnly;
}
public final void setIconOnly(boolean isIconOnly) {
iconOnly = isIconOnly;
}
XPComboBoxButton() {
super("");
DefaultButtonModel model = new DefaultButtonModel() {
public void setArmed(boolean armed) {
super.setArmed(isPressed() ? true : armed);
}
};
setModel(model);
// Set the background and foreground to the combobox colors.
setBackground(UIManager.getColor("ComboBox.background"));
setForeground(UIManager.getColor("ComboBox.foreground"));
ImageIcon icon = XPLookAndFeel.loadIcon("XPComboBoxFocus.res",this);
focusImg = new BufferedImage(2, 2, BufferedImage.TYPE_INT_RGB);
Graphics g3 = focusImg.getGraphics();
icon.paintIcon(this, g3, 0, 0);
}
public XPComboBoxButton(JComboBox cb, Icon i, CellRendererPane pane, JList list) {
this();
comboBox = cb;
comboIcon = i;
rendererPane = pane;
listBox = list;
setEnabled(comboBox.isEnabled());
}
public XPComboBoxButton(JComboBox cb, Icon i, boolean onlyIcon, CellRendererPane pane, JList list) {
this(cb, i, pane, list);
iconOnly = onlyIcon;
}
Skin skinCombo;;
SkinSimpleButtonIndexModel indexModel=new SkinSimpleButtonIndexModel();
Skin skinArrow;
Skin skinButton;
/**
* Mostly taken from the swing sources
* @see javax.swing.JComponent#paintComponent(Graphics)
*/
public void paintComponent(Graphics g) {
boolean leftToRight = getComponentOrientation().isLeftToRight();
int index=indexModel.getIndexForState(model.isEnabled(),model.isRollover(),model.isArmed() && model.isPressed() | model.isSelected());
// Paint the button as usual
if (iconOnly) {
// Branch for non editable Combo Boxes
ButtonModel model = getModel();
boolean selected = model.isArmed() && model.isPressed() | model.isSelected();
getSkinButton().draw(g, index, getWidth(), getHeight());
int amnt = getWidth() - getSkinArrow().getHsize();
int middle = (getHeight() - getSkinArrow().getVsize()) / 2;
getSkinArrow().draw(g, index, getWidth() - getSkinArrow().getHsize(), middle, getSkinArrow().getHsize(), getSkinArrow().getVsize());
} else {
// Branch for editable Combo Boxes
ButtonModel model = getModel();
boolean selected = model.isArmed() && model.isPressed() | model.isSelected();
getSkinCombo().draw(g, index, getWidth(), getHeight());
int middle = (getHeight() - getSkinArrow().getVsize()) / 2;
getSkinArrow().draw(g, index, getWidth() - getSkinArrow().getHsize(), middle, getSkinArrow().getHsize(), getSkinArrow().getVsize());
}
Insets insets = new Insets(3, 4, 4, 1);
int width = getWidth() - (insets.left + insets.right);
int widthFocus = width; //- (skin.getHsize()-skin.getOffset());
int height = getHeight() - (insets.top + insets.bottom);
if (height <= 0 || width <= 0) {
return;
}
int left = insets.left;
int top = insets.top;
int right = left + (width - 1);
int bottom = top + (height - 1);
int iconWidth = XPComboBoxUI.comboBoxButtonSize;
int iconLeft = (leftToRight) ? right : left;
// Let the renderer paint
Component c = null;
boolean mustResetOpaque = false;
boolean savedOpaque = false;
boolean paintFocus = false;
if (!iconOnly && comboBox != null) {
ListCellRenderer renderer = comboBox.getRenderer();
boolean renderPressed = getModel().isPressed();
c = renderer.getListCellRendererComponent(listBox, comboBox.getSelectedItem(), -1, renderPressed, false);
c.setFont(rendererPane.getFont());
if (model.isArmed() && model.isPressed()) {
if (isOpaque()) {
c.setBackground(UIManager.getColor("Button.select"));
}
c.setForeground(comboBox.getForeground());
} else if (!comboBox.isEnabled()) {
if (isOpaque()) {
c.setBackground(UIManager.getColor("ComboBox.disabledBackground"));
}
c.setForeground(UIManager.getColor("ComboBox.disabledForeground"));
} else if (comboBox.hasFocus() && !comboBox.isPopupVisible()) {
c.setForeground(UIManager.getColor("ComboBox.disabledBackground"));
c.setBackground(UIManager.getColor("ComboBox.focusBackground"));
if (c instanceof JComponent) {
mustResetOpaque = true;
JComponent jc = (JComponent) c;
savedOpaque = jc.isOpaque();
jc.setOpaque(true);
paintFocus = true;
}
} else {
c.setForeground(comboBox.getForeground());
c.setBackground(comboBox.getBackground());
}
if (!mustResetOpaque && c instanceof JComponent) {
mustResetOpaque = true;
JComponent jc = (JComponent) c;
savedOpaque = jc.isOpaque();
jc.setOpaque(false);
}
int cWidth = width - (insets.right + iconWidth);
// Fix for 4238829: should lay out the JPanel.
boolean shouldValidate = false;
if (c instanceof JPanel) {
shouldValidate = true;
}
if (leftToRight) {
rendererPane.paintComponent(g, c, this, left, top, cWidth, height, shouldValidate);
} else {
rendererPane.paintComponent(g, c, this, left + iconWidth, top, cWidth, height, shouldValidate);
}
if (paintFocus) {
g.setColor(Color.black);
Graphics2D g2d = (Graphics2D) g;
// BasicStroke focusStroke=new BasicStroke(1.0f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL,1.0f, new float[] {1.0f, 1.0f}, 1.0f);
Rectangle r = new Rectangle(left, top, 2, 2);
TexturePaint tp = new TexturePaint(focusImg, r);
g2d.setPaint(tp);
g2d.draw(new Rectangle(left,top,cWidth, height));
}
}
if (mustResetOpaque) {
JComponent jc = (JComponent) c;
jc.setOpaque(savedOpaque);
}
}
public Skin getSkinCombo()
{
if (skinCombo == null) {
skinCombo = new Skin("XPComboBox.res", 4, 3, 3, 18, 3);
}
return skinCombo;
}
public Skin getSkinArrow()
{
if (skinArrow == null) {
skinArrow = new Skin("XPComboBoxArrow.res", 4, 0);
}
return skinArrow;
}
public Skin getSkinButton()
{
if (skinButton== null) {
skinButton = new Skin("XPComboBoxButton.res", 4, 3);
}
return skinButton;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -