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();
            }
        }
    }

你可能感兴趣的:(Java)