简体   繁体   English

如何将文本添加到JTextArea?

[英]How do I add text to a JTextArea?

I'm making a java Text Editior and I can't seems to know how to insert a line of text that is "[code][/code]" Here is what I'm trying to program. 我正在制作Java Text Editior,但似乎不知道如何插入“ [code] [/ code]”文本行,这是我要编程的内容。 The method for the inserting is called "insert". 插入的方法称为“插入”。 So it has to be something that is insert,(something that inserts strings of text in JTextArea ) 因此它必须是插入的东西(在JTextArea中插入文本字符串的东西)

/////////////////// CODE //////////////////////////////////////////////////////////////////////////////////////////////////

this.insert.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {


        }
    });
/////////////// END OF CODE ///////////////////////////////////////////////////////////////////

Simple example to set/assign text to JTextArea .. this is not the solution but it will help you... 设置/分配文本到JTextArea的简单示例..这不是解决方案,但可以帮助您...

JTextArea textArea = new JTextArea(
    "This is an editable JTextArea. " +
    "A text area is a \"plain\" text component, " +
    "which means that although it can display text " +
    "in any font, all of the text is in the same font."
);
textArea.setFont(new Font("Serif", Font.ITALIC, 16));
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);

Although to set the text .. use this method 虽然设置文本..使用此方法

void insert(String str, int pos) 
Inserts the specified text at the specified position.

public void setText(String t)
Sets the text of JTextArea

For refrerence and help please follow jtextareaguide 有关参考和帮助,请遵循jtextareaguide

Link to video tutorial 链接到视频教程

Guide for Simple Editor in Java Java 简单编辑器指南

use: 采用:

JTextArea text=new JTextArea();
  text.setText("Message..");

Here is a doc. 是一个文档。

public class JTextArea extends JTextComponent

A JTextArea is a multi-line area that displays plain text. JTextArea是显示纯文本的多行区域。 It is intended to be a lightweight component that provides source compatibility with the java.awt.TextArea class where it can reasonably do so. 它旨在成为一种轻量级的组件,在合理的程度上提供与java.awt.TextArea类的源兼容性。 You can find information and examples of using all the text components in Using Text Components, a section in The Java Tutorial. 您可以在《 Java教程》的“使用文本组件”一节中找到有关使用所有文本组件的信息和示例。

Try this: 尝试这个:

JTextArea textj1 = new JTextArea();
textj1.setText(textj1.getText().trim() + "a string or even an arraylist");

Java already provides a method for inserting text in the JTextArea class. Java已经提供了一种在JTextArea类中插入文本的方法。 Try this... 尝试这个...

 JTextArea t = new JTextArea();
 t.setText("specified string");
 t.append("+ added string");

更好地使用:

textArea.setText(textArea.getText()+" Message");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM