程序没什么含金量,高手莫笑。
package first;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import java.io.*;
public class F {
// GUI控件
private JFrame frame = new JFrame();
private JMenu fileMenu = new JMenu("文件");
private JMenuBar bar = new JMenuBar();
private JMenuItem newFileItem = new JMenuItem("新建");
private JMenuItem openFileItem = new JMenuItem("打开");
private JMenuItem saveFileItem = new JMenuItem("保存");
private JMenuItem exitItem = new JMenuItem("退出");
private JScrollPane scrollPane = null;
private JTextArea textarea = new JTextArea(8, 40);
private JFileChooser chooseFile;
//
private String filePath; // 打开文件的路径
private StringBuffer content = new StringBuffer(); // 正文
private boolean isSaved = false; // 当前文件是否已保存
public void GUIInit() {
// frame.setResizable(false); // 禁止调整frame的大小
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(100, 100, 800, 600);
textarea.setLineWrap(true);
scrollPane = new JScrollPane(textarea);
frame.add(scrollPane, BorderLayout.CENTER);
InitMenu();
frame.setVisible(true);
}
private void InitMenu() {
frame.setJMenuBar(bar);
bar.add(fileMenu);
fileMenu.add(newFileItem);
fileMenu.add(openFileItem);
fileMenu.add(saveFileItem);
fileMenu.add(exitItem);
newFileItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
openFileItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (isSaved == false) { // 注意isSaved值的改变
Object[] options = { "OK", "CANCEL" };
if (JOptionPane.showOptionDialog(null, "当前文件还没保存",
"Warning", JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE, null, options,
options[0]) == 1) {
return;
}
}
chooseFile = new JFileChooser();
String msg;
int result = chooseFile.showOpenDialog(frame);
if (result == JFileChooser.APPROVE_OPTION) {
filePath = chooseFile.getSelectedFile().getPath();
} else {
return;
}
try {
BufferedReader in = new BufferedReader(new FileReader(
filePath));
content.delete(0, content.length());
String line = "";
try {
while ((line = in.readLine()) != null) {
content.append(line);
}
} catch (IOException e2) {
JOptionPane.showMessageDialog(frame, e2);
}
textarea.setText(content.substring(0));
} catch (FileNotFoundException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
}
});
saveFileItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (isSaved == true) // 已经保存过就不用再保存了
return;
chooseFile = new JFileChooser();
String msg;
int result = chooseFile.showOpenDialog(frame);
if (result == JFileChooser.APPROVE_OPTION) {
filePath = chooseFile.getSelectedFile().getPath();
} else {
return;
}
//JOptionPane.showMessageDialog(frame, filePath);
try {
PrintWriter out = new PrintWriter(filePath);
out.print(textarea.getText());
out.flush();
} catch (FileNotFoundException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
}
});
exitItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
public static void main(String args[]) {
F f = new F();
f.GUIInit();
}
}