/**
* Copyright 2009 ChinaSoft International Ltd. All rights reserved.
*/
package com.chinasofti.notebook;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.swing.event.*;
import javax.print.*;
import javax.print.attribute.*;
import java.util.*;
import java.text.*;
/**
* MyNotebook 示例
*
* @author Martin
* @version 1.0 2009-10-30
*/
public class MyNotebook extends JFrame implements ActionListener {
Container con;
JScrollPane JSPane;
JTextArea text;
JMenuBar mainMenuBar;
JMenu fileMenu, editMenu, formatMenu, helpMenu;
JMenuItem newItem, openItem, saveItem, saveasItem, pageItem, printItem, exitItem;
JMenuItem undoItem, cutItem, copyItem, pasteItem, findItem, replaceItem, selectallItem, dateItem;
JCheckBoxMenuItem wrapItem;
JMenuItem fontItem;
JMenuItem helpItem, aboutItem;
boolean changed = false;
boolean haveName = false;
File file = null;
//主程序入口
public static void main(String[] args) {
MyNotebook notbook = new MyNotebook();
notbook.setVisible(true);
}
//创建界面、安装各种监听器、
public MyNotebook() {
setTitle("记事本 -- 未命名");
con = getContentPane();
text = new JTextArea();
JSPane = new JScrollPane(text);
createMenu();
setJMenuBar(mainMenuBar);
con.add(JSPane, BorderLayout.CENTER);
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width - 800) / 2, (screenSize.height - 600) / 2, 800, 600);
}
//创建主菜单
public void createMenu() {
//创建JMenuBar
mainMenuBar = new JMenuBar();
//创建四个JMenu
fileMenu = new JMenu("文件");
editMenu = new JMenu("编辑");
formatMenu = new JMenu("格式");
helpMenu = new JMenu("帮助");
//创建JMenuItem并添加到对应的JMenu中
mainMenuBar.add(fileMenu);
newItem = new JMenuItem("新建");
openItem = new JMenuItem("打开..");
saveItem = new JMenuItem("保存..");
saveasItem = new JMenuItem("另存为..");
pageItem = new JMenuItem("页面设置..");
printItem = new JMenuItem("打印..");
exitItem = new JMenuItem("退出");
fileMenu.add(newItem);
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.add(saveasItem);
fileMenu.addSeparator();
fileMenu.add(pageItem);
fileMenu.add(printItem);
fileMenu.addSeparator();
fileMenu.add(exitItem);
mainMenuBar.add(helpMenu);
helpItem = new JMenuItem("帮助主题");
aboutItem = new JMenuItem("关于..");
helpMenu.add(helpItem);
helpMenu.add(aboutItem);
exitItem.addActionListener(this);
saveItem.addActionListener(this);
saveasItem.addActionListener(this);
newItem.addActionListener(this);
printItem.addActionListener(this);
openItem.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
Object obj;
obj = e.getSource();
if (obj == exitItem) {
doExit();
} else if (obj == saveItem) {
doSave();
} else if (obj == saveasItem) {
doSaveAs();
} else if (obj == newItem) {
doNewFile();
} else if (obj == openItem) {
doOpen();
}
}
//当用户按下窗口的“关闭”时,会自动调用此方法
protected void processWindowEvent(WindowEvent e) {
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
doExit();
}
}
//程序退出时的代码
void doExit() {
int select;
if (!changed) {
System.exit(0);
} else {
select = JOptionPane.showConfirmDialog(this, "文件修改后尚未存盘,要保存吗?");
switch (select) {
case JOptionPane.YES_OPTION:
select = doSave();
if (select == 1) {
System.exit(0);
}
break;
case JOptionPane.NO_OPTION:
System.exit(0);
break;
case JOptionPane.CANCEL_OPTION:
break;
}
}
}
//保存用户编辑的文件,保存成功返回1,否则返回0
int doSave() {
FileOutputStream fout;
byte content[];
int flag;
if (!haveName) {
flag = doSaveAs();
} else if (changed) {
try {
fout = new FileOutputStream(file);
content = text.getText().getBytes();
fout.write(content);
fout.close();
changed = false;
JOptionPane.showMessageDialog(this, "保存成功!");
flag = 1;
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(this, "指定的文件名称或属性有问题!");
flag = 0;
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "无法写文件,请检查文件是否被锁定");
flag = 0;
}
} else {
JOptionPane.showMessageDialog(this, "保存成功,其他情况!");
flag = 1;
}
return flag;
}
//用"另存为"对话框保存文件。保存成功返回1,否则返回0
int doSaveAs() {
FileOutputStream fout;
byte content[];
int flag = 0;
File tmpfile = null;
ExampleFileFilter filter = new ExampleFileFilter();
JFileChooser chooser;
filter.addExtension("txt");
filter.setDescription("文本文件");
if (file != null) {
chooser = new JFileChooser(file.getPath());
} else {
chooser = new JFileChooser();
}
chooser.setFileFilter(filter);
flag = chooser.showSaveDialog(this);
if (flag == JFileChooser.APPROVE_OPTION) {
tmpfile = chooser.getSelectedFile();
if (tmpfile.exists()) {
if (JOptionPane.showConfirmDialog(this, "文件已经存在,是否覆盖?",
"警告", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
flag = 1;
} else {
flag = 0;
}
} else {
flag = 1;
}
} else {
flag = 0;
}
if (flag == 1) {//用户已经确定要以指定名称保存文件
try {
fout = new FileOutputStream(tmpfile);
content = text.getText().getBytes();
fout.write(content);
fout.close();
flag = 1;
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(this, "指定的文件名称或属性有问题!");
flag = 0;
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "无法写文件,请检查文件是否被锁定");
flag = 0;
}
}
if (flag == 1) {//文件保存成功,修改相关变量
changed = false;
haveName = true;
file = tmpfile;
this.setTitle(file.getName() + " - 记事本");
}
return flag;
}
//新建一个文件
void doNewFile() {
int select, flag;
if (changed) {
select = JOptionPane.showConfirmDialog(this, "文件修改后尚未存盘,要保存吗?");
switch (select) {
case JOptionPane.YES_OPTION:
flag = doSave();
break;
case JOptionPane.NO_OPTION:
flag = 1;
break;
default:
flag = 0;
break;
}
} else {
flag = 1;
}
if (flag == 1) {
changed = false;
haveName = false;
setTitle("记事本 -- 未命名");
text.setText(null);