效果图
[img]http://dl.iteye.com/upload/picture/pic/85569/77ccaf8e-d3d5-39dd-b7d5-0c2a24c2f25b.jpg[/img]
导师的项目里要用到的!
界面的最上边有工具栏,需要添加下边带文字提示的按钮!
通过继承,可以订做自己喜欢的按钮!
[img]http://dl.iteye.com/upload/picture/pic/85569/77ccaf8e-d3d5-39dd-b7d5-0c2a24c2f25b.jpg[/img]
导师的项目里要用到的!
界面的最上边有工具栏,需要添加下边带文字提示的按钮!
通过继承,可以订做自己喜欢的按钮!
package util;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class PicturizedButton extends JButton
{
private static final long serialVersionUID = -7782576754032594674L;
public PicturizedButton(String filename, String text) {
try {
PicturizedButton.this.setBorderPainted(false);
setHorizontalTextPosition(SwingConstants.CENTER);
setVerticalTextPosition(SwingConstants.BOTTOM);
setText(text);
if (filename != null) {
ImageIcon icon = new ImageIcon(this.getClass().getResource(
filename));
if (icon != null)
setIcon(icon);
}
} catch (Exception e) {
//System.out.println("[Error Load Image: " + filename + "]");
e.printStackTrace();
}
this.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
PicturizedButton.this.setBorderPainted(true);
PicturizedButton.this.setCursor(Cursor
.getPredefinedCursor(Cursor.HAND_CURSOR));
}
public void mouseExited(MouseEvent e) {
PicturizedButton.this.setBorderPainted(false);
PicturizedButton.this.setCursor(Cursor
.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
});
}
public static void main(String[] args){
JFrame frame = new JFrame("TestButton");
PicturizedButton icturizedButton = new PicturizedButton("/image/gis32.png","多拉a梦");
frame.add(icturizedButton);
frame.setVisible(true);
frame.setSize(200,200);
}
}