📄 mynotepad.java
字号:
package notepad;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
public class MyNotepad extends WindowAdapter implements ActionListener,DocumentListener{
private JFrame frame;
private JMenuBar menuBar;
private JMenu menus[];
private JMenuItem items[][];
//private final int itemCount;
private JTextArea text;
private JScrollPane text_scroll;
private String content;
private boolean content_changed;
private final String menuName[] = {"文件", "编辑", "视图", "帮助"};
private final String menuItemName[][] = { //所有菜单项 分别对应
{"新建", "打开", "保存", "另存为...","退出"}, //--文件
{"查找...","替换...","复制", "剪切", "粘贴"}, //--编辑
{"字体...","颜色..."}, //--视图
{"帮助", "关于"} //--帮助
};
//该程序使用到的私有方法
private void initMenu() {
int menuNum = menuName.length;
menus = new JMenu[menuNum];
for (int i=0; i<menuNum; ++i) {
menus[i] = new JMenu(menuName[i]);
}
}
private void initMenuItem() {
int menuItemNum = menuItemName.length;
items = new JMenuItem[menuItemNum][];
for (int i=0; i<menuItemNum; ++i) {
int count = menuItemName[i].length;
items[i] = new JMenuItem[count];
for (int j=0; j<count; ++j) {
items[i][j] = new JMenuItem(menuItemName[i][j]);
}
}
}//end of initMenuItem
private void addMenu() {
for (int i=0; i<menus.length; ++i){
menuBar.add(menus[i]);
}
}
private void addMenuItem() {
for (int i=0; i<menus.length; ++i){
for (int j=0; j<items[i].length; ++j) {
menus[i].add(items[i][j]);
}
}
}
private void addActionListenerForMenuItem() {
int the_c = 0;
for (int i=0; i<items.length; ++i){
the_c = items[i].length;
for (int j=0; j<the_c; ++j) {
items[i][j].addActionListener(this);
}
}
}
//构造函数
public MyNotepad() {
super();
content = "";
content_changed = false;
frame = new JFrame();
menuBar = new JMenuBar();
text = new JTextArea();
text_scroll = new JScrollPane(text);
initMenu();
initMenuItem();
frame.setTitle("俺的记事本...");
addMenu();
addMenuItem();
addActionListenerForMenuItem();
text.getDocument().addDocumentListener(this);
frame.addWindowListener(this);
frame.add(menuBar, BorderLayout.NORTH);
frame.add(text_scroll, BorderLayout.CENTER);
frame.setVisible(true);
frame.setSize(600, 460);
frame.setLocation(220, 180);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
// setMenuBar(menuBar);
}
public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();
// 文件菜单事件
if ("新建".equals(str)) {
/*
fileDlg = new FileDialog(this, "请选择一个文件", FileDialog.LOAD);
fileDlg.setVisible(true);
String file = fileDlg.getFile();
String path = fileDlg.getDirectory();
File f = new File(path + file);
int length = (int) f.length();
char buffer[] = new char[length];
if (null != file) {
try {
FileReader fReader = new FileReader(f);
fReader.read(buffer);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
content = new String(buffer);
text.setText(content);
*/
}
if ("打开".equals(str)) {
openFile();
}
if ("保存".equals(str)) {
saveFile();
}
if ("另存为...".equals(str)) {
saveFile();
}
if ("退出".equals(str)) {
exitNotepad();
}
// 编辑菜单事件
if ("查找...".equals(str)) {
find();
}
if ("替换...".equals(str)) {
replace();
}
if ("复制".equals(str)) {
}
if ("剪切".equals(str)) {
}
if ("粘贴".equals(str)) {
}
// 视图菜单事件
if ("字体...".equals(str)) {
chooseFont();
}
if ("颜色...".equals(str)) {
chooseColor();
}
// 帮助菜单事件
if ("帮助".equals(str)) {
}
if ("关于".equals(str)) {
about();
}
}// end of actionPerformed
//监听文本改变事件
public void changedUpdate(DocumentEvent e) {
content_changed = true;
}
public void insertUpdate(DocumentEvent e) {
content_changed = true;
}
public void removeUpdate(DocumentEvent e) {
content_changed = true;
}
private void openFile() {
JFileChooser fileCh= new JFileChooser();
int result = fileCh.showOpenDialog(frame);
if (result != JFileChooser.APPROVE_OPTION) {
return;
}
File file = fileCh.getSelectedFile();
System.out.print(file);
FileReader fr = null;
BufferedReader br = null;
try {
fr = new FileReader(file);
int in = 0;
while ((in = fr.read()) != -1) {
content += (char)in;
}
fr.close();
}
catch (IOException ioe) {
ioe.printStackTrace();
}
text.setText(content);
}
private void saveFile() {
JFileChooser fileCh= new JFileChooser();
int result = fileCh.showSaveDialog(frame);
if (result != JFileChooser.APPROVE_OPTION) {
return;
}
File file = fileCh.getSelectedFile();
System.out.print(file);
FileWriter fos = null;
BufferedWriter bos = null;
try {
fos = new FileWriter(file);
bos = new BufferedWriter(fos);
bos.write(content, 0, content.length());
fos.flush();
bos.flush();
fos.close();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void find() {
String result = JOptionPane.showInputDialog(frame,
"请输入你要查找的内容", "查找", JOptionPane.INFORMATION_MESSAGE);
}
private void replace() {
JOptionPane.showMessageDialog(frame,
"该功能暂时还未实现", "替换", JOptionPane.INFORMATION_MESSAGE);
}
private void chooseFont() {
MyFontDlg fontDlg = new MyFontDlg(frame, "选择字体对话框", true);
//fontDlg.setFontDlgSize(360,260);
text.setFont(fontDlg.getFont());
//System.out.println(fontDlg.getFont().hashCode());
}
private void chooseColor() {
Color color = JColorChooser.showDialog(frame, "choose color", Color.BLACK);
text.setForeground(color);
}
private void about() {
JOptionPane.showMessageDialog(frame,
"该程序由hehe独立编写\n在使用过程中如果遇到什么问题,请与我联系:\nhe@he.com",
"关于", JOptionPane.INFORMATION_MESSAGE);
}
// 重写WindowAdapter类的方法
public void windowClosing(WindowEvent we) {
if (!content_changed) {
int res = JOptionPane.showConfirmDialog(frame, "真的要退出吗?", "确认退出",
JOptionPane.YES_NO_OPTION);
if (res == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
else if (chooseSaveFile()) { //要保存文件
saveFile();
content_changed = false;
}
else {
System.exit(0);
}
}
public void exitNotepad() {
if (!content_changed) { //内容没有改变, 直接退出
System.exit(0);
return;
}
else if (chooseSaveFile()) { //要保存文件
saveFile();
content_changed = false;
}
else {
System.exit(0);
}
}
private boolean chooseSaveFile(){
boolean result = false;
int res = JOptionPane.showConfirmDialog(frame,
"当前的文件内容已经改变,\n请确认保存文件!", "保存更改", JOptionPane.YES_NO_CANCEL_OPTION);
if (res == JOptionPane.YES_OPTION) {
result = true;
}
return result;
}
public static void main(String[] args) {
new MyNotepad();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -