您的位置:首页 > 编程语言 > Java开发

【java基础之程序】【一个简单的记事本程序】

2018-03-01 14:30 393 查看
package MyDemo;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class MyNotepad2 {
private static Frame notepad;							//窗口
private static Panel north;								//按钮栏
private static Button found;							//新建
private static Button hold;								//保存
private static Button close;							//清空
private static Button see;								//显示
private static Button help;								//使用说明
private static Panel center;							//文档栏
private static TextArea text;							//文本框
private static String str;								//使用说明
private static String texting;							//临时保存文本
private static String textName = "1.txt";				//文本名称
private static File f;									//保存的文本文件
private static FileWriter fw;							//写出文件流

public static void main(String[] args) {
notepad = new Frame("MyNotepad2");					//创建窗口
notepad.setVisible(true);							//设置窗口可见
notepad.setLocation(400, 100);						//设置窗口出现的位置
notepad.setSize(600, 550);							//设置窗口长宽
notepad.setResizable(false);						//设置窗口不可改变大小
notepad.addWindowListener(new WindowAdapter() {		//监听
public void windowClosing(WindowEvent e) {		//如果点击关闭按钮则退出JVM
exit();
}

});

text();												//文本框和按钮方法的调用
str();												//使用说明完成赋值
send();												//监听窗口方法
}

public static void send() {								//实现监听窗口方法
help.addActionListener(new ActionListener() {		//使用说明按钮被按下
public void actionPerformed(ActionEvent e) {
if(!(text.getText().equals(str)||text.getText().equals(""))) {//判断:如果文本是使用说明则不保存
texting = text.getText();				//保存被清空以前的文本
}
text.setText(str);
}
});

close.addActionListener(new ActionListener() {		//清空按钮被按下
public void actionPerformed(ActionEvent e) {
if(!(text.getText().equals(str)||text.getText().equals(""))) {//判断:如果文本是使用说明则不保存
texting = text.getText();				//保存被清空以前的文本
}
text.setText("");
}
});

see.addActionListener(new ActionListener() {		//显示按钮被按下
public void actionPerformed(ActionEvent e) {
text.setText(texting);
}
});

found.addActionListener(new ActionListener() {		//创建按钮被按下
public void actionPerformed(ActionEvent e) {
myNew();
}
});

hold.addActionListener(new ActionListener() {		//保存按钮被按下
public void actionPerformed(ActionEvent e) {
save();
}
});
}

private static void text() {							//文本框和按钮方法
north = new Panel();
found = new Button(" 新建 ");							//添加一个“新建”按钮
hold = new Button(" 保存 ");							//添加一个“保存”按钮
close = new Button(" 清空 ");							//添加一个“清空”按钮
see = new Button(" 显示 ");							//添加一个“显示”按钮
help = new Button("使用说明");							//添加一个“使用说明”按钮
north.add(found);
north.add(hold);
north.add(close);
north.add(see);
north.add(help);
notepad.add(north, BorderLayout.NORTH);//CENTER		//添加到窗口中

center = new Panel();
text = new TextArea(27,70);							//设置文本框大小
text.setFont(new Font("xxx", Font.PLAIN, 15));		//设置文本框文字大小
text.setBackground(Color.WHITE);					//设置文本框背景颜色
text.setForeground(Color.black);					//设置文本框文字颜色
center.add(text);
notepad.add(center, BorderLayout.CENTER);			//添加到窗口中
}

private static void exit() {							//退出JVM方法
if(f == null) {
f = new File(textName);
try {
fw = new FileWriter(f);
fw.write(text.getText(), 0, text.getText().length());
fw.flush();
} catch (IOException e1) {
e1.printStackTrace();
}finally {
try {
fw.close();
} catch (IOException e1) {

e1.printStackTrace();
}											//JVM退出前关闭流
}
}
if(f.length() == 0) {								//在JVM退出前判断文件是否为空文件,如果是空文件则删除
f.deleteOnExit();
}
System.exit(0);
}

private static void myNew() {							//创建按钮方法
String s = text.getText();							//获取文本框里面的内容
String ss = "文件名非法!请重新输入。\r\n如果不创建,默认创建1.txt\r\n请“清空”此文本!";
if(s.contains("\r\n") || s.contains("\n")) {		//下面判断创建的文件名是否有非法字符
text.setText("有回车键\r\n\r\n" + ss);
}else if(s.contains("<")) {
text.setText("有<字符\r\n\r\n" + ss);
}else if(s.contains(">")) {
text.setText("有>字符\r\n\r\n" + ss);
}else if(s.contains("/")) {
text.setText("有/字符\r\n\r\n" + ss);
}else if(s.contains("\\")) {
text.setText("有\\字符\r\n\r\n" + ss);
}else if(s.contains("?")) {
text.setText("有?字符\r\n\r\n" + ss);
}else if(s.contains("、")) {
text.setText("有、字符\r\n\r\n" + ss);
}else if(s.contains("*")) {
text.setText("有*字符\r\n\r\n" + ss);
}else if(s.contains("“")) {
text.setText("有“字符\r\n\r\n" + ss);
}else if(s.contains("”")) {
text.setText("有”字符\r\n\r\n" + ss);
}else if(s.length()<255?true:false) {
text.setText("超出255个字符\r\n\r\n" + ss);
}else {
textName = text.getText() + ".txt";
text.setText("创建" + textName + "成功!\r\n\r\n编写文本后再点击“保存”按钮即可保存此文本。\r\n\r\n点击清空按钮即可清除本内容。"
+ "\r\n\r\n(温馨提示:不清空此文本不推荐直接按“保存”!)");
}
}

private static void save() {							//保存按钮执行的方法
try {
f = new File(textName);
fw = new FileWriter(f);
fw.write(text.getText(), 0, text.getText().length());
fw.flush();

} catch (Exception e1) {

e1.printStackTrace();
}
}

private static void str() {								//使用说明赋值方法
str = "创建:在文本框输入新建文本的名称,当被点击创建按钮时被创建。请注意文件名称的命名规范!"
+ "\r\n命名规范:开头不能用空格,不能超过255个字符,命名不能有回车键、“?”、“、”、“\\”、“/”、“*”、“<”、“>”、“|”、“、”、“双引号本身”"
+ "\r\n打开:在文本框输入文本的名次,当被点击打开按钮时被打开。"
+ "\r\n保存:保存文本框所输入的文本。"
+ "\r\n清空:清空文本框里面的文本。"
+ "\r\n显示:返回清空前的文本。"
+ "\r\n使用说明:打开此文本。"
+ "\r\n(温馨提示:使用前清先清理此文本!)";
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息