实现类似XP风格的快速按钮
2008-01-05 09:52:10 来源:WEB开发网核心提示:实现功能: 类似于Windows xp中的快速按钮的风格, 答应指定按钮的状态是自弹起(就是鼠标键按下时按钮产生被按下的效果, 鼠标键抬起的时候按钮自动回复原状)着是开关按钮(点击一次, 按钮被按下, 再点击一次, 按钮被弹起), 并能够设定一组(多个)或多组互斥按钮(即同组中有且只有一个按钮一直处于被按下的状态).
实现功能: 类似于Windows xp中的快速按钮的风格, 答应指定按钮的状态是自弹起(就是鼠标键按下时按钮产生被按下的效果, 鼠标键抬起的时候按钮自动回复原状)着是开关按钮(点击一次, 按钮被按下, 再点击一次, 按钮被弹起), 并能够设定一组(多个)或多组互斥按钮(即同组中有且只有一个按钮一直处于被按下的状态). 当然, 这个类不是很完善, 有许多地方可以改进, 比如没有加入当按钮处于禁止使用的状态(Disabled), 应该再引入一个Image来显示成灰色的效果等. 您就自己改改吧. :)
程序代码:
SpeedButton.java
--------------------------------
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SpeedButton extends JButton implements MouseListener {
PRivate boolean entered = false; /* 标志, 用于判定鼠标是否在按钮上 */
private boolean downed = false; /* 标志, 用于按钮是否被按下 */
private boolean enabledDowned = false; /* 标志, 用于按钮是否答应被按下 */
private Image image = null;
private int groupID = 0; /* 指定按钮属于哪个互斥组, 取值为0时不判定 */
private int buttonID; /* 按钮本身的ID号, 唯一 */
/**
* 这个构造器有点烦, 这里可以进行优化成多个构造器来组合.
* @see com.javax.swing.JButton
* @author delfan,2002.07.23
* @version 1.0
*/
public SpeedButton(int buttonID, Image image, int width, int height, String tip, boolean enabledDowned,boolean initmode, int groupID, ActionListener actionListener) {
this.buttonID = buttonID;
this.image = image;
addMouseListener(this);
setPreferredSize(new Dimension(width, height));
setToolTipText(tip);
this.enabledDowned = enabledDowned;
this.groupID = groupID;
downed = initmode;
addActionListener(actionListener);
}
public void paint(Graphics g) {
if (getSize().height == 2 getSize().width == 2) { // 画分隔条, 当按钮宽度或高度为2的时候,
g.setColor(new Color(119, 133, 168)); // 认为是分隔条
g.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
g.setColor(new Color(129, 143, 178));
g.fillRect(1, 1, getWidth(), getHeight());
setEnabled(false);
}
else
{
if (entered) { // 鼠标在按钮内
g.setColor(new Color(119, 133, 168));
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(new Color(8, 36, 107));
g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
if (image != null) g.drawImage(image, 2, 2, getParent());
} else { // 鼠标不在按钮内
g.setColor(getParent().getBackground());
g.fillRect(0, 0, getWidth(), getHeight());
if (image != null) g.drawImage(image, 2, 2, getParent());
}
if (downed) { // 按钮被按下
g.setColor(new Color(181, 190, 214));
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(new Color(8, 36, 107));
g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
if (image != null) g.drawImage(image, 3, 3, getParent());
}
}
}
public void update(Graphics g) {paint(g);}
/**
* 按钮是否处于按下状态
*/
public boolean isDowned() {return downed;}
/**
* 取得按钮的组索引号
*/
public int getGroupID() {return groupID;}
public void setDowned(boolean downed) {this.downed = downed;repaint();}
public void mouseEntered(MouseEvent e) {entered = true;repaint();}
public void mouseExited(MouseEvent e) {entered = false;repaint();}
public void mouseClicked(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
if (enabledDowned && downed && groupID == 0)
downed = false;
else
downed = true;
repaint();
}
public void mouseReleased(MouseEvent e) {
if (!enabledDowned) downed = false;
repaint();
if (groupID != 0) // 通过存放按钮的容器进行互斥按钮的状态改变操作
for (int i = 0; i < getParent().getComponentCount(); i++)
if (getParent().getComponent(i) instanceof SpeedButton) {
if (((SpeedButton) getParent().getComponent(i)).getGroupID() == groupID
&& ((SpeedButton) getParent().getComponent(i)) != this)
((SpeedButton) getParent().getComponent(i)).setDowned(false);
}
}
public int getButtonID() {return buttonID;}
}
更多精彩
赞助商链接