随笔-46  评论-64  文章-2  trackbacks-0
这个技巧比较Cool也比较基础常用,关键技术是使用图像重新绘制组件
看下面demo的代码
import java.awt.Insets;
import javax.swing.ImageIcon;
import javax.swing.JButton;

public class ImageButton extends JButton {

    
private static final long serialVersionUID = 7760427126786950870L;

    
public ImageButton(ImageIcon icon) {
        setSize(icon.getImage().getWidth(
null),icon.getImage().getHeight(null));
        setIcon(icon);
        setMargin(
new Insets(0,0,0,0));
        setIconTextGap(
0);
        setBorderPainted(
false);
        setBorder(
null);
        setText(
null);

    }

}


稍微要解释一下的是讲button的边框都设置成为0,还有边框的重绘设置false,我们可以用不同的贴图表示按钮被选中等的状态

使用这个组件的demoCode

        ImageButton button = new ImageButton("images/*.png");
        button.setPressedIcon(
new ImageIcon("images/*.png"));
        button.setRolloverIcon(
new ImageIcon("images/*.png"));
        button.setSelectedIcon(
new ImageIcon("images/*.png"));
        button.setRolloverSelectedIcon(
new ImageIcon("images/*.png"));
        button.setDisabledIcon(
new ImageIcon("images/*.png"));
        button.setDisabledSelectedIcon(
new ImageIcon("images/*.png"));

这个Hack要显示效果好,关键就在于贴图了,可见美工很重要。

我准备每天Hack一篇

更多内容,可以看Swing Hacks


参考资料:
 "Swing Hacks by Joshua Marinacci and Chris Adamson. Copyright 2005 O'Reilly Media, Inc., 0-596-00907-0."
posted on 2007-03-28 23:57 jht 阅读(492) 评论(0)  编辑  收藏 所属分类: J2SESwing Tips

只有注册用户登录后才能发表评论。


网站导航: