文件1:
//MyNotepad.java
package stu.lvjian.notepad;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.TextComponent;
/**
* @description: 这个类用于创建notepad程序的图形界面
* @date: 2007-08-03
* @author: lvjian
* @version: 3.0
* ============================================
* 调用自定义类列表:
* 类名称 日期 作者 版本
* OFileFilter 2007-07-21 lvjian 1.0
* StatusBarPanel 2007-07-26 lvjian 1.0
* TextFileOperation 2007-07-26 lvjian 1.0
* =============================================
*/
public class MyNotepad extends JFrame {
private BufferedWriter output = null;
private BufferedReader input = null;
JPanel pnlContent = null;
JScrollPane jspEdit = null;
JMenuBar mnuNotepad = new JMenuBar();
JMenu mnuFile = new JMenu("文件");//文件菜单
JMenuItem mnuNew = new JMenuItem("新建");
JMenuItem mnuOpen = new JMenuItem("打开");
JMenuItem mnuSave = new JMenuItem("保存");
JMenuItem mnuExit = new JMenuItem("退出");
JMenu mnuFormat = new JMenu("格式");//格式菜单
JMenu mnuFont = new JMenu("字体");//添加字体选项
JMenuItem mnuDefaultFont = new JMenuItem("默认格式");
JCheckBoxMenuItem mnuItalic = new JCheckBoxMenuItem("斜体");
JCheckBoxMenuItem mnuBold = new JCheckBoxMenuItem("粗体");
JMenu mnuColor = new JMenu("颜色");//添加颜色选项
JMenuItem mnuTextColor = new JMenuItem("文本颜色");
JMenuItem mnuBackgroundColor = new JMenuItem("背景颜色");
JMenuItem mnuDefaultColor = new JMenuItem("默认颜色");
//添加自动换行
JCheckBoxMenuItem mnuAutoRow = new JCheckBoxMenuItem("自动换行");
JMenu mnuStatus = new JMenu("查看");//查看菜单
JCheckBoxMenuItem mnuCheckStatus = new JCheckBoxMenuItem("状态栏");
JMenu mnuHelp = new JMenu("帮助");//帮助菜单
JMenuItem mnuHelpItem = new JMenuItem("帮助");
JMenuItem mnuAbout = new JMenuItem("关于");
JTextArea txaEdit = new JTextArea();//创建文本编辑区域
JFileChooser fc = new JFileChooser();//创建文件选择窗口
StatusBarPanel pnlStatusBar = new StatusBarPanel();//创建状态栏
public static boolean isEdit = false;//标识文件是否被编辑过
public static File savedFile = null;//标识已保存文件的信息
/**
* Constructor Method 初始化图形界面
*/
public MyNotepad() {
super("Simple Notepad");//调用父类构造方法
initFrame();//初始化窗口
}
/**
* 初始化窗口界面
*/
private void initFrame() {
pnlContent = (JPanel)this.getContentPane(); //得到当前窗口的容器面板
pnlContent.setLayout(new BorderLayout());//设置布局管理器为BorderLayout
//添加各图形组件
this.setJMenuBar(mnuNotepad);//添加菜单栏
//添加File菜单
mnuNotepad.add(mnuFile);
mnuFile.add(mnuNew);
mnuFile.add(mnuOpen);
mnuFile.add(mnuSave);
mnuFile.addSeparator();//添加分隔符
mnuFile.add(mnuExit);
//添加格式菜单
mnuNotepad.add(mnuFormat);
mnuFormat.add(mnuFont);
mnuFormat.add(mnuColor);
mnuFormat.addSeparator();//添加分隔符
mnuFormat.add(mnuAutoRow);
//添加字体选项
mnuFont.add(mnuBold);//添加粗体字体选项
mnuFont.add(mnuItalic);//添加斜体字体选项
mnuFont.addSeparator();//添加分隔符
mnuFont.add(mnuDefaultFont);
//添加颜色选项
mnuColor.add(mnuTextColor);
mnuColor.add(mnuBackgroundColor);
mnuColor.addSeparator();//添加分隔符
mnuColor.add(mnuDefaultColor);
//添加查看菜单
mnuNotepad.add(mnuStatus);
mnuStatus.add(mnuCheckStatus);
//添加帮助菜单
mnuNotepad.add(mnuHelp);
mnuHelp.add(mnuHelpItem);
mnuHelp.addSeparator();//添加分隔符
mnuHelp.add(mnuAbout);
//给txaEdit组件添加滚动条
jspEdit = new JScrollPane(txaEdit);
pnlContent.add(jspEdit);
//添加状态栏pnlStatusBar组件
pnlContent.add(pnlStatusBar, BorderLayout.SOUTH);
pnlStatusBar.setVisible(false);//默认为不显示
//向个组建添加事件监听器
mnuNew.addActionListener(new mnuNew_ActionPerformed(this));
mnuOpen.addActionListener(new mnuOpen_ActionPerformed(this));
mnuSave.addActionListener(new mnuSave_ActionPerformed(this));
mnuExit.addActionListener(new mnuExit_ActionPerformed(this));
mnuAutoRow.addActionListener(new mnuAutoRow_ActionPerformed(this));
mnuCheckStatus.addActionListener(new mnuCheckStatus_ActionPerformed(this));
mnuHelpItem.addActionListener(new mnuHelpItem_ActionPerformed(this));
mnuAbout.addActionListener(new mnuAbout_ActionPerformed(this));
//添加过滤器
fc.addChoosableFileFilter(new OFileFilter(".txt","txt文件"));
//设置窗口显示位置
setSize(600,450);//设置窗口尺寸
int screenWidth = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();
int screenHeight = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
int xPox = (screenWidth - this.getWidth())/2;
int yPox = (screenHeight - this.getHeight())/2;
this.setBounds(xPox, yPox, this.getWidth(), this.getHeight());
}//end initFrame method
/**
* 处理用户按下新建按键
* @param e 事件变量
*/
public void mnuNewClickAction(ActionEvent e) {
MyNotepad.savedFile = null; //清空文件标识
txaEdit.setText("");//清空文本框内的内容
}
/**
* 处理用户按下打开按键
* @param e 事件变量
*/
public void mnuOpenClickAction(ActionEvent e) {
File file = null;
int returnVal = fc.showOpenDialog(this);//显示文件打开对话框
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = fc.getSelectedFile();//得到选择的文件对象
}
try {
String text = TextFileOperation.readFile(file);//读取文件
txaEdit.setText(text);//显示数据
MyNotepad.savedFile = file;//保存标识
pnlStatusBar.setPath(file);//更新状态栏
} catch (FileNotFoundException cnfe) {
MyNotepad.savedFile = null;//清空标识
cnfe.printStackTrace();
} catch (IOException ioe) {
MyNotepad.savedFile = null;//清空标识
ioe.printStackTrace();
}
}//End mnuOpenClickAction
/**
* 处理用户按下保存按键
* @param e 事件变量
*/
public void mnuSaveClickAction(ActionEvent e) {
File file = null;
String text = null;
if (MyNotepad.savedFile == null) {
System.out.println ("File don't have been saved");
int returnVal = fc.showSaveDialog(this);//显示保存文件对话框
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = fc.getSelectedFile();//得到选择文件
text = txaEdit.getText();//得到要保存的数据
try {//调用函数保存数据
TextFileOperation.saveFile(file, text);//保存数据
MyNotepad.savedFile = file;//保存标识
} catch (IOException ioe) {
MyNotepad.savedFile = null;
ioe.printStackTrace();
}
}//end if
pnlStatusBar.setPath(MyNotepad.savedFile);//更新状态栏信息
} else {
System.out.println ("File have been saved");
file = MyNotepad.savedFile;
text = txaEdit.getText();//得到要保存的数据
try {//调用函数保存数据
TextFileOperation.saveFile(file, text);//保存数据
} catch (IOException ioe) {
MyNotepad.savedFile = null;
ioe.printStackTrace();
}
}// end if-else
}//end mnuSaveClickAction
/**
* 处理用户按下退出按键
* @param e 事件变量
*/
public void mnuExitClickAction(ActionEvent e) {
//=============== 未实现功能 =================
//关闭程序前必须检查用户是否已经保存已编辑文件
//如果已保存则直接退出,如果未保存则需要向用户
//提示是否保存文件.
//============================================
System.exit(0);
}
/**
* 处理自动换行事件,实现文本的自动换行
* @param e 事件变量
*/
public void mnuAutoRowSelectAction (ActionEvent e) {
boolean isSelected = mnuAutoRow.isSelected();//判断组件的状态
if (isSelected == true) {
System.out.println ("自动换行");
txaEdit.setLineWrap(true);//设置为自动换行
} else {
System.out.println ("取消自动换行");
txaEdit.setLineWrap(false);//取消自动换行
}
}
/**
* 处理用户选择查看状态事件
*/
public void mnuCheckStatusSelectAction(ActionEvent e) {
boolean isSelected = mnuCheckStatus.isSelected();//判断组件状态
if (isSelected == true) {
pnlStatusBar.setVisible(true);//设置状态栏mnuStatusBar为可见
} else {
pnlStatusBar.setVisible(false);//设置状态栏mnuStatusBar为不可见
}
}
/**
* 处理用户按下帮助按键
* @param e 事件变量
*/
public void mnuHelpItemClickAction(ActionEvent e) {
//=============== 未实现功能 =================
//每个程序都应该有相应的帮助文档,程序编写者无
//法保证使用该程序的用户有足够的计算机应用水平
//在不需要帮助的情况下能顺利使用该程序。
//============================================
JOptionPane.showMessageDialog(this,"如果有什么问题请联系我:"
+ "\n lvjian700@163.com");
}
/**
* 处理用户按下关于按键
* @param e 事件变量
*/
public void mnuAboutClickAction(ActionEvent e) {
JOptionPane.showMessageDialog(this,"Author: 吕健"
+ "\n QQ: 270294520"
+ "\n Email: lvjian700@163.com");
}
/**
* main method
* @param args 字符串数组参数
*/
public static void main(String[] args) {
MyNotepad frame = new MyNotepad();
frame.setVisible(true);
}
}//End MyNotepad Class
/**
* @description: 创建mnuNew组建的监听器
* @author lvjian
* @version 1.0
*/
class mnuNew_ActionPerformed implements ActionListener {
private MyNotepad adaptee;
mnuNew_ActionPerformed(MyNotepad adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.mnuNewClickAction(e);
}
}
/**
* @description: 创建mnuOpen组建的监听器
* @author lvjian
* @version 1.0
*/
class mnuOpen_ActionPerformed implements ActionListener {
private MyNotepad adaptee;
mnuOpen_ActionPerformed(MyNotepad adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.mnuOpenClickAction(e);
}
}
/**
* @description: 创建mnuSave组建的监听器
* @author lvjian
* @version 1.0
*/
class mnuSave_ActionPerformed implements ActionListener {
private MyNotepad adaptee;
mnuSave_ActionPerformed(MyNotepad adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.mnuSaveClickAction(e);
}
}
/**
* @description: 创建mnuExit组件的监听器
* @author lvjian
* @version 1.0
*/
class mnuExit_ActionPerformed implements ActionListener {
private MyNotepad adaptee;
mnuExit_ActionPerformed(MyNotepad adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.mnuExitClickAction(e);
}
}
/**
* @description: 创建mnuAutoRow组件的监听器
* @author lvjian
* @version 1.0
*/
class mnuAutoRow_ActionPerformed implements ActionListener {
private MyNotepad adaptee;
mnuAutoRow_ActionPerformed(MyNotepad adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.mnuAutoRowSelectAction(e);
}
}
/**
* @description: 创建mnuStatusBar组件的监听器
* @author lvjian
* @version 1.0
*/
class mnuCheckStatus_ActionPerformed implements ActionListener {
private MyNotepad adaptee;
mnuCheckStatus_ActionPerformed(MyNotepad adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.mnuCheckStatusSelectAction(e);
}
}
/**
* @description: 创建mnuHelp组件的监听器
* @author lvjian
* @version 1.0
*/
class mnuHelpItem_ActionPerformed implements ActionListener {
private MyNotepad adaptee;
mnuHelpItem_ActionPerformed(MyNotepad adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.mnuHelpItemClickAction(e);
}
}
/**
* @description: 创建mnuAbout组件的监听器
* @author lvjian
* @version 1.0
*/
class mnuAbout_ActionPerformed implements ActionListener {
private MyNotepad adaptee;
mnuAbout_ActionPerformed(MyNotepad adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.mnuAboutClickAction(e);
}
}