使用JComboBox制作日历可以通过以下步骤实现:
以下是一个示例代码,演示如何使用JComboBox制作日历:
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.YearMonth;
public class CalendarApp extends JFrame {
private JComboBox<Integer> yearComboBox;
private JComboBox<String> monthComboBox;
private JPanel calendarPanel;
private JTable calendarTable;
public CalendarApp() {
setTitle("Calendar App");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// 创建年份选择的JComboBox
yearComboBox = new JComboBox<>();
int currentYear = YearMonth.now().getYear();
for (int year = currentYear - 10; year <= currentYear + 10; year++) {
yearComboBox.addItem(year);
}
// 创建月份选择的JComboBox
monthComboBox = new JComboBox<>();
String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
for (String month : months) {
monthComboBox.addItem(month);
}
// 创建日历显示的JPanel
calendarPanel = new JPanel(new BorderLayout());
// 添加年份和月份选择的JComboBox到主界面
JPanel selectionPanel = new JPanel();
selectionPanel.add(yearComboBox);
selectionPanel.add(monthComboBox);
add(selectionPanel, BorderLayout.NORTH);
// 添加日历显示的JPanel到主界面
add(calendarPanel, BorderLayout.CENTER);
// 监听年份和月份选择的JComboBox的选择事件
yearComboBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
updateCalendar();
}
});
monthComboBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
updateCalendar();
}
});
// 初始化日历显示
updateCalendar();
pack();
setVisible(true);
}
private void updateCalendar() {
int year = (int) yearComboBox.getSelectedItem();
int month = monthComboBox.getSelectedIndex() + 1;
YearMonth yearMonth = YearMonth.of(year, month);
int daysInMonth = yearMonth.lengthOfMonth();
// 创建日期数据
Object[][] data = new Object[6][7];
int day = 1;
for (int row = 0; row < 6; row++) {
for (int col = 0; col < 7; col++) {
if (row == 0 && col < yearMonth.atDay(1).getDayOfWeek().getValue() % 7) {
data[row][col] = "";
} else if (day > daysInMonth) {
data[row][col] = "";
} else {
data[row][col] = day;
day++;
}
}
}
// 创建数据模型
DefaultTableModel model = new DefaultTableModel(data, new String[]{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"});
// 创建日历表格
calendarTable = new JTable(model);
calendarTable.setRowHeight(30);
calendarTable.setDefaultRenderer(Object.class, new CalendarCellRenderer());
// 更新日历显示的JPanel
calendarPanel.removeAll();
calendarPanel.add(new JScrollPane(calendarTable), BorderLayout.CENTER);
calendarPanel.revalidate();
calendarPanel.repaint();
}
private class CalendarCellRenderer extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (value != null) {
int day = (int) value;
if (day == YearMonth.now().getDayOfMonth()) {
setForeground(Color.RED);
} else {
setForeground(Color.BLACK);
}
}
return this;
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new CalendarApp();
}
});
}
}
这个示例代码使用了Java Swing库来创建一个简单的日历应用程序。它使用了JComboBox来选择年份和月份,使用JTable来显示日期。通过监听年份和月份选择的JComboBox的选择事件,动态更新日历显示。在日历表格中,当前日期会以红色字体显示。
腾讯云相关产品和产品介绍链接地址:
请注意,以上链接仅供参考,具体产品选择应根据实际需求进行评估。
领取专属 10元无门槛券
手把手带您无忧上云