?? rolloverbutton.java
字號:
/*
* This class is modified from JEdits sources by Kris Kopicki and Slava Pestov
* The source is avaliable here: http://cvs.sourceforge.net/viewcvs.py/jedit/jEdit/org/gjt/sp/jedit/gui/RolloverButton.java?view=markup
*
* Copyright (C) 2004 Francis C.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
*
* This program 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 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 siuying.commons.ui;
//{{{ Imports
import java.awt.*;
import java.awt.event.*;
import java.lang.reflect.Method;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.plaf.basic.BasicButtonUI;
import java.util.logging.*;
import java.io.PrintWriter;
import java.io.StringWriter;
import javax.swing.plaf.metal.MetalLookAndFeel;
import com.jgoodies.plaf.LookUtils;
//}}}
/**
* This class is modified from JEdits sources by Kris Kopicki and Slava Pestov
* Original source: http://cvs.sourceforge.net/viewcvs.py/jedit/jEdit/org/gjt/sp/jedit/gui/RolloverButton.java?view=markup
*
* If you wish to have rollovers on your buttons, use this class.
*
* Unlike the Swing rollover support, this class works outside of
* <code>JToolBar</code>s, and does not require undocumented client
* property hacks or JDK1.4-specific API calls.<p>
*
* Note: You should not call <code>setBorder()</code> on your buttons,
* as they probably won't work properly.
*/
public class RolloverButton extends JButton {
//{{{ Private variables
private Logger logger = Logger.getLogger(RolloverButton.class.getName());
private String version = System.getProperty("java.version");
private Icon pressedIcon;
public static final float THIRD_TRANSPARENCY = 0.3f;
public static final float TRANSLUCENCE = 0.5f;
public static final float TOWPART_TRANSPARENCY = 0.8f;
public static final float OPACITY = -1f;
//}}}
//{{{ RolloverButton constructor
/**
* Setup the border (invisible initially)
*/
public RolloverButton() {
if(version.startsWith("1.5"))
setContentAreaFilled(false);
if(method != null) {
try {
method.invoke(this,new Boolean[] { Boolean.TRUE });
} catch(Exception e) {
StringWriter s = new StringWriter();
e.printStackTrace(new PrintWriter(s));
logger.warning(s.toString());
}
} else {
addMouseListener(new MouseOverHandler());
}
} //}}}
//{{{ RolloverButton constructor
/**
* Setup the border (invisible initially)
*/
public RolloverButton(Icon icon) {
this();
setIcon(icon);
int width = icon.getIconWidth();
int height = icon.getIconHeight();
pressedIcon = getImageIcon(icon,width,height,1,1,RolloverButton.OPACITY);
this.setPressedIcon(pressedIcon);
} //}}}
//{{{ updateUI() method
public void updateUI() {
if(LookUtils.IS_OS_WINDOWS) {
/* Workaround for uncooperative Windows L&F */
setUI(new BasicButtonUI());
} else
super.updateUI();
setBorder(new EtchedBorder());
setBorderPainted(false);
setMargin(new Insets(2,2,2,2));
setRequestFocusEnabled(false);
} //}}}
//{{{ isOpaque() method
public boolean isOpaque() {
return false;
} //}}}
//{{{ setEnabled() method
public void setEnabled(boolean b) {
super.setEnabled(b);
if(method == null) {
setBorderPainted(false);
repaint();
}
} //}}}
//{{{ setBorderPainted() method
public void setBorderPainted(boolean b) {
try {
revalidateBlocked = true;
super.setBorderPainted(b);
}
finally {
revalidateBlocked = false;
}
} //}}}
//{{{ revalidate() method
/**
* We block calls to revalidate() from a setBorderPainted(), for
* performance reasons.
*/
public void revalidate() {
if(!revalidateBlocked)
super.revalidate();
} //}}}
//{{{ paint() method
public void paint(Graphics g) {
if(method != null || isEnabled()) {
super.paint(g);
} else {
Graphics2D g2 = (Graphics2D)g;
g2.setComposite(c);
super.paint(g2);
}
} //}}}
//{{{ Private members
private static AlphaComposite c = AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, 0.5f);
private static Method method;
private boolean revalidateBlocked;
//}}}
//{{{ MouseHandler class
/**
* Make the border visible/invisible on rollovers
*/
class MouseOverHandler extends MouseAdapter {
public void mouseEntered(MouseEvent e) {
if (isEnabled())
setBorderPainted(true);
}
public void mouseExited(MouseEvent e) {
setBorderPainted(false);
}
} //}}}
private static Icon getImageIcon(Icon defaultIcon,int width, int height,int x,int y,float alpha) {
JFrame frame = new JFrame();
GraphicsConfiguration gc = frame.getGraphicsConfiguration();
Image image = gc.createCompatibleImage(width,height,Transparency.TRANSLUCENT);
Graphics2D g2D = (Graphics2D)image.getGraphics();
if(alpha > 0) {
Composite alphaTemp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,alpha);
g2D.setComposite(alphaTemp);
}
g2D.drawImage(((ImageIcon)defaultIcon).getImage(),x,y,null);
g2D.dispose();
return new ImageIcon(image);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -