要用Java寫一個帶有窗體的日歷,可以使用Java的Swing庫來創建窗體和組件。
下面是一個簡單的示例代碼,展示了如何使用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);
}
});
}
}
這個示例代碼創建了一個帶有窗體的日歷應用,可以通過Prev和Next按鈕切換月份。日歷表格中,當前日期會以紅色顯示。
在這個示例代碼中,我們使用了Swing的JFrame、JLabel、JButton、JTable等組件來構建窗體和日歷表格。updateCalendar()方法用于更新日歷表格的內容。CalendarCellRenderer類用于自定義單元格的渲染,以便高亮顯示當前日期。
通過運行main()方法,可以啟動日歷應用并顯示窗體。