简体   繁体   English

JTextArea insert()不断给出错误

[英]JTextArea insert() keeps giving errors

I'm trying to insert JTextArea text in a specified position. 我正在尝试在指定位置插入JTextArea文本。 But keep getting illegal argument exceptions. 但是,请继续获取非法的论点例外。 Here's the code. 这是代码。

import javax.swing.JTextArea;
import javax.swing.JFrame;
import java.awt.Color;
import javax.swing.JScrollPane;
import java.awt.FlowLayout;
import javax.swing.JButton;
import java.awt.Point;

public class TextWork{
    public static  void main(String[] args)  { 
        JTextArea fnew = new JTextArea(20,20);
        JFrame Main = new JFrame();
        Main.setVisible(true);
        Main.setResizable(true);
        Main.setTitle("Gui");
        Main.setSize(500,500);
        Main.getContentPane().setLayout(null);
        Main.setLocationRelativeTo(null);
        Main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        fnew.insert("Hello",5);
        fnew.setBounds(100,100,100,100); 
        fnew.setEditable(false);
        Main.add(fnew);
    }
}

As per the documentation , if you try to insert text through an index which exceeds the amount of text already present in the component, an exception is thrown. 根据文档 ,如果您尝试通过索引插入文本,而该索引超出了组件中已经存在的文本数量,则会引发异常。

In short, your text field is empty, so try to insert text at the 5th location exceeds the text field's length. 简而言之,您的文本字段为空,因此请尝试在第5个位置插入超出文本字段长度的文本。 Since this is the first time your are adding text, you might want to use setText("Hello") instead. 由于这是您第一次添加文本,因此您可能需要使用setText("Hello")

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

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