突发奇想,手机有便条功能,那么为什么电脑上米有这个功能呢?【可能有 但是我不知道...】
于是在JAVA课程中,正好做一下这个东西
感觉里面最难的是界面设计,因为之前真的没有接触过这个
软件名称为:AssistantReina
首先是主界面,Interface.java
package Base;
import java.util.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.*;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.print.DocFlavor.READER;
import javax.swing.*;
import Base.NoteReina;
class NEW extends JFrame implements ActionListener{
JFrame jf = new JFrame("新建备忘录");
JPanel jp = new JPanel();
JButton save = new JButton("保存");
JButton cancel = new JButton("取消");
JTextField jtf = new JTextField();
JTextArea jtf2 = new JTextArea("请在此处输入内容",17,30);
JLabel jl = new JLabel("标题");
NEW (){
jtf2.setLineWrap(true);
JScrollPane jsp1 = new JScrollPane(jtf2);
jsp1.setBounds(70, 70, 250, 200);
jsp1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
jsp1.getViewport().add(jtf2);
jp.setBounds(40,70,370,320);
jp.setVisible(true);
jsp1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
//jp.setBackground(Color.blue);
jp.add(jsp1);
jf.add(jp);
jf.setLayout(null);
jf.setVisible(true);
jf.setBounds(0, 0, 450, 500);
jf.setLocationRelativeTo(null);
jf.setResizable(false);
jtf.setBounds(70,20,350,35);
jl.setBounds(20,20,50,35);
//jtf2.setBounds(40, 90, 360, 270);
save.setBounds(70,420,120,30);
cancel.setBounds(265, 420, 120, 30);
jf.add(jl);
save.addActionListener(this);
cancel.addActionListener(this);
jf.add(save);
jf.add(cancel);
jf.add(jtf);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
NEW (String filename){
System.out.println("D://AssistanceReina/" + filename + ".txt");
File fileread = new File("D://AssistanceReina/" + filename + ".txt");
String text_trim = "";
String text = "";
try{
BufferedReader reader = new BufferedReader(new FileReader(fileread));
while ((text_trim = reader.readLine())!=null){
text += (text_trim + "\n");
}
} catch (IOException e){
e.printStackTrace();
}
JTextArea jtf2 = new JTextArea(17, 30);
jtf2.setLineWrap(true);
jtf2.append(text);
JScrollPane jsp1 = new JScrollPane(jtf2);
jsp1.setBounds(70, 70, 250, 200);
jsp1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
jsp1.getViewport().add(jtf2);
jp.setBounds(40,70,370,320);
jp.setVisible(true);
jsp1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
//jp.setBackground(Color.blue);
jp.add(jsp1);
jf.add(jp);
jf.setLayout(null);
jf.setVisible(true);
jf.setBounds(0, 0, 450, 500);
jf.setLocationRelativeTo(null);
jf.setResizable(false);
jtf.setBounds(70,20,350,35);
jl.setBounds(20,20,50,35);
//jtf2.setBounds(40, 90, 360, 270);
save.setBounds(70,420,120,30);
cancel.setBounds(265, 420, 120, 30);
jf.add(jl);
save.addActionListener(this);
cancel.addActionListener(this);
jf.add(save);
jf.add(cancel);
jf.add(jtf);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
if (e.getSource() == cancel){
jf.dispose();
}
if (e.getSource() == save){
//System.out.println(jtf.getText());
//BufferedWriter fw1 = new BufferedWriter();
String str = jtf2.getText();
//System.out.println(str);
FileOperate fo = new FileOperate();
try {
fo.CreateNote(str);
} catch (IOException e1) {
e1.printStackTrace();
}
jf.dispose();
}
}
}
class InterfaceReina extends JFrame implements ActionListener{
JFrame jf_main = new JFrame("Reina");
JPanel jp1 = new JPanel();
JLabel jl_wel = new JLabel("Welcome To AssistanceReina",JLabel.CENTER);
JButton b1 = new JButton("新建");
JButton b2 = new JButton("删除");
JButton b3 = new JButton("编辑 / 查看");
JList jl1 = new JList();
InterfaceReina() throws IOException{
File file1 = new File("D://AssistanceReina");
File[] tempList = file1.listFiles();
int len = tempList.length;
String [] list_name = new String [len];
String [] list_name_on = new String [len];
System.out.println("num:"+tempList.length);
for (int i = 0; i < tempList.length; ++i){
if (tempList[i].isFile()){
String str_name = tempList[i].getName();
list_name[i] = str_name;
list_name_on[i] = str_name;
String repstr = ".txt";
list_name_on[i] = list_name[i].substring(0, 19);
//System.out.println(list_name[i]);
}
}
if (len == 1)
list_name_on[0] = "您还没有创建备忘录";
jl1 = new JList(list_name_on);
jl1.setFixedCellWidth(200);
JScrollPane jsp1 = new JScrollPane(jl1);
jsp1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
jsp1.getViewport().add(jl1);
jp1.setBounds(300,150,300,250);
jp1.setVisible(true);
jp1.add(jsp1);
jsp1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jf_main.add(jp1);
jf_main.setLayout(null);
jf_main.setVisible(true);
jf_main.setBounds(0, 0, 600, 400);
jf_main.setLocationRelativeTo(null);
jf_main.setResizable(false);
jl_wel.setBounds(0, 0, 600, 100);
jl_wel.setBorder(BorderFactory.createLineBorder(Color.RED));
jf_main.add(jl_wel);
jf_main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
//jl1.addMouseListener(this);
b1.setBounds(30, 130, 250, 50);
b2.setBounds(30, 210, 250, 50);
b3.setBounds(30, 290, 250, 50);
jf_main.add(b1);
jf_main.add(b2);
jf_main.add(b3);
}
public void actionPerformed(ActionEvent e){
if (e.getSource() == b1){
NEW a = new NEW();
}
else if (e.getSource() == b2){
Object obj = jl1.getSelectedValue();
String str_del = (String)obj;
//System.out.println(str_del);
String str1 = "D://AssistanceReina/" + str_del + ".txt";
System.out.println(str1);
File file1 = new File(str1);
file1.delete();
}
else if (e.getSource() == b3){
Object obj = jl1.getSelectedValue();
String str_edt = (String)obj;
System.out.println(str_edt);
NEW a = new NEW(str_edt);
}
}
}
public class Interface{
public static void main(String[] args) throws IOException{
InterfaceReina iff = new InterfaceReina();
}
}
然后是NoteReina.java
设计每个备忘录的属性
package Base;
import java.text.SimpleDateFormat;
import java.util.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
public class NoteReina {
/**
* 属性:
* 文件编号(创建时间)
* 文件别名(用户自定义或者空)
* 文件总共编辑时间(小时、分钟、秒数、毫秒)
*
* 方法:
* 新建(可以输入别名也可以不输入)
* 删除(可以按照编号和别名来删除)
* 修改(修改现有的文件内容)
*/
String id;
String name;
double hours, minutes, seconds, ms;
NoteReina() throws IOException{
Date date_file1 = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss");
String str_now = formatter.format(date_file1);
this.id = str_now;
this.name = "(null)";
File file1 = new File("D://AssistanceReina");
if (!file1.exists()){
File file2 = new File(file1, "config");
file2.mkdirs();
File file4 = new File(file2, "config.txt");
file4.createNewFile();
str_now += ".txt";
File file3 = new File(file1, str_now);
file3.createNewFile();
}
else{
str_now += ".txt";
File file3 = new File(file1, str_now);
file3.createNewFile();
}
}
NoteReina(String nameNew) throws IOException{
Date date_file1 = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss");
String str_now = formatter.format(date_file1);
this.id = str_now;
this.name = nameNew;
File file1 = new File("D://AssistanceReina");
if (!file1.exists()){
File file2 = new File(file1, "config");
file2.mkdirs();
File file4 = new File(file2, "config.txt");
file4.createNewFile();
str_now += ".txt";
File file3 = new File(file1, str_now);
file3.createNewFile();
String write = str_now + " " +nameNew + "\r\n";
FileWriter fw1 = new FileWriter(file4, true);
fw1.write(write);
fw1.flush();
fw1.close();
}
else{
str_now += ".txt";
File file3 = new File(file1, str_now);
file3.createNewFile();
File file2 = new File(file1, "config");
File file4 = new File(file2, "config.txt");
String write = str_now + " " +nameNew + "\r\n";
FileWriter fw1 = new FileWriter(file4, true);
fw1.write(write);
fw1.flush();
fw1.close();
}
}
void DeleteReina(String nameDel) throws IOException{
String str1 = "D://AssistanceReina/" + nameDel + ".txt";
File file1 = new File("D://AssistanceReina/" + nameDel + ".txt");
file1.delete();
FileReader fr = new FileReader(str1);
BufferedReader br = new BufferedReader(fr);
String str2 = br.readLine();
while (str1 != null){
//do something
str2 = br.readLine();
}
}
public static void main(String [] args) throws IOException{
NoteReina r = new NoteReina("12345");
//r.DeleteReina("2018_05_08-17_04_33");
}
}
有些文件操作后来发现没有加入,于是又新建类来单独解决
FileOperate.java
package Base;
import java.io.*;
//import java.io.File;
//import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
public class FileOperate {
void CreateNote(String text) throws IOException{
File file1 = new File("D://AssistanceReina");
if (!file1.exists())
file1.mkdir();
Date date_file1 = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss");
String str_now = formatter.format(date_file1);
str_now += ".txt";
File file2 = new File(file1, str_now);
file2.createNewFile();
FileWriter fw = new FileWriter(file2);
fw.write(text);
fw.flush();
fw.close();
}
void DeleteNote(String FileName){
File file1 = new File("D://AssistanceReina/" + FileName);
file1.delete();
}
public static void main(String [] args) throws IOException{
FileOperate fo = new FileOperate();
fo.CreateNote("123");
//fo.DeleteNote("2018_05_06-16_18_13.txt");
//NoteReina r = new NoteReina();
}
}
本来想在主界面加个图片,时间太紧没来得及制作,顺便这个标题功能也没做,