Java实现记事本|IO流/GUI
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);
}
}
Java实现记事本|IO流/GUI的更多相关文章
- java中的IO流
Java中的IO流 在之前的时候我已经接触过C#中的IO流,也就是说集中数据固化的方式之一,那么我们今天来说一下java中的IO流. 首先,我们学习IO流就是要对文件或目录进行一系列的操作,那么怎样操 ...
- 第55节:Java当中的IO流-时间api(下)-上
Java当中的IO流(下)-上 日期和时间 日期类:java.util.Date 系统时间: long time = System.currentTimeMillis(); public class ...
- 第54节:Java当中的IO流(中)
Java当中的IO流(中) 删除目录 // 简书作者:达叔小生 import java.io.File; public class Demo{ public static void main(Stri ...
- 第53节:Java当中的IO流(上)
Java当中的IO流 在Java中,字符串string可以用来操作文本数据内容,字符串缓冲区是什么呢?其实就是个容器,也是用来存储很多的数据类型的字符串,基本数据类型包装类的出现可以用来解决字符串和基 ...
- Java中的IO流总结
Java中的IO流总结 1. 流的继承关系,以及字节流和字符流. 2. 节点流FileOutputStream和FileInputStream和处理流BufferedInputStream和Buffe ...
- Java中的IO流大体介绍
由于Java中的IO流是在是知识点繁多,所以我大约花了1周的时间将其整理起来.但是整理起来后并不是将完事了,我还是要分字节流和字符流来讲述.然后字节流和字符流中还有是否带有缓冲流. 讲述完IO流后我将 ...
- Java中的IO流,Input和Output的用法,字节流和字符流的区别
Java中的IO流:就是内存与设备之间的输入和输出操作就成为IO操作,也就是IO流.内存中的数据持久化到设备上-------->输出(Output).把 硬盘上的数据读取到内存中,这种操作 成为 ...
- Java中的IO流(五)
上一篇<Java中的IO流(四)>记录了一下Properties类,此类不属于IO流,它属于集合框架.接下来说一下IO流中的其它流 一,打印流PrintStream PrintStream ...
- Java中的IO流(六)
上一篇<Java中的IO流(五)>把流中的打印流PrintStream,PrintWriter,序列流SequenceInputStream以及结合之前所记录的知识点完成了文件的切割与文件 ...
随机推荐
- oracle体系结构简介
oracle体系结构简介 一.物理存储结构 1.数据文件 存放数据库数据,以dbf为扩展名.将数据放在多个数据文件中, 再将数据文件分放在不同的硬盘中,可以提高存取速度. ...
- /dev/random 和 /dev/urandmon的差别
最近使用这两个设备的时候,发现 /dev/random生成随机数很慢:于是就查了查: 这两个设备的差异在于:/dev/random的random pool依赖于系统中断,因此在系统的中断数不足时,/d ...
- Octave中的矩阵常用操作2
sum(a):矩阵里的数据求和prod(a):乘积floor(a):向上取整ceil(a):向下取整max(A,[],1):取每一列的最大值max(A,[],2):取每一行的最大值max(max(A) ...
- 纯CSS实现自动轮播,CSS变量的定义与使用,计算属性的使用
先来看一下实现的效果: 实现原理: HTML中使用ul>li存放图片 CSS使用CSS3的animation来完成动画 <!-- HTML --> <section class ...
- qtdomdocument找不到
- 201871010114-李岩松《面向对象程序设计(java)》第十六周学习总结
项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...
- 201871010109-胡欢欢《面向对象程序设计(java)》第一周学习总结
<面向对象程序设计(java)>第一周学习总结 正文开头: 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 ...
- acwing 81. 扑克牌的顺子
地址 https://www.acwing.com/problem/content/77/ 从扑克牌中随机抽5张牌,判断是不是一个顺子,即这5张牌是不是连续的. 2-10为数字本身,A为1,J为11, ...
- 浅谈vue中的计算属性和侦听属性
计算属性 计算属性用于处理复杂的业务逻辑 计算属性具有依赖性,计算属性依赖 data中的初始值,只有当初始值改变的时候,计算属性才会再次计算 计算属性一般书写为一个函数,返回了一个值,这个值具有依赖性 ...
- POJ2001Shortest Prefixes(Trie树)
传送门 题目大意:求最短唯一前缀 题解:Trie树 把单词一个个插入,每个字母节点v[]++;然后输出时输出到v[]为1的点, v[]=1说明只有这个单词经过. 代码 : #include<io ...