📄 notepad.java
字号:
package dazuoye;
import javax.swing.*;
import java.io.*;
import java.util.*;
import javax.swing.event.*;
import javax.swing.text.BadLocationException;
import java.awt.*;
import java.awt.event.*;
/**
* @author wang xiaoling B04041413
*
*/
public class Notepad implements ActionListener {
JMenuBar menubar;
JMenu filemenu, editmenu, formatmenu, viewmenu, helpmenu;
JMenuItem newitem, openitem, saveitem, saveasitem, exititem;
JMenuItem undoitem, cutitem, copyitem, pasteitem, delitem, finditem,
findnextitem, replaceitem, selallitem, timeitem, coloritem,
fontitem, aboutitem;
JCheckBoxMenuItem statusitem;
JPopupMenu popupMenu;
JMenuItem pcutitem, pcopyitem, ppasteitem, pdelitem, pselallitem,
pcoloritem, pfontitem;
JTextArea textArea;
JLabel statusLabel;
JFrame frame;
JFileChooser chooser;
JColorChooser c;
FlowLayout layout;
DocumentListener t;
String temp[] = new String[2]; // 用于存储临时撤销前数据
String filecontent = new String("");
String findinput = new String();
int findend = 0; // 上次查找结束标志
int saveflag = 1; // 保存标志
int newflag = 1;// 1可以新建,0不可新建
/**
* 主程序的构造函数
*/
public Notepad() {
frame = new JFrame("Notepad");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) { // 对窗口关闭监听
if (saveflag == 0) { // 是否保存对话框
int d = JOptionPane.showConfirmDialog(frame, "文本已更改,是否保存?",
"记事本", JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.WARNING_MESSAGE);
if (d == JOptionPane.NO_OPTION) { // 选择不保存
System.exit(0);
} else if (d == JOptionPane.YES_OPTION) { // 选择保存
int result = chooser.showSaveDialog(frame);
if (result == JFileChooser.APPROVE_OPTION) { // 保存内容
String str = textArea.getText();
String filename = chooser.getSelectedFile()
.toString(); // 获得文件保存的绝对路径
try {
String temp = new String();
Scanner in = new Scanner(str);
FileOutputStream out = new FileOutputStream(
new File(filename));
while (in.hasNextLine()) { // 是否还有下一行内容
temp = in.nextLine() + "\t"; // 换行处理
out.write(temp.getBytes()); // 向目标文件写数据
}
in.close();
out.close();
} catch (IOException ee) {
}
}
System.exit(0);
} else if (d == JOptionPane.CANCEL_OPTION) { // 放弃保存
}
} else
System.exit(0); // 直接退出程序
}
});
popupMenu = new JPopupMenu();
chooser = new JFileChooser();
chooser.setCurrentDirectory(new File(".\\")); // 默认指定文件夹
menubar = new JMenuBar();
filemenu = new JMenu("File");
filemenu.setMnemonic('F');
newitem = new JMenuItem("New");
newitem.setMnemonic('N');
KeyStroke ks1 = KeyStroke.getKeyStroke(KeyEvent.VK_N,
InputEvent.CTRL_MASK);
newitem.setAccelerator(ks1);
newitem.addMouseListener(new MouseAdapter() { // 为状态栏上显示作监听
public void mouseEntered(MouseEvent e) { // 光标在上空时若状态栏可用则显示相应指示
if (statusitem.getState() == true) // 状态栏已打开
statusLabel.setText("Create a new file"); // 在状态栏显示相应提示信息
}
public void mouseExited(MouseEvent e) { // 光标离开该菜单
if (statusitem.getState() == true) // 状态栏已打开
statusLabel.setText("Status"); // 恢复
}
});
newitem.addActionListener(new ActionListener() { // 监听新建功能
public void actionPerformed(ActionEvent e) {
if (newflag == 0) {
int d = JOptionPane.showConfirmDialog(frame,
"文本已更改,是否保存?", "记事本",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.WARNING_MESSAGE);
if (d == JOptionPane.NO_OPTION) {
textArea.setText("");
filecontent = "";
} else if (d == JOptionPane.YES_OPTION) {
int result = chooser.showSaveDialog(frame);
if (result == JFileChooser.APPROVE_OPTION) {
String str = textArea.getText();
String filename = chooser.getSelectedFile()
.toString();
filename = chooser.getCurrentDirectory()
.getName()
+ filename;
System.out.println(filename);
try {
String temp = new String();
Scanner in = new Scanner(str);
FileOutputStream out = new FileOutputStream(
new File(filename));
while (in.hasNextLine()) {
temp = in.nextLine() + "\t";
out.write(temp.getBytes());
}
in.close();
out.close();
} catch (IOException ee) {
}
}
}
} else {
textArea.setText("");
filecontent = "";
}
}
});
openitem = new JMenuItem("Open...");
KeyStroke ks2 = KeyStroke.getKeyStroke(KeyEvent.VK_O,
InputEvent.CTRL_MASK);
openitem.setAccelerator(ks2);
openitem.setMnemonic('O');
openitem.addMouseListener(new MouseAdapter() { // 为状态栏上显示作监听
public void mouseEntered(MouseEvent e) {
if (statusitem.getState() == true)
statusLabel.setText("Open an existed file");
}
public void mouseExited(MouseEvent e) {
if (statusitem.getState() == true)
statusLabel.setText("Status");
}
});
openitem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int result = chooser.showOpenDialog(frame);
try {
if (result == JFileChooser.APPROVE_OPTION) {
Scanner in = new Scanner(new FileReader(chooser
.getSelectedFile()));
String str = new String();
str = in.nextLine();
str = str + "\n";
while (in.hasNextLine()) {
str = str + in.nextLine();
str = str + "\n";
}
textArea.setText(str);
filecontent = str;
}
} catch (IOException ee) {
}
}
});
saveitem = new JMenuItem("Save");
KeyStroke ks3 = KeyStroke.getKeyStroke(KeyEvent.VK_S,
InputEvent.CTRL_MASK);
saveitem.setAccelerator(ks3);
saveitem.setMnemonic('S');
saveitem.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
if (statusitem.getState() == true)
statusLabel.setText("Save current file");
}
public void mouseExited(MouseEvent e) {
if (statusitem.getState() == true)
statusLabel.setText("Status");
}
});
saveitem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (saveflag == 0) {
int result = chooser.showSaveDialog(frame);
if (result == JFileChooser.APPROVE_OPTION) {
String str = textArea.getText();
String filename = chooser.getSelectedFile().toString();
try {
String temp = new String();
Scanner in = new Scanner(str);
FileOutputStream out = new FileOutputStream(
new File(filename));
while (in.hasNextLine()) {
temp = in.nextLine() + "\t";
out.write(temp.getBytes());
}
} catch (IOException ee) {
}
filecontent = textArea.getText();
saveflag = 1;
newflag = 0;
}
}
}
});
exititem = new JMenuItem("Exit");
exititem.setMnemonic('X');
exititem.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
if (statusitem.getState() == true)
statusLabel.setText("Exit the Notepad");
}
public void mouseExited(MouseEvent e) {
if (statusitem.getState() == true)
statusLabel.setText("Status");
}
});
exititem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (saveflag == 0) {
int d = JOptionPane.showConfirmDialog(frame, "文本已更改,是否保存?",
"记事本", JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.WARNING_MESSAGE);
if (d == JOptionPane.NO_OPTION) {
System.exit(0);
} else if (d == JOptionPane.YES_OPTION) {
int result = chooser.showSaveDialog(frame);
if (result == JFileChooser.APPROVE_OPTION) {
String str = textArea.getText();
String filename = chooser.getSelectedFile()
.toString();
filename = chooser.getCurrentDirectory().getName()
+ filename;
try {
String temp = new String();
Scanner in = new Scanner(str);
FileOutputStream out = new FileOutputStream(
new File(filename));
while (in.hasNextLine()) {
temp = in.nextLine() + "\t";
out.write(temp.getBytes());
}
in.close();
out.close();
} catch (IOException ee) {
}
}
System.exit(0);
} else if (d == JOptionPane.CANCEL_OPTION) {
}
} else
System.exit(0);
}
});
saveasitem = new JMenuItem("Save As...");
saveasitem.setMnemonic('A');
saveasitem.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
if (statusitem.getState() == true)
statusLabel.setText("Save current file as another");
}
public void mouseExited(MouseEvent e) {
if (statusitem.getState() == true)
statusLabel.setText("Status");
}
});
saveasitem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int result = chooser.showSaveDialog(frame);
if (result == JFileChooser.APPROVE_OPTION) {
String str = textArea.getText();
String filename = chooser.getSelectedFile().toString();
try {
String temp = new String();
Scanner in = new Scanner(str);
FileOutputStream out = new FileOutputStream(new File(
filename));
while (in.hasNextLine()) {
temp = in.nextLine() + "\t";
out.write(temp.getBytes());
}
} catch (IOException ee) {
}
filecontent = textArea.getText();
saveflag = 1;
}
}
});
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -