javaGUI显示日历小程序,不用字符串,使用下拉框,通过选择下拉框的年,月显示对应的当月日历,没有额外按钮,只通过选择下拉框显示日历,使用Calendar类
时间: 2023-10-03 13:09:13 浏览: 115
java API 在包java.util中有一个GregorianCalendar类,使用它可以得到一年内某个月的日历.zip
好的,这是一个简单的Java GUI日历小程序,使用了下拉框和Calendar类来显示当月日历。
```java
import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;
import javax.swing.*;
public class CalendarGUI extends JFrame implements ItemListener {
private static final long serialVersionUID = 1L;
private JComboBox<String> yearComboBox;
private JComboBox<String> monthComboBox;
private JLabel[] dayLabels;
private JPanel calendarPanel;
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new CalendarGUI().setVisible(true);
});
}
public CalendarGUI() {
setTitle("Calendar");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
setLocationRelativeTo(null);
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
JPanel controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
yearComboBox = new JComboBox<>();
yearComboBox.addItemListener(this);
for (int i = 1900; i <= 2100; i++) {
yearComboBox.addItem(String.valueOf(i));
}
controlPanel.add(yearComboBox);
monthComboBox = new JComboBox<>();
monthComboBox.addItemListener(this);
for (int i = 1; i <= 12; i++) {
monthComboBox.addItem(String.valueOf(i));
}
controlPanel.add(monthComboBox);
contentPane.add(controlPanel, BorderLayout.NORTH);
calendarPanel = new JPanel();
calendarPanel.setLayout(new GridLayout(0, 7));
String[] dayNames = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
dayLabels = new JLabel[7];
for (int i = 0; i < 7; i++) {
dayLabels[i] = new JLabel(dayNames[i], JLabel.CENTER);
dayLabels[i].setForeground(Color.BLUE);
calendarPanel.add(dayLabels[i]);
}
contentPane.add(calendarPanel, BorderLayout.CENTER);
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
yearComboBox.setSelectedItem(String.valueOf(year));
monthComboBox.setSelectedItem(String.valueOf(month));
updateCalendar(year, month);
}
private void updateCalendar(int year, int month) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, 1);
int firstDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
int lastDayOfMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendarPanel.removeAll();
for (int i = 1; i < firstDayOfWeek; i++) {
calendarPanel.add(new JLabel(""));
}
for (int i = 1; i <= lastDayOfMonth; i++) {
JLabel dayLabel = new JLabel(String.valueOf(i), JLabel.CENTER);
if (i == calendar.get(Calendar.DAY_OF_MONTH)) {
dayLabel.setForeground(Color.RED);
}
calendarPanel.add(dayLabel);
}
pack();
}
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
int year = Integer.parseInt((String) yearComboBox.getSelectedItem());
int month = Integer.parseInt((String) monthComboBox.getSelectedItem());
updateCalendar(year, month);
}
}
}
```
在这个程序中,我们首先创建了一个窗口和两个下拉框,其中一个用于选择年份,另一个用于选择月份。当用户选择年份或月份时,我们将调用`updateCalendar`方法来显示相应的日历。`updateCalendar`方法将使用Calendar类来计算该月份的第一天是星期几,以及该月份有多少天。然后,我们将创建一个网格布局,将星期几标签和日期标签添加到其中。如果当前日期与选定日期相同,我们将以红色显示该日期。最后,我们使用`pack`方法调整窗口大小以适应内容。
阅读全文