当前位置:网站首页>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
边栏推荐
- Source code interpretation of Flink index parameters (read quantity, sent quantity, sent bytes, received bytes, etc.)
- Tips in MATLAB
- 【鉴权/授权】自定义一个身份认证Handler
- 【新版发布】ComponentOne 新增 .NET 6 和 Blazor 平台控件支持
- The whole network is the most complete. How to do interface automation test? Proficient in interface automation test details
- . net tip: talk about the problem that the scoped service cannot be obtained in the middleware structure
- Laravel new route file
- Middle and rear binary tree
- 如果通过 C# 实现对象的深复制 ?
- Top 9 task management system in 2022
猜你喜欢
LNMP MySQL allows remote access
ASP. Net 6 middleware series - conditional Middleware
Web Course Design - his system
C语言实现通讯录----(静态版本)
Xutils3 corrected a bug I reported. Happy
《C语言程序设计》(谭浩强第五版) 第8章 善于利用指针 习题解析与答案
Xamarin effect Chapter 21 expandable floating operation button in GIS
C read / write binary file
类似Jira的十大项目管理软件
MySQL port is occupied when building xampp
随机推荐
2022g2 boiler stoker examination question bank and online simulation examination
一套组合拳,打造一款 IDEA 护眼方案
腾讯视频VIP会员,周卡特价9元!腾讯官方直充,会员立即生效!
Top ten project management software similar to JIRA
MYSQL03_ SQL overview, rules and specifications, basic select statements, display table structure
2022T电梯修理考试模拟100题及在线模拟考试
Middle and rear binary tree
Top 9 task management system in 2022
Yes Redis using distributed cache in NE6 webapi
一套关于 内存对齐 的C#面试题,做错的人很多!
Find the number of leaf nodes of binary tree
This new feature of C 11, I would like to call it the strongest!
. net tip: talk about the problem that the scoped service cannot be obtained in the middleware structure
Preview of converting doc and PDF to SWF file
. net core current limiting control - aspnetcoreratelimit
How does Microsoft solve the problem of multiple programs on PC side -- internal implementation
为什么BI对企业这么重要?
宁德时代地位不保?
Blazor University (11) component - replace attributes of subcomponents
微软是如何解决 PC 端程序多开问题的