1 Star 0 Fork 0

曾铖坚 / day24-code

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
AddJFrame.java 5.52 KB
一键复制 编辑 原始数据 按行查看 历史
曾铖坚 提交于 2024-04-15 12:45 . java中的记事本练习
package com.itheima.ui;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
public class AddJFrame extends JFrame implements ActionListener {
//定义标题输入框
JTextField titleText = new JTextField();
//定义内容的输入区域
JTextArea contentText = new JTextArea();
//定义保存按钮
JButton save = new JButton("保存");
//定义取消按钮
JButton cancel = new JButton("取消");
public AddJFrame() {
//初始化界面
initFrame();
//初始化组件
initView();
//让界面展示出来
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
Object obj = e.getSource();
if (obj == save) {
System.out.println("保存按钮被点击了");
//得到要修改的标题
String title = titleText.getText();
//得到要修改的文本内容
String content = contentText.getText();
//3.写出数据
try {
BufferedWriter bw = new BufferedWriter(new FileWriter("diary\\correct1.txt"));
bw.write(title + "," + content);
bw.newLine();
bw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
System.out.println("数据输出成功");
//4.修改文件中的内容
try {
BufferedReader br = new BufferedReader(new FileReader("diary\\arrInfo.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("diary\\BackuparrInfo.txt"));
String line;
while ((line = br.readLine()) != null) {
//把数据以逗号分隔开
String[] arr = line.split(",");
//arr[0]表示编号 arr[1]表示标题 arr[2]表示文本内容
//index表示编号
int index = 0;
//编号为一位数的时候
if (arr[0].length() == 3) {
index = arr[0].charAt(2) - '0';
}
//编号为两位数的时候
if (arr[0].length() == 4) {
index = Integer.parseInt(arr[0].substring(2));
}
if (index == NoteJFrame.addflag + 1) {
bw.write(arr[0] + "," + title + "," + content);
bw.newLine();
} else {
bw.write(line);
bw.newLine();
}
}
bw.close();
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
//5.把添加的数据重新写到初始文件中
File src = new File("diary\\BackuparrInfo.txt");
File dest = new File("diary\\arrInfo.txt");
NoteJFrame.InitFile(src, dest);
NoteJFrame.fixarr();
this.setVisible(false);
new NoteJFrame();
} else if (obj == cancel) {
System.out.println("取消按钮被点击了");
this.setVisible(false);
new NoteJFrame();
}
}
private void initView() {
//定义最上面的每日一记
JLabel title = new JLabel("每日一记");
title.setBounds(220, 20, 584, 50);
title.setFont(new Font("宋体", Font.BOLD, 32));
this.getContentPane().add(title);
//定义文字:标题
JLabel subject = new JLabel("标题");
subject.setBounds(70, 90, 100, 30);
subject.setFont(new Font("宋体", Font.PLAIN, 16));
this.getContentPane().add(subject);
//定义文字:内容
JLabel content = new JLabel("内容");
content.setBounds(70, 140, 100, 30);
content.setFont(new Font("宋体", Font.PLAIN, 16));
this.getContentPane().add(content);
//设置标题的输入框
titleText.setBounds(120, 90, 426, 30);
titleText.setFont(new Font("宋体", Font.PLAIN, 16));
this.getContentPane().add(titleText);
//设置内容的输入框
contentText.setBounds(120, 140, 426, 300);
contentText.setFont(new Font("宋体", Font.PLAIN, 16));
this.getContentPane().add(contentText);
//设置保存按钮
save.setBounds(132, 466, 140, 40);
save.setFont(new Font("宋体", Font.PLAIN, 24));
save.addActionListener(this);
this.getContentPane().add(save);
//设置取消按钮
cancel.setBounds(312, 466, 140, 40);
cancel.setFont(new Font("宋体", Font.PLAIN, 24));
cancel.addActionListener(this);
this.getContentPane().add(cancel);
}
//对添加界面的一些设置
private void initFrame() {
//设置界面的宽高
this.setSize(600, 600);
//设置界面的标题
this.setTitle("每日一记(添加日记)");
//设置界面置顶
this.setAlwaysOnTop(true);
//设置界面居中
this.setLocationRelativeTo(null);
//设置关闭模式
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//取消默认的居中放置,只有取消了才会按照XY轴的形式添加组件
this.setLayout(null);
//设置背景颜色
this.getContentPane().setBackground(new Color(212, 212, 212));
}
}
1
https://gitee.com/zeng-chengjian/day24-code.git
git@gitee.com:zeng-chengjian/day24-code.git
zeng-chengjian
day24-code
day24-code
master

搜索帮助