当前位置:网站首页>Experiment 6 input / output stream
Experiment 6 input / output stream
2022-04-23 03:13:00 【A happy wild pointer D】
One 、 The experiment purpose
master RandomAccessFile The use of the class ; Master character input 、 Output stream and buffered input 、 Use of output streams .
Two 、 Experimental content
Programming calendar Notepad . The specific requirements are as follows :
(1) The calendar can set the date by entering the year and month in the text box , You can also turn around by year , Click... With the left mouse button “ Last year ” and “ Next year ” Button , Subtract one and add one to the year of the current calendar . You can also turn around by month in a certain year , Click... With the left mouse button “ Last month, ” and “ Next month ” Button , Subtract one and add one to the calendar month .
(2) Left click the date on the calendar , You can use the notepad on the right ( Text area ) Edit relevant logs , And save the log to a file , The name of the file is a sequence of characters consisting of the current date . Users can read 、 Delete the log of a certain date , You can also continue to add new content to a log . In preservation 、 When deleting and reading logs, a confirmation dialog box will pop up first , To confirm saving 、 Delete and read operations .
(3) When there is a log on a certain date , The date is in bold 16 The number shows , The log shows the date ; When the user deletes the log of a certain date , This date restores the original appearance .
Achieve the effect diagram ( Reference resources ) as follows :

3、 ... and 、 Code implementation
The design idea is as follows :
The page can be divided into four sections : The up and down or so . The top and bottom layout adopts FlowLayout, Arrange the components from left to right ; Because there are seven rows and seven columns on the left , Each cell is the same size , So we can use GridLayout Layout manager ; On the right is FlowLayout Layout , Arrange from top to bottom . Because the window is divided into four parts up, down, left and right , So the window uses BorderLayout Layout .

// Initialization interface
public void init() {
// Set window size
this.setBounds(400, 100, 480, 440);
this.setLayout(new BorderLayout(10, 15));
// Up, down, left and right four plates
northPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 15, 10));
southPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));
leftPanel = new JPanel(new GridLayout(7, 7));
rightPanel = new JPanel(new BorderLayout(10, 10));
// Global Fonts
Font font = new Font("Serief", Font.BOLD, 14);
// Set the upper layout
// Year button
jtf_year = new JTextField(4);
jtf_year.setPreferredSize(new Dimension(76, 26));
jtf_year.setText(String.valueOf(this.year));
jtf_year.setEditable(false);
jtf_year.setBackground(Color.white);
preYear = new JButton(" Last year ");
preYear.setFont(font);
nextYear = new JButton(" Next year ");
nextYear.setFont(font);
preYear.addActionListener(this);
nextYear.addActionListener(this);
// Month button
jtf_month = new JTextField(4);
jtf_month.setText(String.valueOf(this.month));
jtf_month.setPreferredSize(new Dimension(76, 26));
jtf_month.setEditable(false);
jtf_month.setBackground(Color.white);
nextMonth = new JButton(" Next month ");
nextMonth.setFont(font);
preMonth = new JButton(" Last month, ");
preMonth.setFont(font);
nextMonth.addActionListener(this);
preMonth.addActionListener(this);
// Add these components to the panel
northPanel.add(preYear);
northPanel.add(jtf_year);
northPanel.add(nextYear);
northPanel.add(preMonth);
northPanel.add(jtf_month);
northPanel.add(nextMonth);
// Set the layout on the left
// Set the head of the calendar
String weeks[] = {" Japan ", " One ", " Two ", " 3、 ... and ", " Four ", " 5、 ... and ", " 6、 ... and "};
JLabel title[] = new JLabel[7];
for (int i = 0; i < 7; i++) {
title[i] = new JLabel(weeks[i]);
title[i].setFont(font);
title[i].setBorder(BorderFactory.createRaisedBevelBorder());
leftPanel.add(title[i]);
}
// Set the day of the calendar
everyDay = new JTextField[42];
for (int i = 0; i < 42; i++) {
everyDay[i] = new JTextField();
everyDay[i].addMouseListener(this);
everyDay[i].setEditable(false);
everyDay[i].setBackground(Color.white);
leftPanel.add(everyDay[i]);
}
// Get the daily schedule , Fill in the calendar
calendar = new CalendarBean(year, month);
this.setCalendarDayText();
// Set the layout on the right
// Current date prompt
lb_date = new JLabel(" The calendar :" + year + " year " + month + " month " + day + " Japan ", JLabel.CENTER);
lb_date.setFont(font);
// journal
note = new JTextArea(14, 20);
rightPanel.add(lb_date, BorderLayout.NORTH);
rightPanel.add(new JScrollPane(note), BorderLayout.SOUTH);
// Set the button layout below
btn_save = new JButton(" Save log ");
btn_delete = new JButton(" Delete log ");
btn_read = new JButton(" Read log ");
btn_save.setFont(font);
btn_delete.setFont(font);
btn_read.setFont(font);
btn_save.addActionListener(this);
btn_delete.addActionListener(this);
btn_read.addActionListener(this);
southPanel.add(btn_save);
southPanel.add(btn_delete);
southPanel.add(btn_read);
// Add these panels to the window
this.add(leftPanel, BorderLayout.CENTER);
this.add(rightPanel, BorderLayout.EAST);
this.add(northPanel, BorderLayout.NORTH);
this.add(southPanel, BorderLayout.SOUTH);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
}
// Read and write file operation
private void readNote() {
// Clear the log area first
note.setText("");
String path = "F:/IDEA/workspace/Test6/note/" + year + "" + month + "" + day + ".txt";
File file = new File(path);
// If the file exists , Just read the log ; If it doesn't exist, it will prompt
if (file.exists()) {
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
StringBuffer sb = new StringBuffer();
// Read line by line , Add the characters of each line to the character buffer , Finally, add all the contents to the log area
while ((line = br.readLine()) != null) {
sb.append(line);
}
note.setText(sb.toString());
br.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
private void deleteNote() {
String path = "F:/IDEA/workspace/Test6/note/" + year + "" + month + "" + day + ".txt";
File file = new File(path);
// There is no log for the current date , You can't delete
if (!file.exists()) {
JOptionPane.showMessageDialog(null, " There is no log for the current date !", " Tips ", JOptionPane.WARNING_MESSAGE);
return;
}
String msg = " Sure to delete " + year + " year " + month + " month " + day + " Japan " + " Do you have a good log ?";
int result = JOptionPane.showConfirmDialog(this, msg, " inquiry ",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (result == JOptionPane.YES_OPTION) {
try {
// If the file exists , Just delete
if (file.exists()) {
boolean success = file.delete();
if (success) {
JOptionPane.showMessageDialog(null, " Delete successful !", " Delete successful ", JOptionPane.INFORMATION_MESSAGE);
note.setText("");
} else {
JOptionPane.showMessageDialog(null, " Delete failed !", " Delete failed ", JOptionPane.ERROR_MESSAGE);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
private void addNote() {
// Determine whether there is a document of that date , New if not , Add if necessary
String path = "F:/IDEA/workspace/Test6/note/" + year + "" + month + "" + day + ".txt";
File file = new File(path);
String msg = "";
if (file.exists()) {
msg = year + " year " + month + " month " + day + " Japan " + " There are logs , Add new content to the log ?";
} else {
msg = " Sure to save " + year + " year " + month + " month " + day + " Japan " + " Do you have a good log ?";
}
String content = note.getText();
// Tips
int result = JOptionPane.showConfirmDialog(this, msg, " inquiry ",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
// If you choose the yes option , Just save the log
if (result == JOptionPane.YES_OPTION) {
try {
// If the log file does not exist, you must first create
if (!file.exists()) {
file.createNewFile();
}
// Append the log to the file
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(content);
bw.flush();
bw.close();
JOptionPane.showMessageDialog(null, " Saved successfully !", " Saved successfully ", JOptionPane.INFORMATION_MESSAGE);
} catch (Exception ee) {
ee.printStackTrace();
}
}
}
// Fill in the number of days
private void setCalendarDayText() {
String days[] = calendar.getCalendar();
String key = year + "" + month;
// look for note Under the folder , Log file of current month , See which days there are logs
File[] files = new File("F:/IDEA/workspace/Test6/note/").listFiles();
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < files.length; i++) {
String name = files[i].getName();
if (name.startsWith(key, 0)) {
int index = name.indexOf(key) + key.length();
String d = name.split(".txt")[0].substring(index);
list.add(Integer.parseInt(d));
}
}
for (int i = 0; i < 42; i++) {
everyDay[i].setText(days[i]);
everyDay[i].setFont(null);// Clear the original BOLD effect
// The day of the log will be displayed in bold
if (days[i] != null) {
if (list.indexOf(Integer.parseInt(days[i])) != -1) {
everyDay[i].setFont(new Font("TimesRoman", Font.BOLD, 16));
}
}
}
}
// Last year 、 Next year 、 Last month, 、 Next month button event handling
@Override
public void actionPerformed(ActionEvent e) {
try {
if (e.getSource() == nextYear) {
year += 1;
jtf_year.setText(String.valueOf(year));
calendar.setYear(year);
this.setCalendarDayText();
} else if (e.getSource() == preYear) {
year -= 1;
jtf_year.setText(String.valueOf(year));
calendar.setYear(year);
this.setCalendarDayText();
} else if (e.getSource() == nextMonth) {
// If it's not the last month
if (month < 12) {
month += 1;
jtf_month.setText(String.valueOf(month));
calendar.setMonth(month);
this.setCalendarDayText();
} else {
JOptionPane.showMessageDialog(null, " No next month !", " Tips ", JOptionPane.WARNING_MESSAGE);
}
} else if (e.getSource() == preMonth) {
if (month > 1) {
month -= 1;
calendar.setMonth(month);
jtf_month.setText(String.valueOf(month));
this.setCalendarDayText();
} else {
JOptionPane.showMessageDialog(null, " Not last month !", " Tips ", JOptionPane.WARNING_MESSAGE);
}
} else if (e.getSource() == btn_save) {
addNote();
} else if (e.getSource() == btn_read) {
readNote();
} else if (e.getSource() == btn_delete) {
deleteNote();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
import java.util.Calendar;
// Tool class , Used to get the calendar of each month , namely day And weekday Correspondence of
public class CalendarBean {
int year, month = 0;
public CalendarBean(int y, int m) {
this.year = y;
this.month = m;
}
public void setYear(int y) {
this.year = y;
}
public int getYear() {
return this.year;
}
public void setMonth(int m) {
this.month = m;
}
public int getMonth() {
return this.month;
}
public String[] getCalendar() {
String days[] = new String[42];
Calendar date = Calendar.getInstance();
date.set(year, month - 1, 1);
int week = date.get(Calendar.DAY_OF_WEEK) - 1;
int day = 0;
// Judge the big month
if (month == 1 || month == 3 || month == 5 || month == 7
|| month == 8 || month == 10 || month == 12) {
day = 31;
}
// Judge Xiaoyue
if (month == 4 || month == 6 || month == 9 || month == 11) {
day = 30;
}
// Judge ordinary and leap years
if (month == 2) {
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
day = 29;
} else {
day = 28;
}
}
for (int i = week, n = 1; i < week + day; i++) {
days[i] = String.valueOf(n);
n++;
}
return days;
}
}
版权声明
本文为[A happy wild pointer D]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220627537816.html
边栏推荐
- 在.NE6 WebApi中使用分布式缓存Redis
- [authentication / authorization] customize an authentication handler
- C# 读写二进制文件
- 【鉴权/授权】自定义一个身份认证Handler
- 【无标题】
- Realize QQ login with PHP
- 12.<tag-链表和常考点综合>-lt.234-回文链表
- ASP. Net and ASP NETCORE multi environment configuration comparison
- Load view Caton
- Improvement of ref and struct in C 11
猜你喜欢

数据挖掘系列(3)_Excel的数据挖掘插件_估计分析

MYSQL04_ Exercises corresponding to arithmetic, logic, bit, operator and operator

利用栈的回溯来解决“文件的最长绝对路径”问题

The whole network is the most complete. How to do interface automation test? Proficient in interface automation test details

Xamarin effect Chapter 21 expandable floating operation button in GIS
![General testing technology [1] classification of testing](/img/f1/d80b6793b6443cbc4048d7e6319f51.png)
General testing technology [1] classification of testing

ASP.NET 6 中间件系列 - 自定义中间件类

2022t elevator repair test simulation 100 questions and online simulation test

荐读 | 分享交易员的书单,向名家请教交易之道,交易精彩无比

《C语言程序设计》(谭浩强第五版) 第8章 善于利用指针 习题解析与答案
随机推荐
微软是如何解决 PC 端程序多开问题的
Recursion - outputs continuously increasing numbers
be based on. NETCORE development blog project starblog - (2) environment preparation and creation project
js递归树结构计算每个节点的叶子节点的数量并且输出
[untitled]
Use of metagroup object tuple in C
Configuration table and page information automatically generate curd operation page
Yes Redis using distributed cache in NE6 webapi
[Mysql] LEFT函数 | RIGHT函数
Configure automatic implementation of curd projects
Source generator actual combat
【无标题】
ASP. Net 6 middleware series - conditional Middleware
[Mysql] LEFT函數 | RIGHT函數
General testing technology [1] classification of testing
Load view Caton
一套关于 内存对齐 的C#面试题,做错的人很多!
2022年做跨境电商五大技巧小分享
[new version release] componentone added Net 6 and blazor platform control support
2022a special equipment related management (elevator) work license question bank and simulation examination