Java实现剪切复制粘贴功能(含不同文本域的判断)

通过焦点事件判断不同的文本域
不同文本域焦点事件的处理

        tp1.addFocusListener(new FocusListener(){

            @Override
            public void focusGained(FocusEvent e) {
                // TODO Auto-generated method stub
                focus = 1;
            }

            @Override
            public void focusLost(FocusEvent e) {
                // TODO Auto-generated method stub
                //focus = 0;
            }
        });
        tp2.addFocusListener(new FocusListener(){

            @Override
            public void focusGained(FocusEvent e) {
                // TODO Auto-generated method stub
                focus = 2;
            }

            @Override
            public void focusLost(FocusEvent e) {
                // TODO Auto-generated method stub
                //focus = 0;
            }
        });
        tp3.addFocusListener(new FocusListener(){

            @Override
            public void focusGained(FocusEvent e) {
                // TODO Auto-generated method stub
                focus = 3;
            }

            @Override
            public void focusLost(FocusEvent e) {
                // TODO Auto-generated method stub
                //focus = 0;
            }
        });

//剪切

public void cut(){
        //final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();

        String temp1 = tp1.getSelectedText();
        String temp2 = tp2.getSelectedText();
        String temp3 = tp3.getSelectedText();

        cutStr1 = new StringSelection(temp1);
        cutStr2 = new StringSelection(temp2);
        cutStr3 = new StringSelection(temp3);

        //根据焦点位置判断文本域
        if(focus == 1){
            clipboard.setContents(cutStr1, null);
            tp1.replaceSelection("");
        }else if(focus == 2){
            clipboard.setContents(cutStr2, null);
            tp2.replaceSelection("");
        }else if(focus == 3){
            clipboard.setContents(cutStr3, null);
            tp3.replaceSelection("");
        }
    }

    //复制
    public void copy(){     
        String temp1 = tp1.getSelectedText();
        String temp2 = tp2.getSelectedText();
        String temp3 = tp3.getSelectedText();

        copyStr1 = new StringSelection(temp1);
        copyStr2 = new StringSelection(temp2);
        copyStr3 = new StringSelection(temp3);

        if(focus == 1){
            clipboard.setContents(copyStr1, null);
        }else if(focus == 2){
            clipboard.setContents(copyStr2, null);
        }else if(focus == 3){
            clipboard.setContents(copyStr3, null);
        }
    }

    //粘贴
    public void paste(){
        final Transferable transferable = clipboard.getContents(this);
        final DataFlavor dataFlavor = DataFlavor.stringFlavor;

        if(transferable.isDataFlavorSupported(dataFlavor)){
            try{
                final String str = (String)transferable.getTransferData(dataFlavor);
                if(focus == 1){
                    tp1.replaceSelection(str);
                }else if(focus == 2){
                    tp2.replaceSelection(str);
                }else if(focus == 3){
                    tp3.replaceSelection(str);
                }
            }catch(Exception e1){
                e1.printStackTrace();
            }
        }
    }
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值