用java代码设计一个日历记事本。 主要要求: (1)实现日期(阳历)的显示、查看,显示星期与日期的对应; (2)选中相应日期后,如果有历史记录,在文本框中显示记录的内容 (3)选中相应日期后,如果无历史记录,可以在文本框中记录内容并保存到txt文件中 (4)在保存文件时,提供一个以当前时间(年-月-日-时-分-秒)为名称的默认文件名。
时间: 2023-06-26 19:09:56 浏览: 109
以下是一个简单的基于Java Swing的日历记事本的实现代码。其中,使用了Java中的Calendar类来获取当前日期,使用了JFileChooser类来选择保存文件的路径。
```java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class CalendarNotebook extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JButton[] dayButtons;
private JLabel monthLabel;
private JTextArea noteArea;
private JButton saveButton;
private int currentYear;
private int currentMonth;
private int currentDay;
public CalendarNotebook() {
super("Calendar Notebook");
// 获取当前日期
Calendar calendar = Calendar.getInstance();
currentYear = calendar.get(Calendar.YEAR);
currentMonth = calendar.get(Calendar.MONTH);
currentDay = calendar.get(Calendar.DAY_OF_MONTH);
// 创建顶部面板
JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
JButton prevButton = new JButton("<");
prevButton.addActionListener(this);
topPanel.add(prevButton);
monthLabel = new JLabel(getMonthString(currentMonth) + " " + currentYear);
topPanel.add(monthLabel);
JButton nextButton = new JButton(">");
nextButton.addActionListener(this);
topPanel.add(nextButton);
// 创建中间面板
JPanel middlePanel = new JPanel(new GridLayout(6, 7));
dayButtons = new JButton[42];
for (int i = 0; i < 42; i++) {
dayButtons[i] = new JButton();
dayButtons[i].setPreferredSize(new Dimension(50, 50));
dayButtons[i].setEnabled(false);
dayButtons[i].addActionListener(this);
middlePanel.add(dayButtons[i]);
}
updateCalendar();
// 创建底部面板
JPanel bottomPanel = new JPanel(new BorderLayout());
noteArea = new JTextArea();
noteArea.setLineWrap(true);
noteArea.setWrapStyleWord(true);
JScrollPane scrollPane = new JScrollPane(noteArea);
bottomPanel.add(scrollPane, BorderLayout.CENTER);
saveButton = new JButton("Save");
saveButton.addActionListener(this);
bottomPanel.add(saveButton, BorderLayout.SOUTH);
// 添加组件到窗口
add(topPanel, BorderLayout.NORTH);
add(middlePanel, BorderLayout.CENTER);
add(bottomPanel, BorderLayout.SOUTH);
// 设置窗口属性
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 400);
setLocationRelativeTo(null);
setVisible(true);
}
// 更新日历
private void updateCalendar() {
// 获取当前月份的第一天
Calendar calendar = Calendar.getInstance();
calendar.set(currentYear, currentMonth, 1);
int firstDay = calendar.get(Calendar.DAY_OF_WEEK) - 1;
// 获取当前月份的天数
int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
// 更新日历按钮的文本和可用状态
for (int i = 0; i < 42; i++) {
if (i >= firstDay && i < firstDay + daysInMonth) {
dayButtons[i].setText(String.valueOf(i - firstDay + 1));
dayButtons[i].setEnabled(true);
} else {
dayButtons[i].setText("");
dayButtons[i].setEnabled(false);
}
}
// 更新选中日期的背景色
for (int i = 0; i < 42; i++) {
if (dayButtons[i].isEnabled() && Integer.parseInt(dayButtons[i].getText()) == currentDay) {
dayButtons[i].setBackground(Color.YELLOW);
} else {
dayButtons[i].setBackground(null);
}
}
}
// 获取月份的字符串表示
private String getMonthString(int month) {
switch (month) {
case 0:
return "January";
case 1:
return "February";
case 2:
return "March";
case 3:
return "April";
case 4:
return "May";
case 5:
return "June";
case 6:
return "July";
case 7:
return "August";
case 8:
return "September";
case 9:
return "October";
case 10:
return "November";
case 11:
return "December";
default:
return "";
}
}
// 显示历史记录或新建记录
private void showNote() {
// 获取选中的日期
int selectedDay = -1;
for (int i = 0; i < 42; i++) {
if (dayButtons[i].isEnabled() && dayButtons[i].getBackground() == Color.YELLOW) {
selectedDay = Integer.parseInt(dayButtons[i].getText());
break;
}
}
// 显示历史记录或新建记录
if (selectedDay == -1) {
noteArea.setText("");
} else {
String filename = currentYear + "-" + (currentMonth + 1) + "-" + selectedDay + ".txt";
File file = new File(filename);
if (file.exists()) {
try {
noteArea.setText("");
noteArea.read(new java.io.FileReader(file), null);
} catch (IOException e) {
e.printStackTrace();
}
} else {
noteArea.setText("");
}
}
}
// 保存记录
private void saveNote() {
// 获取选中的日期和文本框内容
int selectedDay = -1;
String noteText = noteArea.getText();
for (int i = 0; i < 42; i++) {
if (dayButtons[i].isEnabled() && dayButtons[i].getBackground() == Color.YELLOW) {
selectedDay = Integer.parseInt(dayButtons[i].getText());
break;
}
}
// 保存记录到文件
if (selectedDay == -1) {
JOptionPane.showMessageDialog(this, "Please select a date.");
} else {
String filename = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date()) + ".txt";
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Save Note");
fileChooser.setSelectedFile(new File(filename));
int result = fileChooser.showSaveDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
FileWriter writer = new FileWriter(file);
writer.write(noteText);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source instanceof JButton) {
JButton button = (JButton) source;
if (button.getText().equals("<")) {
currentMonth--;
if (currentMonth < 0) {
currentMonth = 11;
currentYear--;
}
} else if (button.getText().equals(">")) {
currentMonth++;
if (currentMonth > 11) {
currentMonth = 0;
currentYear++;
}
} else {
currentDay = Integer.parseInt(button.getText());
}
monthLabel.setText(getMonthString(currentMonth) + " " + currentYear);
updateCalendar();
showNote();
} else if (source == saveButton) {
saveNote();
}
}
public static void main(String[] args) {
new CalendarNotebook();
}
}
```
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)