The java.awt.TextArea could be monitored for changes by adding a TextListener for TextEvents. In the JTextComponent based components, changes are broadcasted from the model via a DocumentEvent to DocumentListeners. The DocumentEvent gives the location of the change and the kind of change if desired. The code fragment might look something like: DocumentListener myListener = ??; JTextArea myArea = ??; myArea.getDocument().addDocumentListener(myListener);
api 原文http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JTextArea.html
JTextArea myArea = ??;
myArea.getDocument().addDocumentListener(myListener);
api 原文http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JTextArea.html
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class NotepadJFrame extends JFrame {
JScrollPane jsp;
JTextArea jta;
boolean motified = false;
String currentFileName = null;
NotepadJFrame() {
setProp();
addComponent();
this.setVisible(true);
}
private void setProp() {
this.setSize(500, 400);
this.setLocation(200, 100);
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.setTitle("我的记事本");
this.setJMenuBar(new NoteMenuBar(this));
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
close();
}
});
}
private void addComponent() {
jsp = new JScrollPane();
jta = new JTextArea();
jsp.getViewport().add(jta);
this.getContentPane().add(jsp, BorderLayout.CENTER);
jta.getDocument().addDocumentListener(new DocumentListener() {
public void insertUpdate(DocumentEvent e) {
motified = true;
}
public void removeUpdate(DocumentEvent e) {
motified = true;
}
public void changedUpdate(DocumentEvent e) {
motified = true;
}
});
}
void close() {
if(motified) {
int rst = JOptionPane.showConfirmDialog(null, "是否要保存");
switch(rst){
case JOptionPane.YES_OPTION:
save();
break;
case JOptionPane.NO_OPTION: System.exit(0);
case JOptionPane.CANCEL_OPTION:
}
} else {
System.exit(0);
}
}
void open() {
JFileChooser jfc = new JFileChooser();
jfc.showOpenDialog(null);
File f = jfc.getSelectedFile();
try {
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String tmp;
StringBuffer all = new StringBuffer();
while(!(tmp = br.readLine()).trim().equals("")) {
all.append(tmp);
all.append("\n");
}
br.close();
fr.close();
this.jta.setText(all.toString());
}catch(IOException e) {
e.printStackTrace();
}
}
void save() {
try {
if(currentFileName==null) {
saveAsFile();
}
FileWriter fw = new FileWriter(this.currentFileName);
BufferedWriter bw = new BufferedWriter(fw);
String[] all = this.jta.getText().split("\n");
for(int i=0;i<all.length;i++) {
bw.write(all[i]);
bw.newLine();
}
bw.flush();
bw.close();
fw.close();
motified=false;
}catch(IOException e) {
e.printStackTrace();
}
}
void saveAsFile() {
JFileChooser chooser = new JFileChooser("c:/");
chooser.showSaveDialog(null);
this.currentFileName = chooser.getSelectedFile().getPath();
save();
}
void newFile() {
if(motified) {
int rst = JOptionPane.showConfirmDialog(null, "是否要保存");
switch(rst){
case JOptionPane.YES_OPTION:
save();
break;
case JOptionPane.NO_OPTION:
case JOptionPane.CANCEL_OPTION:
}
}
jta.setText("");
motified = false;
}
}
import java.awt.*;
import java.awt.event.*;
class NoteMenuBar extends JMenuBar {
private JMenu menuFile;
private JMenuItem[] items;
private NotepadJFrame njf;
NoteMenuBar(NotepadJFrame njf) {
super();
this.njf = njf;
addMenuContent();
}
private void addMenuContent() {
menuFile = new JMenu("文件");
this.add(menuFile);
items = new JMenuItem[5];
String[] strItem = {"新建","打开","保存","另存为","关闭"};
for (int i = 0; i < items.length; i ++) {
items[i] = new JMenuItem(strItem[i]);
if ("关闭".equals(strItem[i])) {
menuFile.addSeparator();
}
items[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String strCmd = e.getActionCommand();
if("新建".equals(strCmd)) {
njf.newFile();
} else if ("打开".equals(strCmd)) {
njf.open();
} else if("保存".equals(strCmd)) {
njf.save();
} else if("另存为".equals(strCmd)) {
njf.saveAsFile();
} else if ("关闭".equals(strCmd)) {
njf.close();
}
}
});
menuFile.add(items[i]);
}
}
}