import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.text.Normalizer;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Notepad extends JFrame{
//定义文件名
private static String fileName = null;
//文件内容
private static String fileContent = null;
//定义选择栏(从上到下)
private JMenuBar jMenuBar = new JMenuBar();
//定义文件菜单
private JMenu jMenu_file= new JMenu("文件");
//定义文件菜单中的子选项
private JMenuItem jMenuItem1 = null;
private JMenuItem jMenuItem2 = null;
private JMenuItem jMenuItem3 = null;
private JMenuItem jMenuItem4 = null;
//定义格式菜单
private JMenu jMenu_eidt= new JMenu("格式");
//定义格式菜单中的子选项
private JMenuItem jMenuItem5 = null;
//定义帮助菜单
private JMenu jMenu_help= new JMenu("帮助");
//定义帮助菜单中的子选项
private JMenuItem jMenuItem6 = null;
//定义文本域
private JTextArea jTextArea = new JTextArea();
//定义监听类对象
private MyActionListener myActionListener = new MyActionListener();
//定义并实例化一个文件选择框
private JFileChooser jFileChooser = new JFileChooser("F:\\");;
//定义文件输入流
private InputStreamReader inputStreamReader = null;
//定义字符输入流缓冲区
private BufferedReader bufferedReader = null;
//定义文件输出流
private FileOutputStream fileOutputStream = null;
private OutputStreamWriter outputStreamWriter =null;
//判断自动换行是否激活
private boolean lineFeed = false;
//定义滚动条
private JScrollPane jScrollPane = null;
//构造函数
public Notepad() {
super("记事本");
this.loadNorth();
this.init();
}
//初始化函数
private void init() {
//设置整体基本布局
//添加Windows窗口关闭监听事件
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
Notepad.this.exit();
}
});
setSize(new Dimension(800,600));
setLocationRelativeTo(null);
//添加菜单栏
setJMenuBar(jMenuBar);
//实例化滚动条对象,并添加垂直,水平滚动条(在需要的时候)
jScrollPane = new JScrollPane();
jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
//添加滚动条只能用setViewportView()
jScrollPane.setViewportView(jTextArea);
//添加文本域
add(jScrollPane);
setVisible(true);
}
//添加顶部分控件(菜单栏)
//布局设置函数
private void loadNorth() {
jMenuItem1 = new JMenuItem("新建");
jMenuItem2 = new JMenuItem("打开");
jMenuItem3 = new JMenuItem("保存");
jMenuItem4 = new JMenuItem("退出");
jMenuItem5 = new JMenuItem("自动换行");
jMenuItem6 = new JMenuItem("关于记事本");
jMenu_file.add(jMenuItem1);
jMenu_file.add(jMenuItem2);
jMenu_file.add(jMenuItem3);
jMenu_file.add(jMenuItem4);
jMenu_eidt.add(jMenuItem5);
jMenu_help.add(jMenuItem6);
jMenuBar.add(jMenu_file);
jMenuBar.add(jMenu_eidt);
jMenuBar.add(jMenu_help);
jMenuBar.setVisible(true);
//为选项添加监听事件
jMenuItem1.addActionListener(myActionListener);
jMenuItem2.addActionListener(myActionListener);
jMenuItem3.addActionListener(myActionListener);
jMenuItem4.addActionListener(myActionListener);
jMenuItem5.addActionListener(myActionListener);
jMenuItem6.addActionListener(myActionListener);
}
//设置监听事件内部类
private class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
String strClick = e.getActionCommand();
switch (strClick) {
case "新建":{
//新建条件1:如果文本区域为空,并且名字也为空(表示是一个空白的文件)或者已经有打开的文件,文件内容和文本区域内容一致则表示文件只是打开了但没有修改无需保存,直接初始化
if (Notepad.fileName == null && Notepad.this.jTextArea.getText().equals("")
|| Notepad.this.jTextArea.getText().equals(Notepad.fileContent)) {
System.out.println("新建1");
JOptionPane.showMessageDialog(null, "已新建!");
}
//新建条件2:①:文件为新建文件,但是有内容,②:文件为打开的文件,但是修改过
else {
System.out.println("新建2");
int answer = JOptionPane.showConfirmDialog(null, "是否保存?","温馨提示",JOptionPane.YES_NO_OPTION);
switch (answer) {
case 0:{
Notepad.this.saveFileByChooser();
break;
}
case 1:
break;
default:
break;
}
}
//将所有参数初始化
Notepad.fileName = null;
Notepad.fileContent = null;
Notepad.this.jTextArea.setText("");
Notepad.this.setTitle("新建 - 记事本");
System.out.println("新建");
break;
}
case "打开":{
//打开条件1:如果文本区域为空,并且名字也为空(表示是一个空白的文件),此时直接打开文件选择框就行
if (Notepad.fileName == null && Notepad.this.jTextArea.getText().equals("")) {
System.out.println("打开1");
openFileByChooser();
}
//打开条件2:如果已经打开的文件,文件内容和文本区域内容一致则表示文件只是打开了但没有修改无需保存,次直接打开文件选择框
else if (Notepad.this.jTextArea.getText().equals(Notepad.fileContent)) {
System.out.println("打开2");
Notepad.this.openFileByChooser();
}
//打开条件3:①:文件为新建文件,但是有内容,②:文件为打开的文件,但是修改过
else {
System.out.println("打开3");
int answer = JOptionPane.showConfirmDialog(null, "是否保存?","温馨提示",JOptionPane.YES_NO_OPTION);
switch (answer) {
case 0:
Notepad.this.saveFileByChooser();
openFileByChooser();
break;
case 1:
openFileByChooser();
break;
default:
break;
}
}
System.out.println("打开");
break;
}
case "保存":{
Notepad.this.saveFileByChooser();
//更新文件内容
Notepad.this.setTitle(Notepad.fileName + " - 记事本");
System.out.println("保存");
break;
}
case "退出":{
Notepad.this.exit();
System.out.println("退出");
break;
}
case "自动换行":{
Notepad.this.autoWrap();
System.out.println("自动换行" + Notepad.this.lineFeed);
break;
}
case "关于记事本":{
System.out.println("关于记事本");
String message = "记事本主要功能:\n"
+ "1.对文件的新建、打开、保存。\n"
+ "2.能够设置文本的自动换行\n"
+ "3.自动设置水平和垂直滚动条\n"
+ "4.能够按照ASCII字符编码(0-255)加密,加密时对每一字符+10,(若超过255,减去255),解密时作对应反变换。\n"
+"创作者:计科2003班--温明祥--5120205799";
JOption
没有合适的资源?快使用搜索试试~ 我知道了~
西南科技大学Java程序设计与实践 实验五 IO实验--记事本
![preview](https://csdnimg.cn/release/downloadcmsfe/public/img/white-bg.ca8570fa.png)
共3个文件
txt:2个
doc:1个
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
![star](https://csdnimg.cn/release/downloadcmsfe/public/img/star.98a08eaa.png)
温馨提示
西南科技大学Java程序设计与实践 实验五 IO实验--记事本 一、实验目的:练习输入/输出流的创建、转换及数据访问设计方法。 二、实验内容:设计一个类似Windows记事本的Application应用程序。 三、实验要求:1、采用Java Application方式建立应用程序框架;2、仿照Windows记事本,实现界面设计;3、完成菜单(新建、打开、保存)设计及事件设计。完成以上内容选做选做:设定循环加解密规则如下:按照ASCII字符编码(0-255),加密时对每一字符+10,(若超过255,减去255),解密时作对应反变换。编程实现文件的“新建”、“打开”(将加密文本文件“D:\1.txt”解密后打开)和“保存”(将新建或打开并修改过的文件加密保存至“D:\1.txt”中)操作。 四、实验报告内容要求:1、简要说明记事本图形用户界面的设计过程;2、深入分析各功能菜单的工作流程;3、完整介绍各功能菜单的具体设计与实现过程。4、简要说明你在该实验中的收获与体会。
资源推荐
资源详情
资源评论
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![thumb](https://img-home.csdnimg.cn/images/20210720083646.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![thumb](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/DOC.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/TXT.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/TXT.png)
共 3 条
- 1
![avatar](https://profile-avatar.csdnimg.cn/75f7987766da44aa941d67bfba886e3a_qq_29711355.jpg!1)
无奈清风吹过
- 粉丝: 432
- 资源: 24
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)
- 1
- 2
- 3
- 4
前往页