从滚动窗格中的文本窗格获取文本

时间:2014-05-06 08:45:20

标签: java swing jscrollpane

我只是Java的初学者。 这是我的问题:

我使用此代码(1)

创建了一个可滚动的文本字段
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)代码,并要求从不在我的可滚动窗格中的文本字段中获取文本,并且它完美地工作..

我认为我无法从滚动窗口中的文本窗格中获取文本...是否可能?

2 个答案:

答案 0 :(得分:2)

您正在(1)中创建两次jZone_Text。第二个jZone_Text不是滚动窗格中的那个,因此它是空的。替换为:

        jZone_Text = new JTextPane();
         scrollPane = new JScrollPane(jZone_Text);                              
         jPanelRecord.add(scrollPane);
         scrollPane.setBounds(20, 70, 550, 190);    
         scrollPane.setVisible(false);

此外,它在(2)中被称为jTitre_Text,我假设它是一个错字

答案 1 :(得分:1)

您创建一个空的JTextPane()

用字符串填充,然后使用.getText()来获取这个" myString"

和btw:不要在第3行创建new JTextPane();)