日历记事本(javafx)
一次作业而已。
系统界面
图3.1 登陆界面
图3.2 注册界面
图3.3 主界面
图3.4 添加日志界面
设计思路
对于这个日历记事本我一共用了六个类,分别是Main:主类,DealText:文件处理类,Information:信息处理类,User:用户类,InterFace:主界面类,Time:时间类。
Main:主要是启动程序,打开登陆界面。
User:主要是管理用户的登陆与注册,是进入主界面的必经之路。
InterFace:对于日历和日志的显示,用户可以在这里对日志进行增删改查,也可以通过调整年月份来查看日历,还可以通过下方的时间显示器查看当前时间。
DealText:处理主界面传来的文本操作,对内部的文本信息进行修改和对为其他的类提供相应的文本操作方法。
Information:对于信息的查询,对于各个界面上的消息提示提供方法。
Time:提供创建日历和修改日历的方法,提供当前时间,当前的日期。
模块详解
登陆注册
设计思路:
登陆和注册功能在User类中实现,通过用户在文本输入框中输入的 信息,调用Information类中的方法,查询是否可以登陆或者注册,若是允许登陆,就弹出提示框,进入主界面。注册完成之后也会弹出提示框,回到登录界面,登陆成功之后会将用户名传到主界面类中,方便主界面的使用。
代码:
package NewNote;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.ParseException;
import java.util.Scanner;
import javafx.stage.Stage;
public class User {
private Information information = new Information();
private DealText dealtext = new DealText();
private InterFace IF = new InterFace();
//登陆
public void Login(String userName, String password) throws IOException, ParseException {
if (information.inquire_login(userName, password)) {
System.out.println("登陆成功,进入主界面");
information.tips_login1();
IF.showMain(userName);
} else {
System.out.println("登陆失败");
information.tips_login2();
}
}
//注册
public void Register(String userName, String password, String Rpassword) throws IOException {
if (information.inquire_register(userName)) {
dealtext.register(userName + "#PC#" + password);
System.out.println("注册成功,请登陆");
information.tips_register1();
IF.showRegister();
} else {
System.out.println("注册失败");
information.tips_register2();
}
}
}
信息查询和提示
设计思路:
通过从TextDeal类中获取相关文本内容,再通过相应的方法将原始的数据分割,处理,计算,再通过相应的方法将处理好的内容返回。通过Alert类创建多个提示窗口,在该提示的地方使用他。
代码:
package NewNote;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import New.Time;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.DialogEvent;
public class Information {
private DealText dealtext = new DealText();
// 相关信息提示窗口
private Alert alert = new Alert(AlertType.INFORMATION);
// 返回用户所查询的列表
private List<String> list = new ArrayList<String>();
// 查询是否能登陆
public boolean inquire_login(String userName, String password) throws FileNotFoundException {
for (String[] massage : dealtext.getFile(dealtext.getFileUser())) {
if (massage[0].equals(userName) && massage[1].equals(password)) {
return true;
}
}
return false;
}
// 查询是否能注册
public boolean inquire_register(String userName) throws FileNotFoundException {
for (String[] massage : dealtext.getFile(dealtext.getFileMassage())) {
if (massage[0].equals(userName)) {
return false;
}
}
return true;
}
// 返回用户该日期下的日志
public List<String> returnDayMassage(String date, String user) throws FileNotFoundException {
list.clear();
for (String[] massage : dealtext.getFile(dealtext.getFileMassage())) {
if (massage[0].equals(date) && massage[2].equals(user)) {
list.add(massage[1] + "#PC#" + massage[3]);
}
}
if (list.size() == 0) {
System.out.println("日志为空");
}
return list;
}
// 返回用户该月份下的日志
public List<String> returnMonthMassage(String date, String user) throws FileNotFoundException {
list.clear();
System.out.println(date + " " + user);
for (String[] massage : dealtext.getFile(dealtext.getFileMassage())) {
System.out.println();
if (massage[0].contains(date) && massage[2].equals(user)) {
list.add(massage[1] + "#PC#" + massage[3]);
}
}
if (list.size() == 0) {
System.out.println("日志为空");
}
return list;
}
// 返回所有信息
public String returnAllMassage(String date, String user) throws FileNotFoundException {
String text = "";
System.out.println(date + " " + user);
for (String[] massage : dealtext.getFile(dealtext.getFileMassage())) {
System.out.println(massage[0] + " " + massage[2]);
if (massage[0].contains(date) && massage[2].equals(user)) {
System.out.println(massage[0] + massage[1] + ": " + massage[3] + "\n");
text = text + massage[0] + massage[1] + ": " + massage[3] + "\n";
}
}
return text;
}
// 信息保存成功提示
public void tips_save1() {
alert.setTitle("保存日志");
alert.setContentText("成功保存日志");
alert.showAndWait();
}
// 信息删除保存成功提示
public void tips_delete1() {
alert.setTitle("删除日志");
alert.setContentText("成功删除日志");
alert.showAndWait();
}
// 信息删除失败提示
public void tips_delete2() {
alert.setTitle("删除日志");
alert.setContentText("删除日志失败˙");
alert.showAndWait();
}
// 信息读取成功提示
public void tips_read1() {
alert.setTitle("读取日志");
alert.setContentText("成功读取日志");
alert.showAndWait();
}
// 信息读取失败提示
public void tips_read2() {
alert.setTitle("读取日志");
alert.setContentText("日志不存在,读取日志失败!");
alert.showAndWait();
}
// 登陆成功提示
public void tips_login1() {
alert.setTitle("登陆");
alert.setContentText("登陆成功,欢迎您!");
alert.showAndWait();
}
// 登陆失败提示
public void tips_login2() {
alert.setTitle("登陆");
alert.setContentText("登陆失败!请重试");
alert.showAndWait();
}
// 注册成功提示
public void tips_register1() {
alert.setTitle("注册");
alert.setContentText("注册成功!请登陆");
alert.showAndWait();
}
// 注册失败提示
public void tips_register2() {
alert.setTitle("注册");
alert.setContentText("注册失败!用户名已存在");
alert.showAndWait();
}
// 密码输入错误提示
public void tips_passowrd() {
alert.setTitle("密码");
alert.setContentText("两次输入的密码不一致");
alert.showAndWait();
}
// 密码输入为空提示
public void tips_null() {
alert.setTitle("错误");
alert.setContentText("输入不能为空");
alert.showAndWait();
}
// 闹钟添加成功提示
public void tips_clock1() {
alert.setTitle("闹钟");
alert.setContentText("添加成功");
alert.showAndWait();
}
}
文本处理
设计思路:
通过两条文件路径和BufferedWriter,BufferedReader两个类读取和写入有关文本,来达到有关文本的一切操作。直接的实现是读和写,删除的是现实先将文本里的内容取出,通过主界面给的指令进行删除操作,再将文本内容清空,再将操作完的文本内容存入文本中。删除的设计也是通过这种方法实现的。
代码:
package NewNote;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;
import javafx.collections.ObservableList;
public class DealText {
//两条文件路径
private String fileUser = "src/NewNote/user.txt";
private String fileMassage = "src/NewNote/text.txt";
//事件内容列表
private static ObservableList<String> strList;
private static List<String> list = new ArrayList<String>();
public ObservableList<String> getStrList() {
return strList;
}
public void setStrList(ObservableList<String> strList) {
this.strList = strList;
}
public void setList(List<String> list) {
this.list = list;
}
public List<String> getList() {
System.out.println("list储存" + list.size());
return list;
}
public String getFileUser() {
return fileUser;
}
public void setFileUser(String fileUser) {
this.fileUser = fileUser;
}
public String getFileMassage() {
return fileMassage;
}
public void setFileMassage(String fileMassage) {
this.fileMassage = fileMassage;
}
// 用户注册信息内容写入
public void register(String massage) throws FileNotFoundException {
try {
System.out.println("文件编辑");
BufferedWriter filewrite = new BufferedWriter(new FileWriter(fileUser, true));
filewrite.write(massage);
filewrite.newLine();
filewrite.flush();
filewrite.close();
System.out.println("添加成功");
} catch (FileNotFoundException e) {
throw e;
} catch (IOException e) {
System.out.println("read异常");
}
}
// 用户事件信息内容写入
public void addMassage(String massage) throws IOException {
try {
System.out.println(massage);
System.out.println("添加文件信息");
BufferedWriter filewrite = new BufferedWriter(new FileWriter(fileMassage, true));
filewrite.write(massage);
filewrite.newLine();
filewrite.flush();
filewrite.close();
System.out.println("添加成功");
} catch (FileNotFoundException e) {
throw e;
} catch (IOException e) {
System.out.println("add异常");
}
}
// 删除用户该天的事件
public void deleteMassage(List<String> data, String userName) throws FileNotFoundException {
try {
String line;
List<String> list = new ArrayList<String>();
// 将字节流变为字符流
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileMassage)));
while ((line = reader.readLine()) != null) {
list.add(line);
}
for (String dd : data) {
for (int i = 0; i < list.size(); i++) {
String[] ddd = dd.split("#PC#");
if (list.get(i).contains(ddd[0]) && list.get(i).contains(userName)
&& list.get(i).contains(ddd[1])) {
list.remove(list.get(i));
break;
}
}
}
BufferedWriter filewrite1 = new BufferedWriter(new FileWriter(fileMassage));
filewrite1.write("");
BufferedWriter filewrite2 = new BufferedWriter(new FileWriter(fileMassage, true));
for (String massage : list) {
filewrite2.write(massage);
filewrite2.newLine();
filewrite2.flush();
}
filewrite2.close();
System.out.println("删除成功");
} catch (FileNotFoundException e) {
throw e;
} catch (IOException e) {
System.out.println("delete异常");
}
}
// 修改用户该天的事件
public void changeMassage(String data, String data2) throws FileNotFoundException {
try {
String str = null;
String line;
List<String> list = new ArrayList<String>();
// 将字节流变为字符流
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileMassage)));
while ((line = reader.readLine()) != null) {
list.add(line);
}
for (int i = 0; i < list.size(); i++) {
if (list.get(i).contains(data)) {
String[] ss = list.get(i).split("#PC#");
ss[3] = data2;
list.remove(list.get(i));
for (int j = 0; i < ss.length - 1; i++)
str = ss[j] + "#PC#";
str += ss[ss.length - 1];
list.add(str);
break;
}
}
BufferedWriter filewrite1 = new BufferedWriter(new FileWriter(fileMassage));
filewrite1.write("");
BufferedWriter filewrite2 = new BufferedWriter(new FileWriter(fileMassage, true));
for (String massage : list) {
filewrite2.write(massage);
filewrite2.newLine();
filewrite2.flush();
}
filewrite2.close();
} catch (FileNotFoundException e) {
throw e;
} catch (IOException e) {
System.out.println("delete异常");
}
}
// 内容读取
public List<String[]> getFile(String file) throws FileNotFoundException {
String data[] = null;
String line;
List<String[]> list = new ArrayList<String[]>();
try {
// 将字节流变为字符流
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
while ((line = reader.readLine()) != null) {
data = line.split("#PC#");
list.add(data);
}
} catch (FileNotFoundException e) {
throw e;
} catch (IOException e) {
System.out.println("read异常");
}
return list;
}
}
时间处理
设计思路:
通过Java现有的LocalDateTime类可以直接得到现在的具体时间,在通过不同的格式划分将他们分成三个方法,一是得到现有的时间(具体到时分秒),二是得到日期,三是得到时分秒。通过这三个方法系统可以得到用户储存日志的时间。
通过Java现有的Calendar类可以得到当前的年份,月份,当前月份的最大天数, 当前日子是周几。建一个Button[]通过设置按钮的文本内容表示日历的日期。每当 点击按钮,日志显示列表的标题就回被设置成当前日期,并且日志显示列表中的内 容会被替换成这个日期的日志。
通过Timer,MediaPlayer实现闹钟的功能,Timer负责计时,当时间到了,就会放 音乐,此时会弹出一个窗口,点击按钮就可以关闭闹钟。
代码:
package NewNote;
import java.io.File;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JOptionPane;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.util.Duration;
public class Time {
private DealText dealtext = new DealText();
private Information information = new Information();
//多媒体播放器
public static Media media = new Media(Time.class.getResource("zfx.mp3").toString());
//音乐播放器
public static MediaPlayer player = new MediaPlayer(media);
private Calendar cal;
private InterFace in;
private static GridPane Gpane;
private static int year, month, day, hour, minute, second;
//初始化Time类
public Time(InterFace in) {
super();
// TODO Auto-generated constructor stub
cal = new GregorianCalendar();
year = cal.get(cal.YEAR);
month = cal.get(cal.MONTH) + 1;
this.in = in;
}
//定时器
public void clock(String a) throws ParseException {
Timer timer = new Timer();
timer.schedule(new MyTask(), time_difference(a) * 1000);
}
//闹钟运行
public class MyTask extends TimerTask {
@Override
public void run() {
System.out.println("时辰已到,该干活了");
player.play();
int n = JOptionPane.showConfirmDialog(null, "闹钟响了,是否关闭?", "闹钟", JOptionPane.YES_OPTION);
if (n == 0 ) {
Time.player.stop();
}
}
}
// 比较两个时间的先后
public Boolean Compare_time(String clocktime) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date bt = sdf.parse(getNowTime());
Date et = sdf.parse(clocktime);
if (bt.before(et)) {
return true;
}
return false;
}
/*
* 计算两个日期相差的秒数,按秒输出
*/
public int time_difference(String b) throws ParseException {
String a = getNowTime();
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date startDate = sf.parse(a);
Date endDate = sf.parse(b);
long x = endDate.getTime();
long y = startDate.getTime();
int c = (int) ((x - y) / 1000);
System.out.println(c);
return c;
}
public Calendar getCal() {
return cal;
}
public void setCal(Calendar cal) {
this.cal = cal;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
//得到现在的日期
public String getNowDay() {
LocalDateTime dateTime = LocalDateTime.now(); // get the current date and time
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
return dateTime.format(formatter);
}
//得到现在的时间
public String getNowFtime() {
LocalDateTime dateTime = LocalDateTime.now(); // get the current date and time
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
return dateTime.format(formatter);
}
// 获取系统当前时间
public String getNowTime() {
LocalDateTime dateTime = LocalDateTime.now(); // get the current date and time
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
return dateTime.format(formatter);
}
//重置日历
public void setCalendar(int year, int month, AnchorPane Pane_MI_pane, Label label, String userName) {
Pane_MI_pane.getChildren().remove(Gpane);
Gpane = new GridPane();
this.year = year;
this.month = month;
this.cal = new GregorianCalendar(year, month - 1, 1);
Button[] button = new Button[40];
int number = 1;
int daymax = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
for (int i = 1; i <= daymax; i++) {
int dayofweek = cal.get(Calendar.DAY_OF_WEEK) - 1;
button[i] = new Button(String.valueOf(i));
button[i].setStyle(
"-fx-background-color:null;-fx-background-insets: 0; -fx-pref-width: 40px;-fx-pref-height: 30px;");
String s = String.valueOf(i);
int k = i;
button[i].setOnAction(e -> {
day = k;
label.setText(year + "年" + month + "月" + day + "日");
try {
System.out.println(userName + "ddd");
dealtext.setList(
information.returnDayMassage(String.format("%04d-%02d-%02d", year, month, day), userName));
in.setListView();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
});
Gpane.add(button[i], dayofweek, number);
if (dayofweek == 6) {
number++;
}
cal.add(Calendar.DAY_OF_MONTH, 1);// 日子数加一,变成下一天
}
Pane_MI_pane.getChildren().add(Gpane);
Pane_MI_pane.setTopAnchor(Gpane, 120.0);
Pane_MI_pane.setLeftAnchor(Gpane, 20.0);
}
//显示精确时间
public void Ftime(Label Label_MI_time) {
EventHandler<ActionEvent> eventHandler = e -> {
Label_MI_time.setText(getNowFtime());
};
Timeline timeline = new Timeline(new KeyFrame(Duration.millis(1000), eventHandler));
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play();
}
}
感想
大量的试错才可以找到正确的技术使用方法。
钻牛角尖是不正确的,有时候问问身边的人也许会事半功倍。
如果文章有帮助,不妨点个小小的赞,感谢!
有时候中文网站找不到答案,去看看英文文档,或者求助去Stack overflow。
免费下载
参考: