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

📄 createtool.java

📁 这也是我在论坛上看见的一个程序。感觉非常好
💻 JAVA
字号:
package com.sunking.tp.tool;

import java.awt.*;
import java.awt.event.MouseEvent;
import javax.swing.*;
import com.sunking.tp.framework.*;
import com.sunking.tp.swing.*;
import com.sunking.tp.util.*;

/**
 *
 * <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 CreateTool extends AbstractTool {
    int index = 3;
    JTPComponent btt;
    JTPComponent source;
    Point lastPoint;
    /**
     *
     * @param desk 所属绘图桌面
     * @param c 被创建组件的原型
     * @param icon 图标
     */
    public CreateTool(Desktop desk, JTPComponent c, Icon icon) {
        super(desk);
        setToolTip(JTPUtil.getString("CreateTool_TIP"));
        setIcon(icon);
        source = c;
    }

    /**
     *启用工具
     */
    public void enabledTool() {
        super.enabledTool();
        desk.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));

        if (source instanceof JTPImage) {
            ImageIcon icon = JTPUtil.getImageIconFromChooser();
            if (icon != null) {
                ( (JTPImage) source).setIcon(icon);
            }
        }
    }

    public void mousePressed(MouseEvent e) {
        lastPoint = e.getPoint();
        btt = (JTPComponent) (source.clone()); //复制一个做为新创建的组件
        btt.setText("[" + index++ +"]" + source.getText());
        ( (Component) btt).setLocation(e.getPoint());
        ( (Container) desk).add( ( (Component) btt), null);

        int type = UndoableEditFactory.CREATE_JTPBUTTON;
        if (btt instanceof JTPTextArea) {
            type = UndoableEditFactory.CREATE_JTPTEXTAREA;
        } else if (btt instanceof JTPImage) {
            type = UndoableEditFactory.CREATE_JTPIMAGE;
        }
        UndoableEditFactory.getDefault().startEdit(type, desk, btt);
    }

    public void mouseReleased(MouseEvent e) {
        /**
         *长宽为0时表示未创建
         */
        try {
            if ( ( (Component) btt).getWidth() == 0 || ( (Component) btt).getHeight() == 0) {
                ( (Container) desk).remove( ( (Component) btt));
                UndoableEditFactory.getDefault().cancelEdit();
            } else {
                UndoableEditFactory.getDefault().stopEdit(btt);
            }
        } catch (Exception ex) {
        }
        lastPoint = null;
        btt = null;
        desk.setTool(null);
    }

    /**
     *拖动组件时改变大小
     */
    public void mouseDragged(MouseEvent e) {
        if (btt != null && lastPoint != null) {
            int x = e.getX(), y = e.getY(), m = lastPoint.x, n = lastPoint.y;
            int tmp = 0;
            if (x < m) {
                tmp = x;
                x = m;
                m = tmp;
            }
            if (y < n) {
                tmp = y;
                y = n;
                n = tmp;
            }
            ( (Component) btt).setBounds(m, n, x - m, y - n);
            ( (Container) desk).repaint();
        }
    }
}

⌨️ 快捷键说明

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