⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 facebutton.java

📁 java编写的扫雷程序
💻 JAVA
字号:
/* FaceButton.java 1.0 2003-6-15
 *
 * Copyleft (c) 2003 RatKing.
 */

package jmine;

import java.awt.AWTEvent;
import java.awt.Dimension;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.plaf.ButtonUI;

/**
 * 笑脸按钮
 *
 * @author <a href="ratking@ynet.com">RatKing</a>
 * @version 1.0
 */
public class FaceButton extends AbstractButton /*implements MouseListener*/ {
    private JMine jmine;

    public static final int FACE_SMILE  = 0;
    public static final int FACE_O      = 1;
    public static final int FACE_WIN    = 2;
    public static final int FACE_LOSE   = 3;

    private ImageIcon[] icon = new ImageIcon[4];

    private int iconStyle;
    private ImageIcon myIcon;

    private static final Border raisedBevelBorder
            = new CompoundBorder(LineBorder.createGrayLineBorder(),
                                BorderFactory.createRaisedBevelBorder());
    private static final Border loweredBevelBorder
            = new CompoundBorder(LineBorder.createGrayLineBorder(),
                                MineLineBorder.createBorder());

    public FaceButton(JMine jmine) {
        this(jmine, FACE_SMILE);
    }

    public FaceButton(JMine jmine, int iconStyle) {
        this.jmine = jmine;
        icon[FACE_SMILE]= jmine.createImageIcon("face_smile.png", "笑脸");
        icon[FACE_O]    = jmine.createImageIcon("face_o.png", "吃啦?");
        icon[FACE_WIN]  = jmine.createImageIcon("face_win.png", "戴眼镜");
        icon[FACE_LOSE] = jmine.createImageIcon("face_lose.png", "苦脸");

        // Create the model
        setModel(new DefaultButtonModel());
        // initialize
        init(null, null);

        enableEvents(AWTEvent.MOUSE_EVENT_MASK);
        setIconStyle(iconStyle);
        setBorderUp();
        setFocusPainted(false);
        setContentAreaFilled(false);

        Dimension d = new Dimension(26, 26);
        setPreferredSize(d);
        setMinimumSize(d);
        setMaximumSize(d);
    }

    /**
     * 设置边框为凸出样式
     */
    public void setBorderUp() {
        setBorder(raisedBevelBorder);
    }


    /**
     * 设置边框为凹下样式
     */
    public void setBorderDown() {
        setBorder(loweredBevelBorder);
    }

    public void setIconStyle(int iconStyle) {
        this.iconStyle = iconStyle;
        setIcon(icon[iconStyle]);
    }

    /**
     * Resets the UI property to a value from the current look and
     * feel.
     *
     * @see JComponent#updateUI
     */
    public void updateUI() {
        setUI((ButtonUI)UIManager.getUI(this));
    }

    private static final String uiClassID = "ButtonUI";

    /**
     * Returns a string that specifies the name of the L&F class
     * that renders this component.
     *
     * @return the string "ButtonUI"
     * @see JComponent#getUIClassID
     * @see UIDefaults#getUI
     * @beaninfo
     *        expert: true
     *   description: A string that specifies the name of the L&F class.
     */
    public String getUIClassID() {
        return uiClassID;
    }

    /** 重载对鼠标事件的处理 */
    protected void processMouseEvent(MouseEvent e) {
        super.processMouseEvent(e);

        if ((e.getID() == MouseEvent.MOUSE_PRESSED && e.getButton() == MouseEvent.BUTTON1)
                || (e.getID() == MouseEvent.MOUSE_ENTERED
                && (e.getModifiersEx() & InputEvent.BUTTON1_DOWN_MASK) == InputEvent.BUTTON1_DOWN_MASK)) {
            // 按钮凹下去
            setBorderDown();
        }
        else if ((e.getID() == MouseEvent.MOUSE_RELEASED
                && ((e.getModifiersEx() & (InputEvent.BUTTON1_DOWN_MASK | InputEvent.BUTTON2_DOWN_MASK | InputEvent.BUTTON3_DOWN_MASK)) == 0))
                || e.getID() == MouseEvent.MOUSE_EXITED) {
            // 按钮凸起来
            setBorderUp();
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -