Java实现记事本

题目

利用GUI实现一个简单的记事本(notepad),即打开文件,文字内容显示在界面上;

允许对文字内容进行编辑,并可以保存到文件。

代码

package notePadExp;

import java.awt.BorderLayout;
import java.awt.FileDialog;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException; import javax.swing.JButton;
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.JScrollPane;
import javax.swing.JTextArea; class notPadcontainer{ public Boolean visible = false;
//组件定义成属性
public JFrame notPadFrame;
public JMenuBar notPadMenuBar;
public JMenu firMenu;
public JMenu secMenu;
public JMenu thirMenu;
public JMenu fourMenu; public JMenuItem buildItem;
public JMenuItem openItem;
public JMenuItem reserveItem;
public JMenuItem paperSetItem;
public JMenuItem clearItem;
public JMenuItem aboutItem;
public JMenuItem fontItem20;
public JMenuItem fontItem40;
public JTextArea textArea;
public JScrollPane textScrollPane; /*
* 无参构造函数
* 创建组件 初始化组件
*/
notPadcontainer(){
//窗体Frame
this.notPadFrame = new JFrame("notePad by fishers"); //设置窗体 名字为notePad
this.notPadFrame.setLayout(new BorderLayout()); //边界布局方式
this.notPadFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置关闭框
this.notPadFrame.setSize(500,500); //设置窗口大小 //菜单组件
this.notPadMenuBar = new JMenuBar();
this.firMenu = new JMenu("文件");
this.thirMenu = new JMenu("字体");
this.secMenu = new JMenu("编辑");
this.fourMenu = new JMenu("帮助"); //create JMenuItem for the First menu
this.buildItem = new JMenuItem("新建");
this.openItem = new JMenuItem("打开");
this.reserveItem = new JMenuItem("保存");
this.paperSetItem = new JMenuItem("页面设置"); //create JMenuItem for the sec thir four menu
this.clearItem = new JMenuItem("清空");
this.aboutItem = new JMenuItem("关于");
this.fontItem20 = new JMenuItem("字体20号");
this.fontItem40 = new JMenuItem("字体40号");
//文本组件
this.textArea = new JTextArea();
this.textScrollPane = new JScrollPane(textArea);
textArea.setFont(new Font("宋体",Font.PLAIN,20)); //默认20号字体
ItemAdd();
runListener();
} //添加组件
public void ItemAdd(){ //添加JMenu到JMenuBar
notPadMenuBar.add(firMenu);
notPadMenuBar.add(secMenu);
notPadMenuBar.add(thirMenu);
notPadMenuBar.add(fourMenu); //添加JMenuItem到第一个菜单
firMenu.add(buildItem);
firMenu.add(openItem);
firMenu.add(reserveItem);
firMenu.add(paperSetItem);
secMenu.add(clearItem);
thirMenu.add(fontItem20);
thirMenu.add(fontItem40);
fourMenu.add(aboutItem); //notPadFrame中添加各个组件
this.notPadFrame.setJMenuBar(notPadMenuBar);
this.notPadFrame.add(textScrollPane,BorderLayout.CENTER);
} /*
* 事件监听代码部分
*/
public void runListener() {
//新建文件 = 清空。。
buildItem.addActionListener( e -> {
textArea.setText("");
}); //打开文件
openItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//设置弹出框 FileDialog
FileDialog saveFiledialog = new FileDialog(notPadFrame,"打开文件",FileDialog.LOAD);
saveFiledialog.setVisible(true);
String fileDir = saveFiledialog.getDirectory(); //拿到目录
String fileName = saveFiledialog.getFile(); //拿到文件名
// System.out.println(fileDir);
// System.out.println(fileName);
File openFile = new File(fileDir,fileName); //使用File类创建新文件对象
try {
FileReader freader = new FileReader(openFile); //字符流
StringBuffer tempBuffer = new StringBuffer();//StringBuffer可变
int len = 0; //下面使用read方法读取
while((len = freader.read()) != -1) {
tempBuffer.append((char)len); //append方法加入StringBuffer
}
String openString = new String(tempBuffer.toString());
textArea.setText(openString);
freader.close();//关闭流
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}); //保存文件
reserveItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
FileDialog saveFiledialog = new FileDialog(notPadFrame,"保存文件",FileDialog.SAVE);//父级frame 标题 mode
saveFiledialog.setVisible(true);
String fileDir = saveFiledialog.getDirectory();
String fileName = saveFiledialog.getFile();
System.out.println(fileDir);
System.out.println(fileName);
String ContentString = textArea.getText();
File saveFile = new File(fileDir,fileName);
try {
FileWriter fWriter = new FileWriter(saveFile); //使用字符流写文件
fWriter.write(ContentString); //写文件
fWriter.close(); //关闭流
}catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}); //清空文件
clearItem.addActionListener( e -> {
textArea.setText("");
}); //监听关于
aboutItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JFrame aboutFrame = new JFrame("关于"); //新建窗口
aboutFrame.setLayout(new BorderLayout());
aboutFrame.setSize(300,115);
aboutFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);//设置隐藏窗口 JPanel panel = new JPanel();
JLabel label = new JLabel("不会换行、中文乱码的记事本");
JPanel panel2 = new JPanel();
JLabel label2 = new JLabel("Copyright © 2019 fishers");
panel.add(label);
panel2.add(label2);
aboutFrame.add(panel,BorderLayout.CENTER);
aboutFrame.add(panel2,BorderLayout.PAGE_START);
aboutFrame.setVisible(true);
}
}); fontItem20.addActionListener(e->{
textArea.setFont(new Font("宋体",Font.PLAIN,20));
}); fontItem40.addActionListener(e->{
textArea.setFont(new Font("宋体",Font.PLAIN,40));
});
} //setVisible:设置窗口显示
public void setVisible(Boolean visible) {
this.visible = visible;
this.notPadFrame.setVisible(this.visible);
}
} public class notePad { public static void main(String[] args) {
notPadcontainer oneNote = new notPadcontainer();
oneNote.setVisible(true);
} }

最新文章

  1. 洗牌算法Fisher_Yates原理
  2. Util应用程序框架公共操作类(四):验证公共操作类
  3. linux arch目录下处理器体系架构介绍
  4. LintCode Validate Binary Search Tree
  5. MAC电脑操作快捷键
  6. 绕过D盾安全狗连接菜刀
  7. emlog模板制作帮助手册
  8. 检测浏览器对HTML5和CSS3支持情况的利器——Modernizr
  9. 编译 Spring-framework的经验分享
  10. ASP.net:截取固定长度字符串显示在页面,多余部分显示为省略号
  11. Linux 系统之Sysvinit
  12. python--tile函数
  13. Giraph入门
  14. box-shadow阴影详解
  15. Qt5构建出错问题解决办法
  16. 201521123060 《Java程序设计》第6周学习总结
  17. CMake 笔记
  18. python--第二十四天总结
  19. LeetCode(47):全排列 II
  20. window下rabbitmq环境安装

热门文章

  1. React 语法基础(一)之表达式和jsx
  2. deepin系统右键刷新-解决增删改文件没有变化
  3. AcWing 836. 合并集合
  4. SSM 实现登录注册功能
  5. 2019南昌网络赛H The Nth Item(打表找询问循环节 or 分段打表)
  6. 【洛谷5368】[PKUSC2018] 真实排名(组合数学)
  7. python进阶之内存模型
  8. Python程序中的线程操作-锁
  9. 【文本处理命令】之grep搜索命令详解
  10. springboot实践1