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

📄 jtpdefaultmenufactory.java

📁 用java实现的画图程序
💻 JAVA
字号:
package com.sunking.tp.swing;

import com.sunking.tp.framework.*;
import com.sunking.tp.tool.*;
import com.sunking.tp.util.*;
import javax.swing.*;
import java.awt.*;
/**
 *
 * <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 JTPDefaultMenuFactory  implements JTPMenuFactory {
	/**
	*单件模式的唯一实例
	*/
	private static JTPMenuFactory staticInstance;
    private JTPDefaultMenuFactory() {}    
    public static JTPMenuFactory getDefault() {
        if (staticInstance == null) {
            staticInstance = new JTPDefaultMenuFactory();
        }
        return staticInstance;
    }
	
    public JPopupMenu getPopup(JTPComponent c[]) {
        if(c == null || c.length == 0){
            return null;
        }
        Desktop desk = getDesktop((Component)c[0]);
        JPopupMenu popup = new JPopupMenu();
        createDefaultMenuItem(popup,desk);
        createSelfMenuItem(popup,desk,c);
        return popup;
    }
	/**
	*建立该组件自己的菜单
	*/
    private void createSelfMenuItem(JPopupMenu popup, Desktop desktopPane,JTPComponent c[]){
        String className = null;
        for (int index = 0; index < c.length; index++) {
            if(index == 0){
                className = c[0].getClass().getName();
            }else{
                if(!c[index].getClass().getName().equals(className)){
                    return;
                }
            }
        }
        popup.add(className+"'s MenuItems");
    }
	/**
	*建立组件通用菜单
	*/
    private void createDefaultMenuItem(JPopupMenu popup, Desktop desktopPane) {
        AbstractAction deleteAction = new ToolAction(
            desktopPane,
            new DeleteTool(desktopPane, JTPUtil.getImage("delete.gif")));
        deleteAction.putValue(Action.NAME,"Delete");
        deleteAction.putValue(Action.MNEMONIC_KEY,new Integer('D'));
        deleteAction.putValue(Action.ACCELERATOR_KEY,KeyStroke.getKeyStroke("DEL"));

        AbstractAction copyAction = new ToolAction(
            desktopPane, new CopyTool(desktopPane, JTPUtil.getImage("copy.gif")));
        copyAction.putValue(Action.NAME,"Copy");
        copyAction.putValue(Action.MNEMONIC_KEY,new Integer('C'));
        copyAction.putValue(Action.ACCELERATOR_KEY,KeyStroke.getKeyStroke("ctrl C"));

        AbstractAction pasteAction = new ToolAction(
            desktopPane,
            new PasteTool(desktopPane, JTPUtil.getImage("paste.gif")));
        pasteAction.putValue(Action.NAME,"Paste");
        pasteAction.putValue(Action.MNEMONIC_KEY,new Integer('P'));
        pasteAction.putValue(Action.ACCELERATOR_KEY,KeyStroke.getKeyStroke("ctrl V"));

        AbstractAction changeTextAction = new ToolAction(
            desktopPane,
            new ChangeTextTool(desktopPane, JTPUtil.getImage("changetext.gif")));
        changeTextAction.putValue(Action.NAME,"Change Text");
        changeTextAction.putValue(Action.MNEMONIC_KEY,new Integer('T'));

        popup.add(copyAction);
        popup.add(pasteAction);
        popup.add(deleteAction);
        popup.addSeparator();
        popup.add(changeTextAction);
    }

    Desktop getDesktop(Component c) {
        if (c instanceof Desktop)
            return (Desktop)c;
        c = c.getParent();
        if (c == null)
            return null;
        return getDesktop(c);
    }
}

⌨️ 快捷键说明

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