Java 从滚动窗格中的文本窗格获取文本
我只是Java的初学者。 我的问题是: 我用以下代码创建了一个可滚动的文本字段(1):Java 从滚动窗格中的文本窗格获取文本,java,swing,jscrollpane,Java,Swing,Jscrollpane,我只是Java的初学者。 我的问题是: 我用以下代码创建了一个可滚动的文本字段(1): jZone_Text = new JTextPane(); scrollPane = new JScrollPane(jZone_Text); jZone_Text = new JTextPane(); jPanelRecord.add(scrollPane); scrollPane.setBo
jZone_Text = new JTextPane();
scrollPane = new JScrollPane(jZone_Text);
jZone_Text = new JTextPane();
jPanelRecord.add(scrollPane);
scrollPane.setBounds(20, 70, 550, 190);
scrollPane.setVisible(false);
然后我想从我的JTextPane中获取文本并将其写入.txt文件
所以我做了这个(2):
try {
FileWriter fw = new FileWriter ("C:\\Users\\Admin\\Desktop\\memo.txt");
BufferedWriter bw = new BufferedWriter (fw);
PrintWriter fichierSortie = new PrintWriter (bw);
fichierSortie.println (jZoneText.getText()+"\n ");
fichierSortie.close();
}catch (IOException e2){
}
但当我尝试时,它会创建我的备忘录,但它是空白的
我尝试使用(2)代码,并要求从一个文本字段中获取文本,该字段不在我的可滚动窗格中,并且工作正常
我想我无法从滚动窗格中的文本窗格中获取文本。。。可能吗?您在(1)中创建了两次jZone_文本。第二个jZone_文本不是滚动窗格中的文本,因此为空。替换为:
jZone_Text = new JTextPane();
scrollPane = new JScrollPane(jZone_Text);
jPanelRecord.add(scrollPane);
scrollPane.setBounds(20, 70, 550, 190);
scrollPane.setVisible(false);
另外,在(2)中,它被称为jTitre_Text,我假设它是一个打字错误您创建了一个空的
JTextPane()
用字符串填充它,然后使用.getText()
获取此“myString”
顺便说一句:不要在第3行中创建
新的JTextPane()
) 非常感谢你!!我没有看到那个错误:-)@xxx请单击复选标记验证答案,否则这个问题将显示为正确。并投票选出好答案;)这就是这个董事会的工作原理:)我不得不等15分钟才说问题解决:)谢谢你们提醒我。我不能投票给你们,我是名誉1号,我必须等到15岁才知道。显然也谢谢你们!!x) 我太专注于(2)