使用JTextArea制作剪切/替换方法 [英] Making cut/replace methods with JTextArea

查看:73
本文介绍了使用JTextArea制作剪切/替换方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在JTextArea上进行剪切和替换.我有用于复制和粘贴的代码,我认为除了删除JTA中的标记文本外,我还可以使用它.

I am to make a cut and a replace method on JTextArea. I have the code for copy and paste, and I reckon I could just use that, in addition to just deleting the marked text in the JTA.

剪切和替换方法的代码看起来像什么?

How would the code for a code for cut and replace methods look like?

用于复制的代码如下:

public void copy(){
    int start=ta.getSelectionStart();
    int end=ta.getSelectionEnd();
    String s=ta.getText();
    aString=s.substring(start,end);
    System.out.println(aString);
}

粘贴代码如下:

public void paste(){
    int start=ta.getSelectionStart();
    String startText=ta.getText().substring(0, start);
    String endText=ta.getText().substring(start);
    String res=startText+clipBoard+endText;
    ta.setText(res);
}

所以基本上:如何删除JTextArea中的标记文本?还是有更好的方法可以做到这一点?

So basically: How do I delete marked text in a JTextArea? Or is there possibly a better way to do this?

推荐答案

JTextArea默认使用关键字组合(Ctrl + C等)支持剪切/复制/粘贴.如果您想进行自定义调用(例如菜单操作),则JTextComponent本身提供了剪切/复制/粘贴方法,您可以重复使用.

JTextArea supports cut/copy/paste by default using keyword combination (Ctrl+C etc). If you want to have custom invocations (like menu action), JTextComponent itself provides cut/copy/paste methods which you can re-use.

这篇关于使用JTextArea制作剪切/替换方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆