当前位置:网站首页>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
边栏推荐
- 宁德时代地位不保?
- 为什么BI对企业这么重要?
- 由于3²+4²=5²,所以称‘3,4,5‘为勾股数,求n(包括n)以内所有勾股数数组。
- MYSQL03_ SQL overview, rules and specifications, basic select statements, display table structure
- ASP. Net 6 middleware series - Custom middleware classes
- Mysql database design specification
- Creating wechat voucher process with PHP
- How does Microsoft solve the problem of multiple PC programs
- [Mysql] LEFT函數 | RIGHT函數
- TP5 email (2020-05-27)
猜你喜欢

2022年P气瓶充装培训试题及模拟考试

Impact of AOT and single file release on program performance

类似Jira的十大项目管理软件

Data mining series (3)_ Data mining plug-in for Excel_ Estimation analysis

A set of combination boxing to create an idea eye protection scheme

This new feature of C 11, I would like to call it the strongest!

Configure automatic implementation of curd projects

TP5 email (2020-05-27)

Xutils3 corrected a bug I reported. Happy

全网最全,接口自动化测试怎么做的?精通接口自动化测试详解
随机推荐
Ningde's position in the times is not guaranteed?
Establishing and traversing binary tree
在.NE6 WebApi中使用分布式缓存Redis
C WPF UI framework mahapps switching theme
TP5 multi conditional where query (using PHP variables)
The most easy to understand dependency injection and control inversion
be based on. NETCORE development blog project starblog - (1) why do you need to write your own blog?
MYSQL05_ Ordr by sorting, limit grouping, group by grouping
Mise en service PID du moteur de codage (anneau de vitesse | anneau de position | suivant)
Drawing polygons with < polygon / > circular array in SVG tag
如果通过 C# 实现对象的深复制 ?
Maui initial experience: Cool
Middle and rear binary tree
建立与遍历二叉树
荐读 | 分享交易员的书单,向名家请教交易之道,交易精彩无比
General testing technology [1] classification of testing
Queue storage and circular queue
先中二叉建树
全网最全,接口自动化测试怎么做的?精通接口自动化测试详解
Use DFS to solve the problem of "number of dictionary rows"