完善功能
parent
b648749ded
commit
b7ea976fa8
|
@ -1,3 +1,3 @@
|
||||||
out/*
|
out/*
|
||||||
.idea/*
|
.idea/*
|
||||||
note.iml
|
*.iml
|
||||||
|
|
|
@ -0,0 +1,30 @@
|
||||||
|
start7410
|
||||||
|
1+
|
||||||
|
成长路上,有快乐,有幸福;有苦涩,有艰辛。成长告诉我,我不再是那个淘气的孩子,我长大了;成长告诉我珍惜光阴;成长告诉我珍惜友谊;成长告诉我和家人心连着心;下面是有有关成长的优美散文,欢迎参阅。
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
start7410
|
||||||
|
2+
|
||||||
|
有关成长的优美散文:在体验中成长生活丰富多彩,探索它也是很有趣的。
|
||||||
|
实践也是生活的一部分,也让我们更加多姿多彩,也让我们得到很多。
|
||||||
|
一次集体旅行,让我们得到快乐;一次搞卫生,让我们体味到劳动的艰辛;一次来之不易的成功,让我们体会到付出的重要,明白了奋斗的可贵。
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
start7410
|
||||||
|
3+
|
||||||
|
如今社会步入小康,百姓们再也不用像以前一样担忧粮食了。每当到吃饭时间,父母总是做好饭喊我吃,我就只要乖乖吃饭就可以了。
|
||||||
|
有一次我突然想,每次都是父母做饭,我总也要实践一下吧,于是乎,我经过父母的允许,准备自己弄一顿饭。
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
start7410
|
||||||
|
4+
|
||||||
|
买菜。
|
||||||
|
“选菜是自己要仔细看,别选些坏的……”“得买2,3元洋葱,对了,盐不多了,买些盐……”刚出门,妈妈生怕我不知道,忙说了大堆话,“好好好”,我含糊的回答后便小跑迅速离开,去菜市场的路上我都在想着该买的东西,不时的回想。因为以前随父母来过,所以我很快到了。菜市场气氛不多说,肯定是喧闹的,买几个辣椒,随便在小摊上拿上几个,之后付钱。
|
||||||
|
我初次买,实在不好说,连价都不知道砍,肯定被商家赚到了一些。不过有几个买菜老板很热情,一见我来买菜,忙客气的招呼,过了好久,我觉得差不多了,又仔细的回忆一下,嗯,应该没漏,没多留后飞奔似的回家。
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,22 +1,33 @@
|
||||||
package top.dreamcenter.note;
|
package top.dreamcenter.note;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import javax.swing.text.BadLocationException;
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.event.KeyAdapter;
|
import java.awt.event.KeyAdapter;
|
||||||
import java.awt.event.KeyEvent;
|
import java.awt.event.KeyEvent;
|
||||||
|
import java.awt.event.WindowAdapter;
|
||||||
|
import java.awt.event.WindowEvent;
|
||||||
|
import java.io.*;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
|
|
||||||
|
private static final int TOTAL_PAGE = 3650;
|
||||||
|
private static final String MARK = "start7410";
|
||||||
|
|
||||||
|
private static File file = new File("data/data.txt");
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
JFrame jf = new JFrame("日记本");
|
|
||||||
|
JFrame jf = new JFrame("日记本 [F5 上一页; F6 下一页; F7 保存]");
|
||||||
|
|
||||||
jf.setSize(800, 1020);
|
jf.setSize(800, 1020);
|
||||||
jf.setResizable(false);
|
jf.setResizable(false);
|
||||||
jf.setLocationRelativeTo(null);
|
jf.setLocationRelativeTo(null);
|
||||||
jf.getContentPane().setLayout(null);
|
jf.getContentPane().setLayout(null);
|
||||||
jf.setBackground(Color.WHITE);
|
jf.setBackground(Color.WHITE);
|
||||||
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
jf.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
|
||||||
|
|
||||||
// 内容面板穿透
|
// 内容面板穿透
|
||||||
JPanel imPanel=(JPanel) jf.getContentPane();
|
JPanel imPanel=(JPanel) jf.getContentPane();
|
||||||
|
@ -31,55 +42,165 @@ public class Main {
|
||||||
JLabel backgroundLabel = new JLabel(new ImageIcon(newimg));
|
JLabel backgroundLabel = new JLabel(new ImageIcon(newimg));
|
||||||
backgroundLabel.setBounds(0, 0, jf.getWidth() , jf.getHeight());
|
backgroundLabel.setBounds(0, 0, jf.getWidth() , jf.getHeight());
|
||||||
|
|
||||||
|
// 数据
|
||||||
|
final int[] curPage = {0};
|
||||||
|
LinkedList<String> contentlist = new LinkedList<>();
|
||||||
|
LinkedList<String> dateList = new LinkedList<>();
|
||||||
|
initList(contentlist, dateList);
|
||||||
|
|
||||||
|
// 页号
|
||||||
|
JLabel pageNo = new JLabel(String.valueOf(curPage[0]));
|
||||||
|
pageNo.setFont(new Font("Segoe Script", Font.PLAIN, 20));
|
||||||
|
pageNo.setHorizontalAlignment(JLabel.RIGHT);
|
||||||
|
pageNo.setBounds(330,40,60,40);
|
||||||
|
|
||||||
|
// 日期
|
||||||
|
JTextField date = new JTextField(dateList.get(curPage[0]));
|
||||||
|
date.setBorder(null);
|
||||||
|
date.setFont(new Font("Segoe Script", Font.PLAIN, 18));
|
||||||
|
date.setBounds(100,45,130,30);
|
||||||
|
|
||||||
// 文本行
|
// 文本行
|
||||||
JTextArea textArea = new JTextArea();
|
JTextArea textArea = new JTextArea(contentlist.get(curPage[0]));
|
||||||
textArea.setFont(new Font(null, Font.PLAIN, 36));
|
textArea.setFont(new Font(null, Font.PLAIN, 36));
|
||||||
textArea.setOpaque(false); // 透明背景
|
textArea.setOpaque(false); // 透明背景
|
||||||
textArea.setLineWrap(true); // 自动换行
|
textArea.setLineWrap(true); // 自动换行
|
||||||
textArea.setTabSize(2);
|
textArea.setTabSize(2);
|
||||||
textArea.setBounds(20, 90, jf.getWidth() - 40, jf.getHeight() - 140);
|
textArea.setBounds(20, 90, jf.getWidth() - 40, jf.getHeight() - 140);
|
||||||
|
|
||||||
int maxRows = 19;
|
date.addKeyListener(new KeyAdapter() {
|
||||||
|
@Override
|
||||||
|
public void keyReleased(KeyEvent e) {
|
||||||
|
dateList.set(curPage[0], date.getText());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
textArea.addKeyListener(new KeyAdapter() {
|
textArea.addKeyListener(new KeyAdapter() {
|
||||||
@Override
|
@Override
|
||||||
public void keyReleased(KeyEvent e) {
|
public void keyReleased(KeyEvent e) {
|
||||||
try {
|
contentlist.set(curPage[0], textArea.getText());
|
||||||
int len = textArea.getText().length();
|
switch (e.getKeyCode()) {
|
||||||
Rectangle rec = textArea.modelToView(len);
|
case KeyEvent.VK_F5 -> { // 上一页
|
||||||
// 最大行数
|
if (curPage[0] > 0) {
|
||||||
int line = rec.y / rec.height + 1;
|
contentlist.set(curPage[0], textArea.getText());
|
||||||
|
dateList.set(curPage[0], date.getText());
|
||||||
// 超出部分逻辑判断
|
curPage[0]--;
|
||||||
if (line > maxRows) {
|
pageNo.setText(String.valueOf(curPage[0]));
|
||||||
int i = len - 1;
|
textArea.setText(contentlist.get(curPage[0]));
|
||||||
for (; i >= 0; i--) {
|
date.setText(dateList.get(curPage[0]));
|
||||||
Rectangle rec0 = textArea.modelToView(i);
|
|
||||||
|
|
||||||
int tmpLine = rec0.y / rec0.height + 1;
|
|
||||||
if (tmpLine <= maxRows){
|
|
||||||
i++;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
case KeyEvent.VK_F6 -> { // 下一页
|
||||||
System.out.println("超出行数字符串内容:" + textArea.getText(i, len - i));
|
if (curPage[0] < TOTAL_PAGE) {
|
||||||
textArea.setText(textArea.getText(i, len - i));
|
contentlist.set(curPage[0], textArea.getText());
|
||||||
|
dateList.set(curPage[0], date.getText());
|
||||||
|
curPage[0]++;
|
||||||
|
pageNo.setText(String.valueOf(curPage[0]));
|
||||||
|
textArea.setText(contentlist.get(curPage[0]));
|
||||||
|
date.setText(dateList.get(curPage[0]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
} catch (BadLocationException badLocationException) {
|
|
||||||
badLocationException.printStackTrace();
|
|
||||||
}
|
}
|
||||||
|
case KeyEvent.VK_F7 -> { // 保存
|
||||||
|
saveData(contentlist, dateList);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
jf.addWindowListener(new WindowAdapter() {
|
||||||
|
public void windowClosing(WindowEvent e) {
|
||||||
|
int confirm = JOptionPane.showConfirmDialog(jf, "是否保存并退出?", "保存退出", JOptionPane.YES_NO_CANCEL_OPTION);
|
||||||
|
if (confirm == JOptionPane.YES_OPTION) {
|
||||||
|
saveData(contentlist, dateList);
|
||||||
|
System.exit(0);
|
||||||
|
} else if (confirm == JOptionPane.NO_OPTION) {
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
// 添加组件
|
// 添加组件
|
||||||
jf.getLayeredPane().add(backgroundLabel, Integer.valueOf(Integer.MIN_VALUE));
|
jf.getLayeredPane().add(backgroundLabel, Integer.valueOf(Integer.MIN_VALUE));
|
||||||
|
jf.getContentPane().add(date);
|
||||||
|
jf.getContentPane().add(pageNo);
|
||||||
jf.getContentPane().add(textArea);
|
jf.getContentPane().add(textArea);
|
||||||
|
|
||||||
jf.setVisible(true);
|
jf.setVisible(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void initList(List<String> contentList, List<String> dateList) {
|
||||||
|
if (!file.exists()) {
|
||||||
|
try {
|
||||||
|
boolean createResult = file.createNewFile();
|
||||||
|
if (!createResult) {
|
||||||
|
JOptionPane.showMessageDialog(null, "数据文件创建失败", "警告", JOptionPane.WARNING_MESSAGE);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
JOptionPane.showMessageDialog(null, "IO错误:" + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < TOTAL_PAGE; i++){
|
||||||
|
contentList.add("");
|
||||||
|
dateList.add("");
|
||||||
|
}
|
||||||
|
|
||||||
|
try (BufferedReader br = new BufferedReader(
|
||||||
|
new InputStreamReader(new FileInputStream(file)))) {
|
||||||
|
String tmp;
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
|
int page = -1;
|
||||||
|
String date = "";
|
||||||
|
while ((tmp = br.readLine()) != null) {
|
||||||
|
if (MARK.equals(tmp)) {
|
||||||
|
if (page != -1) {
|
||||||
|
dateList.set(page - 1, date);
|
||||||
|
contentList.set(page - 1, sb.toString());
|
||||||
|
sb.delete(0, sb.length());
|
||||||
|
}
|
||||||
|
String[] part = br.readLine().split("\\+");
|
||||||
|
page = Integer.parseInt(part[0]);
|
||||||
|
if (part.length == 2){
|
||||||
|
date = part[1];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
sb.append(tmp).append("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (page != -1) {
|
||||||
|
dateList.set(page - 1, date);
|
||||||
|
contentList.set(page - 1, sb.toString());
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void saveData(List<String> contentList, List<String> dateList){
|
||||||
|
try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)))) {
|
||||||
|
for (int i = 0; i < TOTAL_PAGE; i++) {
|
||||||
|
String date = dateList.get(i);
|
||||||
|
String content = contentList.get(i);
|
||||||
|
if (!"".equals(date) || !"".equals(content)) {
|
||||||
|
bw.write(MARK);
|
||||||
|
bw.newLine();
|
||||||
|
bw.write(String.valueOf(i + 1));
|
||||||
|
bw.write("+");
|
||||||
|
bw.write(date);
|
||||||
|
bw.newLine();
|
||||||
|
bw.write(content);
|
||||||
|
bw.newLine();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
JOptionPane.showMessageDialog(null, "保存成功!", "消息", JOptionPane.INFORMATION_MESSAGE);
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
JOptionPane.showMessageDialog(null, "保存失败:" + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue