jtoolbutton.java

来自「java画图源码」· Java 代码 · 共 82 行

JAVA
82
字号
package com.sunking.tp.swing;

import java.beans.*;

import java.awt.event.*;
import javax.swing.*;

import com.sunking.tp.framework.*;
import com.sunking.tp.tool.*;

/**
 *
 * <p>Title: </p>
 * <p>Description: 工具按钮</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author <a href="mailto:sunkingxie@hotmail.com">SunKing</a>
 * @version 1.0
 */
public class JToolButton extends JToggleButton implements ActionListener,PropertyChangeListener{
    private static final long serialVersionUID = -3000000000000000007L;
    Desktop desk;
    Tool tool;
    /**
     * @param desk  绘图桌面
     * @param tool  工具
     */
    public JToolButton(Desktop desk,Tool tool) {
        this.desk = desk;
        this.tool = tool;
        addActionListener(this);
        init(tool);
        tool.addPropertyChangeListener(this);
        setFocusable(false);
    }
    void init(Tool tool){
        setText(tool.getLabel());
        setToolTipText(tool.getToolTip());
        setIcon(tool.getIcon());
        setEnabled(tool.isEnabled());
        setSelected(tool.isSelected());
    }
    public void actionPerformed(ActionEvent e) {
        desk.setTool(tool);
    }
    /**
     *
     * @param e
     */
    public void propertyChange(PropertyChangeEvent e){
        String property = e.getPropertyName();
        Object newValue = e.getNewValue();
        if(property.equals(Tool.ENABLED_PROPERTY)){
            if(!newValue.equals(Tool.NULL_VALUE)){
                setEnabled(((Boolean)newValue).booleanValue());
            }
        }else if(property.equals(Tool.ICON_PROPERTY)){
            if(!newValue.equals(Tool.NULL_VALUE)){
                setIcon((Icon)e.getNewValue());
                setSelectedIcon((Icon)e.getNewValue());
            }else{
                setIcon(null);
            }
        }else if(property.equals(Tool.LABEL_PROPERTY)){
            if(!newValue.equals(Tool.NULL_VALUE)){
                setText(e.getNewValue().toString());
            }else{
                setText(null);
            }
        }else if(property.equals(Tool.TOOLTIP_PROPERTY)){
            if(!newValue.equals(Tool.NULL_VALUE)){
                setToolTipText(e.getNewValue().toString());
            }else{
                setToolTipText(null);
            }
        }else if(property.equals(Tool.SELECT_PROPERTY)){
            setSelected(((Boolean)newValue).booleanValue());

        }
    }
}

⌨️ 快捷键说明

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