?? captionbuttonui.java
字號:
/*
* CaptionButtonUI.java
*
* Created on June 9, 2007, 8:56 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package dyno.swing.beans;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.ItemEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.plaf.ComponentUI;
/**
* 這個類時CaptionButton組建的UI類。
* 該UI類是個有狀態UI類。
*
* @author William Chen
*/
public class CaptionButtonUI extends ComponentUI implements MouseMotionListener,
MouseListener, FocusListener {
//缺省前景色和缺省字體
private static final Color DEFAULT_FOREGROUND = new Color(33, 93, 198);
//定義一些缺省屬性的值,比如顏色、字體、尺寸、間隙、缺省圖標、虛線框的stroke等等。
private static final Color LIGHTER = new Color(255, 255, 255);
private static final Color DARKER = new Color(198, 211, 247);
private static final int TEXT_LEADING_GAP = 14;
private static final int IMAGE_TAILING_GAP = 12;
private static final Color HOVERED_COLOR = new Color(66, 142, 255);
private static Stroke DASHED_STROKE = new BasicStroke(1,
BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[] { 1 },
0);
private static Icon iconExpanded;
private static Icon iconFoldered;
private static Icon hoveredExpanded;
private static Icon hoveredFoldered;
static {
//初始化
iconExpanded = new ImageIcon(CaptionButtonUI.class.getResource(
"resources/expanded.png"));
iconFoldered = new ImageIcon(CaptionButtonUI.class.getResource(
"resources/foldered.png"));
hoveredExpanded = new ImageIcon(CaptionButtonUI.class.getResource(
"resources/hovered_expanded.png"));
hoveredFoldered = new ImageIcon(CaptionButtonUI.class.getResource(
"resources/hovered_foldered.png"));
}
//是否畫虛線框
private boolean armed;
//文字左邊間隙
private int textLeadingGap = TEXT_LEADING_GAP;
//圖像郵編間隙
private int imageTailingGap = IMAGE_TAILING_GAP;
//漸變色起始色
private Color lightColor = LIGHTER;
//漸變色結束色
private Color darkColor = DARKER;
//當前鼠標是否浮動在上面
private boolean hovered;
//鼠標浮動在標題上方時標題的顏色
private Color hoveredColor = HOVERED_COLOR;
//該UI實例對應的CaptionButton
protected CaptionButton button;
/** Creates a new instance of CaptionButtonUI */
public CaptionButtonUI() {
}
public static ComponentUI createUI(JComponent c) {
return new CaptionButtonUI();
}
//安裝CaptionButton的LAF
public void installUI(JComponent c) {
//設置缺省屬性
button = (CaptionButton) c;
button.setForeground(DEFAULT_FOREGROUND);
button.setFocusable(true);
//添加事件處理器
button.addMouseListener(this);
button.addMouseMotionListener(this);
button.addFocusListener(this);
}
//卸載UI
public void uninstallUI(JComponent c) {
//卸載事件處理器
button.removeMouseListener(this);
button.removeMouseMotionListener(this);
button.removeFocusListener(this);
}
//畫CaptionButton的背景。繼承CaptionButtonUI的子類可以覆蓋該方法自定義背景
protected void paintBackground(Graphics g) {
int w = button.getWidth();
int h = button.getHeight();
Graphics2D g2d = (Graphics2D) g;
GradientPaint gp = new GradientPaint(1, 1, lightColor, w - 2, 1,
darkColor);
g2d.setPaint(gp);
g2d.fillRect(1, 1, w - 2, h - 1);
gp = new GradientPaint(2, 0, lightColor, w - 4, 0, darkColor);
g2d.setPaint(gp);
g2d.fillRect(2, 0, w - 4, 1);
g2d.setColor(lightColor);
g2d.drawLine(0, 2, 0, h - 1);
g2d.setColor(darkColor);
g2d.drawLine(w - 1, 2, w - 1, h - 1);
}
public void paint(Graphics g, JComponent c) {
paintBackground(g);
paintCaptionText(g);
paintIcon(g);
if (armed) {
paintArmed(g);
}
}
//畫虛線框,繼承CaptionButtonUI的子類可以覆蓋該方法自定義焦點獲得外觀
protected void paintArmed(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.black);
g2d.setStroke(DASHED_STROKE);
g2d.drawRoundRect(1, 1, button.getWidth() - 3, button.getHeight() - 3,
2, 2);
}
//畫標題欄展開、關閉圖標,繼承CaptionButtonUI的子類可以覆蓋該方法自定義展開、關閉圖標
protected void paintIcon(Graphics g) {
Icon icon = null;
if (hovered) {
icon = button.isExpanded() ? hoveredExpanded : hoveredFoldered;
} else {
icon = button.isExpanded() ? iconExpanded : iconFoldered;
}
int x = button.getWidth() - imageTailingGap - icon.getIconWidth();
int y = (button.getHeight() - icon.getIconHeight()) / 2;
icon.paintIcon(button, g, x, y);
}
//畫標題欄文字,繼承CaptionButtonUI的子類可以覆蓋該方法自定義文字的外觀
protected void paintCaptionText(Graphics g) {
FontMetrics fm = g.getFontMetrics();
if (button.getText() != null) {
Color foreground = button.getForeground();
Color color = hovered ? hoveredColor : foreground;
g.setColor(color);
int y = ((button.getHeight() - fm.getHeight()) / 2) +
fm.getAscent();
g.drawString(button.getText(), textLeadingGap, y);
}
}
//處理按下鼠標事件
public void mousePressed(MouseEvent e) {
//改換外觀
setArmed(true);
//折疊或者展開
button.setExpanded(!button.isExpanded());
//觸發選擇事件
ItemEvent evt = new ItemEvent(button, button.isExpanded() ? 0 : 1,
button.getText(),
button.isExpanded() ? ItemEvent.SELECTED : ItemEvent.DESELECTED);
button.fireItemStateChanged(evt);
//獲得焦點
button.requestFocus();
}
//鼠標進入事件、浮動事件
public void mouseEntered(MouseEvent e) {
//浮動
setHovered(true);
//改鼠標外觀
button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}
//鼠標退出事件
public void mouseExited(MouseEvent e) {
//浮動消失
setHovered(false);
}
void setHovered(boolean b) {
hovered = b;
button.repaint();
}
//焦點消失事件
public void focusLost(FocusEvent e) {
setArmed(false);
}
void setArmed(boolean b) {
armed = b;
button.repaint();
}
public void mouseDragged(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void focusGained(FocusEvent e) {
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -