java 如何将 JTextArea 中的选定文本转换为字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15859289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make selected text in JTextArea into a String?
提问by applemavs
I'm working on a simple word processor with java swing and layouts, and I'm trying to figure out how to make individual blocks of text bold, italics, or different font sizes instead of the whole block of text changing at once in my JTextArea.
我正在开发一个带有 java swing 和布局的简单文字处理器,我试图弄清楚如何使单个文本块变为粗体、斜体或不同的字体大小,而不是在我的整个文本块中一次更改JTextArea。
Is there some way to initialize a String as the user highlights the text in the JTextArea with their mouse? I would love it if there was some sort of ActionListener or something for JTextArea which could detect all this and easily save anything as a string, but I'm not sure if this is possible. Something like this would be great:
当用户用鼠标突出显示 JTextArea 中的文本时,是否有某种方法可以初始化字符串?如果有某种 ActionListener 或 JTextArea 可以检测到所有这些并轻松将任何内容保存为字符串,我会喜欢它,但我不确定这是否可能。这样的事情会很棒:
String selectedtext;
JTextArea type;
class TextPanel extends JPanel implements ActionListener
{
public TextPanel()
{
type = new JTextArea();
type.addActionListener(this);
this.add(type);
}
public void actionPerformed(ActionEvent e)
{
selectedtext = e.getSelected();
}
}
采纳答案by drew moore
JTextArea doesn't have any built-in functionality that will do this, but:
JTextArea 没有任何内置功能可以做到这一点,但是:
In order for someone to select text, they have to click on the text area, drag and release the click. So, attach a MouseListener and implement the mouseReleased method to check if any text was selected, and if so to save it as a string:
为了让某人选择文本,他们必须单击文本区域,拖动并释放单击。因此,附加一个 MouseListener 并实现 mouseReleased 方法来检查是否选择了任何文本,如果是,则将其保存为字符串:
public void mouseReleased(MouseEvent e) {
if (textArea.getSelectedText() != null) { // See if they selected something
String s = textArea.getSelectedText();
// Do work with String s
}
}
回答by Brandon Buck
You're not going to be able to accomplish this with a JTextArea
, you will need something that supports rich display of text like a JTextPanel
and you'll need to define styles for it, applying these styles to specific regions.
您将无法使用 a 来完成此操作JTextArea
,您将需要支持丰富显示文本的东西,例如 aJTextPanel
并且您需要为其定义样式,将这些样式应用于特定区域。
Hereis an example of a utility class for created styles (linked to give example of defining styles). The addNewStyle
and changeFont
are the two most important methods to reference. The addNewStyle
method shows how to add a predefined style to the document that you can reference when inserted (mostly for pasting should you want to past with a format). The changeFont
method shows how to create a style and apply it to a region (in the method the region is from 0
to the end of the document - so the entire document).
这是创建样式的实用程序类的示例(链接以给出定义样式的示例)。该addNewStyle
和changeFont
是参考两个最重要的方法。该addNewStyle
方法显示了如何将预定义的样式添加到文档中,您可以在插入时引用该样式(主要用于粘贴,如果您想粘贴格式)。该changeFont
方法展示了如何创建样式并将其应用于一个区域(在该方法中,该区域是从0
文档的末尾到整个文档)。
You'll probably need these styles to be crafted dynamically and so you'll need to fetch them from the region if one exists (that I've not done). All of this is done with a StyledDocument
您可能需要动态制作这些样式,因此您需要从该区域(如果存在)获取它们(我还没有这样做)。所有这一切都是通过一个StyledDocument
And example of appending text with a style to a StyledDocument
(purely for example) is:
将带有样式的文本附加到 a StyledDocument
(纯粹例如)的示例是:
styledDocument.insertString(
styledDocument.getLength(), textToInsert,
styledDocument.getStyle(styleName));
It's been some time since I worked with JTextPane
s and StyledDocuments
so most of this is pulled from the project where I did the work. I wish I could give you more info rather than just a starting point.
自从我使用JTextPane
s以来已经有一段时间了,StyledDocuments
所以大部分内容都是从我完成工作的项目中提取的。我希望我能给你更多的信息,而不仅仅是一个起点。
回答by MadProgrammer
You can use JTextComponent#setCaretPositionfollowed by JTextComponent#moveCaretPositionto highlight/select
您可以使用JTextComponent#setCaretPosition后跟JTextComponent#moveCaretPosition来突出显示/选择
回答by JavaTechnical
For detecting selection changes in JTextArea, it would be better if you make use of the CaretListener.
为了检测 JTextArea 中的选择更改,最好使用 CaretListener。
jTextArea.addCaretListener(new CaretListener(){
public void caretUpdate(CaretEvent ce)
{
int dot=ce.getDot();
int mark=ce.getMark();
if(dot!=mark)
selectedText=jTextArea.getSelectedText();
else selectedText=null;
}
});
Now, if you want to do some operations with the selected text when mouse is dragged, you could do it, because the selectedText is updated.
现在,如果您想在鼠标拖动时对选定的文本进行一些操作,您可以这样做,因为 selectedText 已更新。
回答by Protostome
JTextArea doesn't have that capability. You should look at JEditorPane
JTextArea 没有这种能力。你应该看看 JEditorPane
It can display html , so you can use bold tags and whatever you need..
它可以显示 html ,因此您可以使用粗体标签以及您需要的任何内容。