【发布时间】:2020-01-10 19:01:22
【问题描述】:
我正在尝试构建一个日历应用程序并使用我标记为笔记的备忘录部分。我有一个添加按钮来添加新注释,我希望它添加到路径中的当前文件中。我正在尝试使用 BufferWriter 来执行此操作。我附加了 newNote() 方法,它打开一个新框架并允许新文本。我想我正在尝试将新文本附加到当前文件中,但我看到的示例显示以这种方式执行此操作。 txt 文件的输出不是我所期望的。我认为这是由于调用了 textArea 对象,它正在提取对象的数据,而不是 textArea 内的输入。我对 Java 有点陌生,我正在做这个项目供个人使用,而不是上课。任何帮助和见解将不胜感激。这也是我第一次在论坛发帖,如果有更好的方法,请告诉我。
newNote() 方法。
public static void newNote() {//opens new frame to create a new note
//variables for the new window
JFrame noteFrame = new JFrame("New Note");
JPanel notePanel = new JPanel();
JButton cancelButton = new JButton("Cancel");
JButton addButton = new JButton("Add");
JTextArea textArea = new JTextArea("Add notes here");
//creates and positions buttons
addButton.setBounds(150,330,65,40);
addButton.addActionListener(new ActionListener() {//writes contents to a txt file when Add is clicked
@Override
public void actionPerformed(ActionEvent actionEvent) {
BufferedWriter writer = null;
try {
writer = new BufferedWriter((new FileWriter("/home/skydawg/pCloudDrive/Documents/test/Log.txt", true)));
writer.write(String.valueOf(textArea));
writer.newLine();
writer.flush();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
if (writer != null) try {
writer.close();
} catch (IOException ioe2) {
// just ignore it
}
noteFrame.dispose();//closes the frame
}}
});
输出到txt文件
newjavax.swing.JTextArea[,10,10,280x295,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.synth.SynthBorder@13e59af,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=javax.swing.plaf.ColorUIResource[r=0,g=0,b=0],disabledTextColor=javax.swing.plaf.ColorUIResource[r=218,g=218,b=218],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=javax.swing.plaf.ColorUIResource[r=255,g=255,b=255],selectionColor=javax.swing.plaf.ColorUIResource[r=134,g=108,b=186],colums=0,columWidth=0,rows=0,rowHeight=0,word=true,wrap=true]
【问题讨论】:
标签: java swing jtextarea bufferedwriter