How to write a calendar with a window using Java?
To create a calendar with a window using Java, you can utilize Java’s Swing library to build the window and components.
Here is a simple example code demonstrating how to create a calendar with a window using Java.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalDate;
import java.time.YearMonth;
public class CalendarApp extends JFrame {
private YearMonth currentYearMonth;
private JLabel monthLabel;
private JButton prevButton;
private JButton nextButton;
private JTable calendarTable;
public CalendarApp() {
currentYearMonth = YearMonth.now();
setTitle("Calendar App");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
monthLabel = new JLabel("", JLabel.CENTER);
calendarTable = new JTable(6, 7);
prevButton = new JButton("Prev");
prevButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
currentYearMonth = currentYearMonth.minusMonths(1);
updateCalendar();
}
});
nextButton = new JButton("Next");
nextButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
currentYearMonth = currentYearMonth.plusMonths(1);
updateCalendar();
}
});
updateCalendar();
setLayout(new BorderLayout());
add(monthLabel, BorderLayout.NORTH);
add(new JScrollPane(calendarTable), BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
buttonPanel.add(prevButton);
buttonPanel.add(nextButton);
add(buttonPanel, BorderLayout.SOUTH);
}
private void updateCalendar() {
monthLabel.setText(currentYearMonth.getYear() + "年" + currentYearMonth.getMonthValue() + "月");
LocalDate firstDayOfMonth = currentYearMonth.atDay(1);
int firstDayOfWeek = firstDayOfMonth.getDayOfWeek().getValue();
int daysInMonth = currentYearMonth.lengthOfMonth();
int numRows = (int) Math.ceil((firstDayOfWeek + daysInMonth) / 7.0);
String[] columnNames = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
Object[][] data = new Object[numRows][7];
int day = 1 - firstDayOfWeek;
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < 7; j++) {
if (day > 0 && day <= daysInMonth) {
data[i][j] = day;
} else {
data[i][j] = "";
}
day++;
}
}
calendarTable.setModel(new javax.swing.table.DefaultTableModel(data, columnNames));
calendarTable.setDefaultRenderer(calendarTable.getColumnClass(0), new CalendarCellRenderer());
}
private class CalendarCellRenderer extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (value != null) {
int day = (int) value;
if (day == LocalDate.now().getDayOfMonth() && currentYearMonth.equals(YearMonth.now())) {
label.setForeground(Color.RED);
} else {
label.setForeground(table.getForeground());
}
}
return label;
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new CalendarApp().setVisible(true);
}
});
}
}
This sample code creates a calendar application with a form that allows users to switch between months using Prev and Next buttons. The current date in the calendar table is displayed in red.
In this example code, we used Swing components such as JFrame, JLabel, JButton, JTable to build the window and calendar grid. The updateCalendar() method is used to refresh the content of the calendar grid. The CalendarCellRenderer class is used to customize the rendering of cells in order to highlight the current date.
By running the main() method, you can launch the calendar application and display the window.