按鈕點擊須要讓jpanel實現鼠標點擊事件。跟隨着效果的切換,便可實現按鈕的效果。ide
舉個例子:this
有一個"購買"按鈕,images以下spa
normal: code
mouseOver: orm
disabled: blog
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; } }
適當的擴展能夠作成通用類,可以模擬各種按鈕類。get
如下是 mini大富翁內的商店界面:紅框內是擴展實現it