write() 方法无法识别 TextArea
write() method not recognizing TextArea
我已经为名为 "Save" 的工具栏菜单项编写了一个 ActionListener
,单击时我希望程序将 TextArea
l1 的内容保存到 .txt文件名为"filename",所以这就是我想出的代码(由于程序很长,我在这里只粘贴了错误所在或可能所在的代码片段):
import java.awt.*;
import java.awt.event.*;
...
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.Writer;
class Progetto extends Frame {
...
private TextArea l1 = new TextArea();
...
private MenuBar menubar;
private Menu file;
private MenuItem save;
...
salva.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
BufferedWriter fileOut = new BufferedWriter(new FileWriter("filename.txt"));
l1.write(fileOut);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
});
当我 运行 这个程序时,我得到以下错误:
Error: cannot find symbol
symbol: method write(java.io.BufferedWriter)
location: variable l1 of type java.awt.TextArea
我在文档和论坛上到处都看了,但我不明白为什么会出现这个错误,也不知道如何解决...
我该如何解决这个错误?
编辑
TextArea
看起来像这样:
Jack: 9.0
Mark: 9.0
Nick: 1.0
Linn: 6.5
Jhon: 3.5
将动作侦听器中的代码更改为:
salva.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
String text = l1.getText();
if ( text != null && !text.isEmpty() )
{
String[] lines = text.split("\n");
BufferedWriter fileOut = new BufferedWriter(new FileWriter("filename.txt"));
for ( String line : lines )
{
fileOut.write( line, 0, line.length()-1 );
fileOut.newLine();
}
fileOut.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
我已经为名为 "Save" 的工具栏菜单项编写了一个 ActionListener
,单击时我希望程序将 TextArea
l1 的内容保存到 .txt文件名为"filename",所以这就是我想出的代码(由于程序很长,我在这里只粘贴了错误所在或可能所在的代码片段):
import java.awt.*;
import java.awt.event.*;
...
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.Writer;
class Progetto extends Frame {
...
private TextArea l1 = new TextArea();
...
private MenuBar menubar;
private Menu file;
private MenuItem save;
...
salva.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
BufferedWriter fileOut = new BufferedWriter(new FileWriter("filename.txt"));
l1.write(fileOut);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
});
当我 运行 这个程序时,我得到以下错误:
Error: cannot find symbol
symbol: method write(java.io.BufferedWriter)
location: variable l1 of type java.awt.TextArea
我在文档和论坛上到处都看了,但我不明白为什么会出现这个错误,也不知道如何解决...
我该如何解决这个错误?
编辑
TextArea
看起来像这样:
Jack: 9.0
Mark: 9.0
Nick: 1.0
Linn: 6.5
Jhon: 3.5
将动作侦听器中的代码更改为:
salva.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
String text = l1.getText();
if ( text != null && !text.isEmpty() )
{
String[] lines = text.split("\n");
BufferedWriter fileOut = new BufferedWriter(new FileWriter("filename.txt"));
for ( String line : lines )
{
fileOut.write( line, 0, line.length()-1 );
fileOut.newLine();
}
fileOut.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});