用java代码设计一个日历记事本。 主要要求: (1)实现日期(阳历)的显示、查看,显示星期与日期的对应; (2)选中相应日期后,如果有历史记录,在文本框中显示记录的内容 (3)选中相应日期后,如果无历史记录,可以在文本框中记录内容并保存到txt文件中 (4)在保存文件时,提供一个以当前时间(年-月-日-时-分-秒)为名称的默认文件名。
时间: 2023-07-14 07:13:14 浏览: 153
以下是一个简单的Java代码设计日历记事本的示例:
```java
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.io.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class CalendarNotebook implements ActionListener {
private JFrame frame;
private JPanel panel;
private JTextArea textArea;
private JButton prevButton, nextButton, saveButton;
private Calendar calendar;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CalendarNotebook window = new CalendarNotebook();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public CalendarNotebook() {
initialize();
}
private void initialize() {
frame = new JFrame("Calendar Notebook");
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
frame.getContentPane().add(panel, BorderLayout.NORTH);
prevButton = new JButton("<");
prevButton.addActionListener(this);
panel.add(prevButton);
nextButton = new JButton(">");
nextButton.addActionListener(this);
panel.add(nextButton);
saveButton = new JButton("Save");
saveButton.addActionListener(this);
panel.add(saveButton);
textArea = new JTextArea();
frame.getContentPane().add(textArea, BorderLayout.CENTER);
calendar = new GregorianCalendar();
updateTextArea();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == prevButton) {
calendar.add(Calendar.DAY_OF_MONTH, -1);
updateTextArea();
} else if (e.getSource() == nextButton) {
calendar.add(Calendar.DAY_OF_MONTH, 1);
updateTextArea();
} else if (e.getSource() == saveButton) {
try {
String fileName = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date()) + ".txt";
File file = new File(fileName);
FileWriter writer = new FileWriter(file);
writer.write(textArea.getText());
writer.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
private void updateTextArea() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String date = sdf.format(calendar.getTime());
String dayOfWeek = getDayOfWeek(calendar.get(Calendar.DAY_OF_WEEK));
frame.setTitle(date + " " + dayOfWeek);
textArea.setText("");
try {
File file = new File(date + ".txt");
if (file.exists()) {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
textArea.append(line + "\n");
}
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
private String getDayOfWeek(int dayOfWeek) {
switch (dayOfWeek) {
case Calendar.SUNDAY:
return "Sunday";
case Calendar.MONDAY:
return "Monday";
case Calendar.TUESDAY:
return "Tuesday";
case Calendar.WEDNESDAY:
return "Wednesday";
case Calendar.THURSDAY:
return "Thursday";
case Calendar.FRIDAY:
return "Friday";
case Calendar.SATURDAY:
return "Saturday";
default:
return "";
}
}
}
```
这个程序使用了Java Swing库来创建GUI。在程序中,我们创建了一个JFrame窗口,一个JPanel面板,一个JTextArea文本框,以及三个JButton按钮。我们使用Calendar类来处理日期,并使用SimpleDateFormat类将日期格式化为字符串。当用户点击“<”按钮或“>”按钮时,我们会更新日历并更新文本框中的内容。当用户点击“Save”按钮时,我们会将文本框中的内容保存到一个以当前时间命名的txt文件中。如果用户在某个日期没有记录,那么我们会自动创建一个以该日期命名的txt文件。
阅读全文