我正在尝试构建日历应用程序,并使用我标记为便笺的便笺部分。我有一个添加按钮来添加新的笔记,我想将其添加到路径中的当前文件中。我正在尝试使用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,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]