Java 获取并写出一行数字

Java 获取并写出一行数字,java,swing,Java,Swing,我需要从文本组件中获得具有多行支持的特定行。所以它不是JTextArea就是JTextPane 如何获得1号线、2号线或。。等例如,从下面的文本中获取第3行 line1 line2 line3 line4 是否可以为某一行设置另一个值?例如。设置lineNew而不是line2 line1 lineNew line3 line4 有什么办法吗?要获取JTextComponent中的文本,请使用getText()方法,该方法将返回字符串 然后,要获取行,请在\n上拆分字符串 JTextArea

我需要从文本组件中获得具有多行支持的特定行。所以它不是
JTextArea
就是
JTextPane

如何获得1号线、2号线或。。等例如,从下面的文本中获取第3行

line1
line2
line3
line4
是否可以为某一行设置另一个值?例如。设置
lineNew
而不是
line2

line1
lineNew
line3
line4

有什么办法吗?

要获取
JTextComponent
中的文本,请使用
getText()
方法,该方法将返回
字符串

然后,要获取行,请在
\n
上拆分字符串

JTextArea txt = new JTextArea("line1\nline2\nline3\nline4");
String s = txt.getText();
String[] lines = s.split("\n");
// now to access the second line, use lines[1]
现在,如果要修改文本,可以使用
setText(String)
方法

txt.setText("something else");

还有一些其他方法可用于更改文本,如
insert(String,int)
append(String)
、和
replaceRange(String,int,int)
。所有这些都记录在。

中。要获取
JTextComponent
中的文本,请使用
getText()
方法,该方法将返回
字符串

然后,要获取行,请在
\n
上拆分字符串

JTextArea txt = new JTextArea("line1\nline2\nline3\nline4");
String s = txt.getText();
String[] lines = s.split("\n");
// now to access the second line, use lines[1]
现在,如果要修改文本,可以使用
setText(String)
方法

txt.setText("something else");
还有一些其他方法可用于更改文本,如
insert(String,int)
append(String)
、和
replaceRange(String,int,int)
。所有这些都记录在。

中,您可以使用它来替换某一行

对于检索某一行,我不完全确定,但我认为,应该允许您从文本中快速提取某一行。或者正如tskuzzy已经建议的那样,检索完整的文本并自己拆分它

您可以使用它来替换某一行

对于检索某一行,我不完全确定,但我认为,应该允许您从文本中快速提取某一行。或者像tskuzzy已经建议的那样,检索完整的文本并自己拆分

如何获得1号线、2号线或。。等等

通过
JTextArea.getText()
/
JTextPane
jtextparea
/
JTextPane.getText()
获取文本。将文本作为字符串后,可以通过使用新行字符作为分隔符拆分文本来获得不同的行

JTextArea jText = new JTextArea("line1\nline2\nline3\nline4");

String temp = jText.getText();

String[] tempArr = temp.split("\n");
如何获得1号线、2号线或。。等等

通过
JTextArea.getText()
/
JTextPane
jtextparea
/
JTextPane.getText()
获取文本。将文本作为字符串后,可以通过使用新行字符作为分隔符拆分文本来获得不同的行

JTextArea jText = new JTextArea("line1\nline2\nline3\nline4");

String temp = jText.getText();

String[] tempArr = temp.split("\n");
//获取文本的方法

public String getText(int lineNos){

             return Str[lineNos].getText();

}
//方法设置文本

public void setText(int lineNos){

            Str[lineNos].setText("Hello");   // Can also use Scanner here

}
//获取文本的方法

public String getText(int lineNos){

             return Str[lineNos].getText();

}
//方法设置文本

public void setText(int lineNos){

            Str[lineNos].setText("Hello");   // Can also use Scanner here

}

阅读全文并将其替换为全文?阅读全文并将其替换为全文?