import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class notepadDemo extends JFrame implements Runnable{
boolean sure = false;// 是否被修改
boolean isSave = true;// 是否被保存过
JCheckBoxMenuItem LineWrap;// 自动换行按钮
JTextField textField;// 用作状态栏
JTextField timeField;// 用作状态栏
JPanel fieldPanel;// 状态栏用 容器
JTextArea textArea; // 文本域
JMenuBar menuBar;// 菜单栏
JPopupMenu popup;// 弹出菜单
Thread t = new Thread(this);//线程
/*----------------构造器----------------*/
notepadDemo(String aName) {
setFrame(aName);// 设定窗口属性
setMenuBar();// 设定菜单栏
showFrame();// 显示所有窗口 必须在最后调用
}
/*------------------添加元素显示窗口------------------*/
public void showFrame() {
this.add(menuBar, "North");
this.add(fieldPanel, "South");
this.setVisible(true);
}
/*-----------------设置窗口属性-------------------*/
public void setFrame(String aName) {
this.setTitle(aName);// 窗口名称
this.setBounds(150, 150, 640, 480);// 设定大小和起始位置
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);// 关闭窗口时不做任何动作(因为可能有文件还未保存)
this.addWindowListener(new WindowAdapter() {// 窗口关闭事件:同时停止运行程序
public void windowClosing(WindowEvent e) {
exit(); // 退出时要做的动作。如是否保存文件等
}
});
textArea = new JTextArea();
fieldPanel = new JPanel(new GridLayout(1, 2));
timeField = new JTextField();// 状态栏显示时间
textField = new JTextField();// 状态栏显示当前处理文件的路径
fieldPanel.add(textField);
fieldPanel.add(timeField);
this.getContentPane().add(new JScrollPane(textArea));// 自动换行添加滚动条的文件域
/**
* 文本域事件监听
*/
textArea.getDocument().addDocumentListener(new DocumentListener() {
public void insertUpdate(DocumentEvent e) {
isSave = false;
}
public void removeUpdate(DocumentEvent e) {
isSave = false;
}
public void changedUpdate(DocumentEvent e) {
isSave = false;
}
});
/**
* 文本域鼠标事件监听
*/
textArea.addMouseListener(new MouseListener() {// 在文本域内的鼠标事件
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {// 如果按下的是鼠标右键
popup = new JPopupMenu();// 弹出菜单
JMenuItem cut = new JMenuItem("剪切(T)");
JMenuItem copy = new JMenuItem("复制(C)");
JMenuItem paste = new JMenuItem("粘贴(P)");
/*----------添加按钮的快捷键-----------*/
cut.setMnemonic('T');
cut.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_T, InputEvent.CTRL_MASK));
copy.setMnemonic('C');
copy.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_C, InputEvent.CTRL_MASK));
paste.setMnemonic('P');
paste.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_V, InputEvent.CTRL_MASK));
/*-------向弹出菜单内添加按钮-------*/
popup.add(copy);
popup.add(cut);
popup.add(paste);
/*--------------按钮事件-----------------*/
cut.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textArea.cut();
}
});
copy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textArea.copy();
}
});
paste.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textArea.paste();
}
});
/*------------显示弹出菜单-------------*/
popup.show(textArea, e.getX(), e.getY());
}
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
});
}
/*----------------菜单栏--------------------*/
public void setMenuBar() {
menuBar = new JMenuBar();
JMenu file = new JMenu("文件(F)");
JMenu edit = new JMenu("编辑(E)");
JMenu check = new JMenu("格式(O)");
JMenu help = new JMenu("帮助(H)");
JMenuItem menuNew = new JMenuItem("新建(N)");
JMenuItem menuOpen = new JMenuItem("打开(O)");
JMenuItem menuExit = new JMenuItem("退出(X)");
JMenuItem menuSave = new JMenuItem("保存(S)");
JMenuItem menuSaveAs = new JMenuItem("另存(A)");
JMenuItem cut = new JMenuItem("剪切(T)");
JMenuItem copy = new JMenuItem("复制(C)");
JMenuItem paste = new JMenuItem("粘贴(P)");
JMenuItem myFont = new JMenuItem("字体(F)");
LineWrap = new JCheckBoxMenuItem("自动换行(W)", false);
JMenuItem TimeDate = new JMenuItem("时间/日期(D)");
JMenuItem about = new JMenuItem("关于(A)");
/*---------------------状态栏---------------------*/
textField.setEditable(false);// 文本不可编辑
timeField.setEditable(false);// 文本不可编辑
/*---------------------按钮快捷键---------------------*/
file.add(menuNew);
menuNew.setMnemonic('N');
menuNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,
InputEvent.CTRL_MASK));
file.add(menuOpen);
menuOpen.setMnemonic('O');
menuOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
InputEvent.CTRL_MASK));
file.addSeparator();
file.add(menuSave);
menuSave.setMnemonic('S');
menuSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
InputEvent.CTRL_MASK));
file.add(menuSaveAs);
menuSaveAs.setMnemonic('A');
menuSaveAs.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,
InputEvent.CTRL_MASK));
file.addSeparator();
file.add(menuExit);
menuExit.setMnemonic('X');
menuExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,
InputEvent.CTRL_MASK));
edit.add(cut);
cut.setMnemonic('T');
cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T,
InputEvent.CTRL_MASK));
edit.addSeparator();
edit.add(copy);
copy.setMnemonic('C');
copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
InputEvent.CTRL_MASK));
edit.add(paste);
paste.setMnemonic('P');
paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,
InputEvent.CTRL_MASK));
edit.addSeparator();
edit.add(TimeDate);
TimeDate.setMnemonic('D');
TimeDate.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D,
InputEvent.CTRL_MASK));
check.add(LineWrap);
LineWrap.setMnemonic('W');
LineWrap.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W,
InputEvent.CTRL_MASK));
check.add(myFont);
myFont.setMnemonic('F');
myFont.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,
InputEvent.CTRL_MASK));
help.add(about);
about.setMnemonic('A');
about.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,
InputEvent.CTRL_MASK));
menuBar.add(file);
file.setMnemonic('F');
menuBar.add(edit);
edit.setMnemonic('E');
menuBar.add(check);
check.setMnemonic('O');
menuBar.add(help);
help.setMnemonic('H');
/*------------------添加事件-----------------------*/
menuOpen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
menuOpen_actionPerformed(e);
}
});
menuSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
menuSave_actionPerformed(e);
}
});
menuExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fileExit_actionPerformed(e);
}
});
menuSaveAs.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
menuSaveAs_actionPerformed(e);
}
});
menuNew.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
menuNew_actionPerformed(e);
}
});
cut.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cut_actionPerformed(e);
}
});
copy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
copy_actionPerformed(e);
}
});
paste.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
paste_actionPerformed(e);
}
});
myFont.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
myFont_actionPerformed(e);
}
});
TimeDate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
t.start();
}
});
about.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
about_actionPerformed(e);
}
});
LineWrap.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
LineWrap_actionPerformed(e);
}
});
}
/*--------------事件处理----------------------*/
public void fileExit_actionPerformed(ActionEvent e) {
exit();
}
public void menuOpen_actionPerformed(ActionEvent e) {
/**
* 打开文件 选择窗口
*
* @JFileChooser 可以新开一个文件选择窗口
*/
JFileChooser fileChooser = new JFileChooser();
fileChooser.addChoosableFileFilter(fileChooser.getFileFilter());// 添加一个文件过滤器
// 当前的文件过滤器
if (fileChooser.APPROVE_OPTION != fileChooser.showOpenDialog(this)) {
return;// 弹出的对话框,如果用户选择不为确认(yes、ok)按钮时,return
}
/*--------------------------------------------------------------------*/
/**
* 文件读操作
*
* @字符缓冲流
*/
if (isSave) {
textArea.setText("");// 清空文本区
try {
BufferedReader dataIn = new BufferedReader(new FileReader(// 新建一个字符流
fileChooser.getSelectedFile().getPath()));// 文件路径是
// 选择的文件的路径
textField.setText(fileChooser.getSelectedFile().getPath());// 保存当前操作文件的路径
String str = null;
do {
try {
str = dataIn.readLine();// 字符流的读取文件内容方法 一次读一行
} catch (IOException e1) {
System.out.println("读取一个文件里的内容时出现一个" + e1.toString()
+ "异常");
}
if (str == null) {
textArea.append("");
} else {
textArea.append(str + "/n");
}
} while (str != null);
} catch (FileNotFoundException e1) {
System.out.println("打开一个文件时出现一个" + e1.toString() + "异常");
}
} else {// 另存为操作
try {
JFileChooser fileSaveAs = new JFileChooser();// 保存文件的窗口
fileSaveAs.setDialogTitle("另存为"); // 设置保存文件的窗口的标题
fileSaveAs.addChoosableFileFilter(fileSaveAs.getFileFilter());
if (fileSaveAs.APPROVE_OPTION != fileSaveAs
.showSaveDialog(this)) {
return;// 弹出的对话框,如果用户选择不为确认(yes、ok)按钮时,return
}
BufferedWriter dataOut = new BufferedWriter(new BufferedWriter(
new FileWriter(fileSaveAs.getSelectedFile())));
String str = null;// 写入用的临时字符串变量
str = textArea.getText();// 取得文本
dataOut.write(str);// 写入文本
dataOut.close();// 关闭流
} catch (IOException e1) {
System.out.println("另存文件时发现一个" + e1.toString() + "异常");
}
isSave = true;// 保存过为true
}
}
/*----------------------------------------------------*/
/**
* 文件另存操作
*/
public void menuSaveAs_actionPerformed(ActionEvent e) {
try {
JFileChooser fileSaveAs = new JFileChooser();// 保存文件的窗口
fileSaveAs.setDialogTitle("另存为"); // 设置保存文件的窗口的标题
fileSaveAs.addChoosableFileFilter(fileSaveAs.getFileFilter());
if (fileSaveAs.APPROVE_OPTION != fileSaveAs.showSaveDialog(this)) {
return;// 弹出的对话框,如果用户选择不为确认(yes、ok)按钮时,return
}
BufferedWriter dataOut = new BufferedWriter(new BufferedWriter(
new FileWriter(fileSaveAs.getSelectedFile())));
String str = null;// 写入用的临时字符串变量
str = textArea.getText();// 取得文本
dataOut.write(str);// 写入文本
dataOut.close();// 关闭流
} catch (IOException e1) {
System.out.println("另存文件时发现一个" + e1.toString() + "异常");
}
isSave = true;// 保存过为true
}
/*----------------------------------------------------*/
/**
* 文件保存操作
*/
public void menuSave_actionPerformed(ActionEvent e) {
try {
JFileChooser fileSaveAs = new JFileChooser();// 保存文件的窗口
BufferedWriter dataOut = new BufferedWriter(new BufferedWriter(
new FileWriter(textField.getText())));
String str = null;// 写入用的临时字符串变量
str = textArea.getText();// 取得文本
dataOut.write(str);// 写入文本
dataOut.close();// 关闭流
} catch (IOException e1) {
try {
JFileChooser fileSaveAs = new JFileChooser();// 保存文件的窗口
fileSaveAs.setDialogTitle("另存为"); // 设置保存文件的窗口的标题
fileSaveAs.addChoosableFileFilter(fileSaveAs.getFileFilter());
if (fileSaveAs.APPROVE_OPTION != fileSaveAs
.showSaveDialog(this)) {
return;// 弹出的对话框,如果用户选择不为确认(yes、ok)按钮时,return
}
BufferedWriter dataOut = new BufferedWriter(new BufferedWriter(
new FileWriter(fileSaveAs.getSelectedFile())));
String str = null;// 写入用的临时字符串变量
str = textArea.getText();// 取得文本
dataOut.write(str);// 写入文本
dataOut.close();// 关闭流
} catch (IOException e2) {
System.out.println("另存文件时发现一个" + e2.toString() + "异常");
}
// System.out.println("另存文件时发现一个" + e1.toString() + "异常");
}
isSave = true;// 保存过为true
}
/**
* 文件新建操作
*/
public void menuNew_actionPerformed(ActionEvent e) {
if (isSave) {// 如果保存过
textArea.setText("");// 清空文本区
} else {// 如果没保存过
if (textArea.getText().equals("")) {
textArea.setText("");// 清空文本区
} else {
menuSaveAs_actionPerformed(e);// 执行另存为
isSave = true;// 保存过为true
}
}
}
/**
* 编辑剪切操作
*/
public void cut_actionPerformed(ActionEvent e) {
textArea.cut();
}
/**
* 编辑复制操作
*/
public void copy_actionPerformed(ActionEvent e) {
textArea.copy();
}
/**
* 编辑粘贴操作
*/
public void paste_actionPerformed(ActionEvent e) {
textArea.paste();
}
/**
* 编辑字体操作
*/
public void myFont_actionPerformed(ActionEvent e) {
JColorChooser fileColor = new JColorChooser(); // 调色板
textArea.setSelectionColor(fileColor
.showDialog(this, "颜色", Color.black));
// JFontChooser fileFont;
}
/**
* 自动换行
*/
public void LineWrap_actionPerformed(ActionEvent e) {
if (LineWrap.getState() == false) {// 单行菜单按钮未被选中时
textArea.setLineWrap(false);// 不自动换行
textArea.setText(textArea.getText());// 刷新文本区
}
if (LineWrap.getState()) {// 单行菜单按钮被选中时
textArea.setLineWrap(true);// 自动换行
textArea.setText(textArea.getText());// 刷新文本区
}
}
/**
* 编辑时间日期操作 在状态栏显示日期时间
*/
public void run() {
while(true){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
timeField.setText(new Date().toLocaleString().toString());// 显示时间日期
}
}
/**
* 帮助关于操作
*/
public void about_actionPerformed(ActionEvent e) {
}
/*-----------------退出方法-------------------*/
public void exit() {
if (isSave) {// 保存过就退出
System.exit(0);
} else {// 没保存过
/**
* 弹出个对话框 选择是否要保存
*/
final JDialog ready = new JDialog(this, "警告");
ready.setBounds(300, 300, 260, 150);
ready.setLayout(new BorderLayout());// 边界布局
JTextArea text = new JTextArea("/n/t文件内容已更改。/n /t是否保存?");
text.setEditable(false);
ready.add(new JLabel().add(text), "Center");
JButton yes = new JButton("是");
JButton no = new JButton("否");
JButton cancel = new JButton("取消");
JPanel panel = new JPanel(new GridLayout(1, 3, 10, 10));
panel.add(yes);
panel.add(no);
panel.add(cancel);
ready.add(panel, "South");
ready.setVisible(true);
yes.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 另存为
menuSaveAs_actionPerformed(e);
ready.setVisible(false);
}
});
no.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);// 不保存就退出
}
});
cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ready.setVisible(false);
}
});
}
}
/*------------------------------------*/
public static void main(String[] args) {
new notepadDemo("记事本");
}
}