为什么这个选择下拉框不跳转其他日历JPanel panel2 = new JPanel();panel2.setLayout(new GridLayout(7,7));7*7的网格 panel2.add(new JLabel(“一”,SwingConstants.CENTER));panel2.add(new JLabel(“二”,SwingConstants.CENTER));panel2.add(new JLabel(“三”,SwingConstants.CENTER));panel2.add(new JLabel(“四”,SwingConstants.CENTER));panel2.add(new JLabel(“五”,SwingConstants.CENTER));panel2.add(new JLabel(“六”,SwingConstants.CENTER));panel2.add(new JLabel(“日”,SwingConstants.CENTER));int year = comboBoxYear.getSelectedIndex();int month = comboBoxMonth.getSelectedIndex();日历第一个月份的一天 = 日历.getInstance();firstDayOfMonth.set(year,month-1,1);int firstDayOfWeek = firstDayOfMonth.get(Calendar.DAY_OF_WEEK)-1;int numOfMonth = firstDayOfMonth.getActualMaximum(Calendar.DAY_OF_MONTH);整数 k,num = 1;字符串编号;for(k = 0;k < 42;k++){ if(k < firstDayOfWeek - 2||k >= firstDayOfWeek + numOfMonth-2){ panel2.add(new JLabel(“”,SwingConstants.CENTER)); }else{ number = String.valueOf(num); panel2.add(new JLabel(number,SwingConstants.CENTER)); num++; } }ActionListener listener = e -> { int year1 = comboBoxYear.getSelectedIndex(); int month1 = comboBoxMonth.getSelectedIndex();Calendar firstDayOfMonth1 = Calendar.getInstance();首日1.集(年1,月1-1,1);int firstDayOfWeek1 = firstDayOfMonth1.get(Calendar.DAY_OF_WEEK)-1;int numOfMonth1 = firstDayOfMonth1.getActualMaximum(Calendar.DAY_OF_MONTH);整数 k1, 数字 1 = 1;字符串编号 1;for(k1 = 0; k1 < 42; k1++){ if(k1 < firstDayOfWeek1 - 2|| k1 >= firstDayOfWeek1 + numOfMonth1 -2){ panel2.add(new JLabel(“”,SwingConstants.CENTER)); }else{ number1 = String.valueOf(num1); panel2.add(new JLabel(number1,SwingConstants.CENTER)); num1++; } }};comboBoxYear.addActionListener(listener);comboBoxMonth.addActionListener(listener);
时间: 2024-01-24 15:18:58 浏览: 76
可能是因为你没有将这个选择下拉框与其他日历JPanel进行关联。你需要在监听器中添加代码来更新其他日历JPanel的显示内容。具体来说,当选择下拉框的值发生变化时,你需要重新计算该月份的第一天是星期几以及该月份有多少天,然后更新其他日历JPanel的显示内容。你可以通过遍历JPanel中的所有标签来实现更新。
相关问题
javaGUI显示日历小程序,不用字符串,使用下拉框,通过选择下拉框的年,月显示对应的当月日历,没有额外按钮,只通过选择下拉框显示日历,使用Calendar类
好的,这是一个简单的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`方法调整窗口大小以适应内容。
javaGUI通过下拉框选择日期来显示日历,不要额外加按钮
可以使用JComboBox来创建下拉框,然后根据选择的日期更新日历显示。
下面是一个简单的实现,可以根据需要进行修改和优化:
```java
import java.awt.*;
import java.awt.event.*;
import java.time.*;
import java.time.format.*;
import javax.swing.*;
public class CalendarGUI extends JFrame implements ActionListener {
private final JComboBox<String> yearBox, monthBox;
private final JLabel[] dayLabels;
private final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-M-d");
public CalendarGUI() {
setTitle("Calendar");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JPanel topPanel = new JPanel();
topPanel.add(new JLabel("Year:"));
yearBox = new JComboBox<>();
for (int year = LocalDate.now().getYear() - 10; year <= LocalDate.now().getYear() + 10; year++) {
yearBox.addItem(String.valueOf(year));
}
yearBox.setSelectedItem(String.valueOf(LocalDate.now().getYear()));
yearBox.addActionListener(this);
topPanel.add(yearBox);
topPanel.add(new JLabel("Month:"));
monthBox = new JComboBox<>();
for (int month = 1; month <= 12; month++) {
monthBox.addItem(String.valueOf(month));
}
monthBox.setSelectedItem(String.valueOf(LocalDate.now().getMonthValue()));
monthBox.addActionListener(this);
topPanel.add(monthBox);
add(topPanel, BorderLayout.NORTH);
JPanel centerPanel = new JPanel(new GridLayout(7, 7));
String[] weekDays = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
for (String weekDay : weekDays) {
centerPanel.add(new JLabel(weekDay, SwingConstants.CENTER));
}
dayLabels = new JLabel[42];
for (int i = 0; i < dayLabels.length; i++) {
dayLabels[i] = new JLabel("", SwingConstants.CENTER);
centerPanel.add(dayLabels[i]);
}
updateCalendar();
add(centerPanel, BorderLayout.CENTER);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == yearBox || e.getSource() == monthBox) {
updateCalendar();
}
}
private void updateCalendar() {
int year = Integer.parseInt((String) yearBox.getSelectedItem());
int month = Integer.parseInt((String) monthBox.getSelectedItem());
LocalDate date = LocalDate.of(year, month, 1);
int firstDayOfWeek = date.getDayOfWeek().getValue() % 7;
int lastDay = date.plusMonths(1).minusDays(1).getDayOfMonth();
for (int i = 0; i < dayLabels.length; i++) {
if (i < firstDayOfWeek || i >= firstDayOfWeek + lastDay) {
dayLabels[i].setText("");
} else {
dayLabels[i].setText(String.valueOf(i - firstDayOfWeek + 1));
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(CalendarGUI::new);
}
}
```
这个GUI界面包含一个顶部面板和一个中心面板。顶部面板包含两个下拉框,用于选择年份和月份;中心面板包含一个网格布局,用于显示日历。网格布局中第一行为星期几的标签,接下来是42个用于显示日期的标签(最多有6*7=42天)。当用户选择年份或月份时,通过计算更新中心面板中的日期标签。
阅读全文