码迷,mamicode.com
首页 > 编程语言 > 详细

[Java Swing 大富翁] swing下使用JPanel 模拟 按钮实现。

时间:2015-06-05 13:46:08      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

按钮点击需要让jpanel实现鼠标点击事件。跟随着效果的切换,即可实现按钮的效果。

举个例子:

有一个"购买"按钮,images如下

 

normal: 技术分享 

mouseOver:技术分享 

disabled:技术分享 

pressed:技术分享

 

代码如下:

public class ShopButton extends JPanel implements MouseListener {
    
    private Shop shopUI;
    
    private Image[] img;
    
    private Image normalImage;
    private Image rolloverImage;
    private Image pressedImage;
    private Image disabledImage;
    
    private Image currentImage;

    private boolean enabled = true;
    
    private String name = null;

    private Control control;
    
    public ShopButton(Shop shopUI,String name, int x, int y,Control control) {
        this.shopUI = shopUI;
        this.name = name;//设置名称
        this.control = control;
        this.img = this.shopUI.createCardImg(name);//这里就是使用工厂模式获取img资源
        this.normalImage = this.img[0];
        this.rolloverImage = this.img[1];
        this.pressedImage =this.img[2];
        this.disabledImage = this.img[3];
        this.currentImage = normalImage;
        this.setBounds(x, y, this.img[0].getWidth(null), this.img[0].getHeight(null));
        this.addMouseListener(this);
    }

    
    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public void paint(Graphics g) {
        this.setOpaque(false); // 背景透明
        if (enabled){
            g.drawImage(currentImage, this.getX(), this.getY(), this.getWidth(),
                    this.getHeight(), this);
        } else if (!(name.equals("buy") || name.equals("cancel"))){
            g.drawImage(disabledImage, this.getX(), this.getY(), this.getWidth(),
                    this.getHeight(), this);
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {

    }

    @Override
    public void mousePressed(MouseEvent e) {
            currentImage = pressedImage;
            if(enabled){
                if (this.name.equals("close")){//退出商店
                    this.shopUI.moveToBack();
                    this.control.exitShop();
                } else if (this.name.equals("cancel")){//取消当前选择
                    this.shopUI.setChooseCard(null);
                } else if (this.name.equals("buy")){//购买当前选择
                    this.control.buyCard(this.shopUI.getShop());
                } else {
                    this.shopUI.setChooseCard(this);
                }
            }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
            currentImage = rolloverImage;
    }

    @Override
    public void mouseEntered(MouseEvent e) {
            currentImage = rolloverImage;
    }

    @Override
    public void mouseExited(MouseEvent e) {
            currentImage = normalImage;
    }
}

适当的扩展可以做成通用类,能够模拟各类按钮类。

以下是 mini大富翁内的商店界面:红框内是扩展实现

技术分享

 

[Java Swing 大富翁] swing下使用JPanel 模拟 按钮实现。

标签:

原文地址:http://www.cnblogs.com/moonlight-fly/p/4554299.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!