所以我是java的新手,我正在开发我的第一个java程序,它是一个记事本的副本。
我目前对“新”功能有疑问,所以基本上我的目标是如果用户在文档上更改了某些内容然后在新文档上按“新建”它将显示对话框以保存然后清除textarea(这是我的askToSave()方法),如果它是一个现有的文件,那么它只会保存文件然后清除textarea。但是如果文档没有变化那么它就会清除textarea。
我的问题是我创建了一个DocumentLisnter来监听我的textarea如果有变化,我将Instance Variable statusTextArea设置为false然后如果我的textare上发生了更改,它会将此值设置为true然后如果这是真的它将运行我的保存方法,然后只需清除textarea。但似乎即使我没有改变文本区域的任何内容,它总是运行我的askToSave()方法。
这是我的“新”听众
public class NewMenuListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (statusTextArea == true) {
askToSave();
} else {
editorTextArea.setText("");
}
}
}
这是我的askToSave方法:
private void askToSave() {
JOptionPane message = new JOptionPane();
int x = message.showConfirmDialog(frame, "Do you want to save changes?", "DVTextEditor", message.YES_NO_CANCEL_OPTION);
if (x == message.YES_OPTION) {
if(chooser.getSelectedFile() == null) {
chooser.showSaveDialog(frame);
saveFile(chooser.getSelectedFile());
} else {
saveFile(chooser.getSelectedFile());
}
editorTextArea.setText("");
} else if(x == message.NO_OPTION) {
editorTextArea.setText("");
} else {
//meh
}
}
这是DocumentListener:
public class DocListener implements DocumentListener {
public void changedUpdate(DocumentEvent e) {
statusTextArea = true;
}
public void insertUpdate(DocumentEvent e) {
statusTextArea = true;
}
public void removeUpdate(DocumentEvent e) {
statusTextArea = true;
}
}
下面的完整代码:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class DVTextEditor {
private JTextArea editorTextArea;
private JFrame frame;
private JFileChooser chooser;
private boolean statusTextArea;
public DVTextEditor() {
chooser = new JFileChooser();
statusTextArea = false;
}
public void go() {
// make frame
JFrame frame = new JFrame("DVTextEditor");
//make menu bar and menu
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenu formatMenu = new JMenu("Format");
//make menu items for file menu
JMenuItem newMenuItem = new JMenuItem("New");
JMenuItem openMenuItem = new JMenuItem("Open");
JMenuItem saveMenuItem = new JMenuItem("Save");
//add menu items in file menu
fileMenu.add(newMenuItem);
fileMenu.add(openMenuItem);
fileMenu.add(saveMenuItem);
//action listeners for New and Save menu items
newMenuItem.addActionListener(new NewMenuListener());
openMenuItem.addActionListener(new OpenMenuListener());
saveMenuItem.addActionListener(new SaveMenuListener());
//make menu items for format menu
JCheckBoxMenuItem wrapMenuItem = new JCheckBoxMenuItem("Word Wrap ");
//add menu items in format menu
formatMenu.add(wrapMenuItem);
//action listener for word Wrap item menu
wrapMenuItem.addItemListener(new WordWrapListener());
// add menu in menu bar
menuBar.add(fileMenu);
menuBar.add(formatMenu);
// make text area and put inside scrollpane
editorTextArea = new JTextArea(15, 50);
editorTextArea.setMargin(new Insets(5, 5, 5, 5));
Font defaulFont = new Font("sanserif", Font.PLAIN, 15);
editorTextArea.setFont(defaulFont);
// add listener for JTextArea
editorTextArea.getDocument().addDocumentListener(new DocListener());
// make scrollpane then add textarea to scrollpane
JScrollPane eScrollBar = new JScrollPane(editorTextArea);
eScrollBar.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
eScrollBar.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
// add menu bar and text area in frame
frame.setJMenuBar(menuBar);
frame.getContentPane().add(BorderLayout.CENTER, eScrollBar);
// enable frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
frame.setVisible(true);
}
public class NewMenuListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (statusTextArea == true) {
askToSave();
statusTextArea = false;
System.out.println("Status is false");
}
else {
editorTextArea.setText("");
statusTextArea = false;
System.out.println("Status is false");
}
}
} //end New Menu Inner Class
private void askToSave() {
JOptionPane message = new JOptionPane();
int x = message.showConfirmDialog(frame, "Do you want to save changes?", "DVTextEditor", message.YES_NO_CANCEL_OPTION);
if (x == message.YES_OPTION) {
if(chooser.getSelectedFile() == null) {
chooser.showSaveDialog(frame);
saveFile(chooser.getSelectedFile());
} else {
saveFile(chooser.getSelectedFile());
}
editorTextArea.setText("");
}
else if (x == message.NO_OPTION) {
editorTextArea.setText("");
}
else {
//meh
}
}
public class DocListener implements DocumentListener {
public void changedUpdate(DocumentEvent e) {
statusTextArea = true;
System.out.println("Status is true!");
}
public void insertUpdate(DocumentEvent e) {
statusTextArea = true;
System.out.println("Status is true!");
}
public void removeUpdate(DocumentEvent e) {
statusTextArea = true;
System.out.println("Status is true!");
}
}
public class OpenMenuListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
chooser.showOpenDialog(frame);
loadFile(chooser.getSelectedFile());
}
} // end Open Menu Inner Class
private void loadFile(File file) {
StringBuilder myDoc = new StringBuilder();
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = null;
while((line = reader.readLine()) != null) {
myDoc.append(line + "\n");
}
reader.close();
} catch (Exception ex) {
System.out.println("Could not read file");
ex.printStackTrace();
}
editorTextArea.setText(myDoc.toString());
statusTextArea = false;
System.out.println("Status is false");
}
public class SaveMenuListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
chooser.showSaveDialog(frame);
saveFile(chooser.getSelectedFile());
}
} // end Save Menu Inner Class
private void saveFile(File file) {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
//writer.write(editorTextArea.getText()); <- does not work write only one line
//instead of using bufferedWriter's method to write use textarea's method to write
editorTextArea.write(writer);
writer.close();
} catch (IOException ex) {
System.out.println("Could not save file!");
ex.printStackTrace();
}
statusTextArea = false;
System.out.println("Status is false");
}
public class WordWrapListener implements ItemListener {
public void itemStateChanged (ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED) {
editorTextArea.setLineWrap(true);
editorTextArea.setWrapStyleWord(true);
frame.repaint();
} else {
editorTextArea.setLineWrap(false);
editorTextArea.setWrapStyleWord(false);
frame.repaint();
}
}
} // end inner class
public static void main(String[] args) {
DVTextEditor x = new DVTextEditor();
x.go();
}
}