gpt4 book ai didi

java - 为什么日期选择器会意外触发 JComboBox 的 actionPerformed?

转载 作者:行者123 更新时间:2023-11-29 06:04:58 31 4
gpt4 key购买 nike

我有两个日期选择器组件和一个 JComboBox 组件及其关联的 Action Listener。我的问题是:当我从任何日期选择器中选择一个日期时,不会触发它们的关联事件,还会触发 JComboBox 的事件。更令人惊讶的是,首先触发了 JComboBox 的事件!

另一方面,当我单击 JComboBox 组件并选择一个值时,只会触发 JComboBox 事件,而不会触发日期选择器事件,这很好。但出现上段情况的原因是什么?

JComboBox event fired even if only date pickers are clicked

为了进一步说明,这里是控制台的示例输出。首先,我单击第一个日期选择器并选择一个日期:

index: -1  null
Combo Box is involved. Surprised?
This is only fired when datePicker1 is involved.

(是的,我很惊讶!)然后我点击第二个日期选择器并选择一个日期:

index: -1  null
Combo Box is involved. Surprised?
This is only fired when datePicker2 is involved.

(是的,我再次感到惊讶!)最后我单击组合框并选择一个项目:

 index: 1  Last 6 months
Combo Box is involved. Surprised?

不,看到上面的输出我并不感到惊讶。

关于为什么会发生这种奇怪情况有什么想法吗?

完整源码:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;

import net.sourceforge.jdatepicker.JDateComponentFactory;
import net.sourceforge.jdatepicker.impl.JDatePickerImpl;
import net.sourceforge.jdatepicker.impl.UtilCalendarModel;
import org.jdesktop.swingx.JXDatePicker;

public class DatePickerDemo {

private static boolean isDateRangeConsistent(UtilCalendarModel m1, UtilCalendarModel m2) {
return m1.getValue().compareTo(m2.getValue()) <= 0 ? true : false;
}

private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("DatePickerDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final JLabel label1 = new JLabel();
label1.setText("Choose Range (Start - End): ");

String[] fixedRanges = {"Last 3 months", "Last 6 months", "Last 12 months"};
final JComboBox<String> fixedRangesComboBox = new JComboBox<String>(fixedRanges);
fixedRangesComboBox.setSelectedIndex(-1);


final JDatePickerImpl datePicker1 = (JDatePickerImpl) JDateComponentFactory.createJDatePicker();
datePicker1.getModel().setSelected(true);

final JDatePickerImpl datePicker2 = (JDatePickerImpl) JDateComponentFactory.createJDatePicker();
datePicker2.getModel().setSelected(true);


datePicker1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Start date cannot be after end date!
if ( ! isDateRangeConsistent((UtilCalendarModel) datePicker1.getModel(),
(UtilCalendarModel) datePicker2.getModel())) {
((UtilCalendarModel) datePicker1.getModel())
.setValue(((UtilCalendarModel) datePicker2.getModel()).getValue());
}

//if the date range is changed by date picker, the fixed combo box becomes irrelevant
fixedRangesComboBox.setSelectedIndex(-1);

int rangeDaysStart = datePicker1.getModel().getDay();
int rangeMonthsStart = 1 + datePicker1.getModel().getMonth();
int rangeYearsStart = datePicker1.getModel().getYear();
label1.setText("Choose Range (Start - End): " + rangeDaysStart
+ "/" + rangeMonthsStart + "/" + rangeYearsStart);

System.out.println("This is only fired when datePicker1 is involved.");
}
});


datePicker2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// End date cannot be before start date!
if ( ! isDateRangeConsistent((UtilCalendarModel) datePicker1.getModel(),
(UtilCalendarModel) datePicker2.getModel())) {
((UtilCalendarModel) datePicker2.getModel())
.setValue(((UtilCalendarModel) datePicker1.getModel()).getValue());
}

//if the date range is changed by date picker, the fixed combo box becomes irrelevant
fixedRangesComboBox.setSelectedIndex(-1);

int rangeDaysStart = datePicker2.getModel().getDay();
int rangeMonthsStart = 1 + datePicker2.getModel().getMonth();
int rangeYearsStart = datePicker2.getModel().getYear();
label1.setText("Choose Range (Start - End): " + rangeDaysStart
+ "/" + rangeMonthsStart + "/" + rangeYearsStart);

System.out.println("This is only fired when datePicker2 is involved.");
}
});

fixedRangesComboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("index: " + fixedRangesComboBox.getSelectedIndex() + " "
+ fixedRangesComboBox.getSelectedItem());

System.out.println("Combo Box is involved. Surprised?");
}
});

frame.getContentPane().add(label1, BorderLayout.NORTH);
frame.getContentPane().add(datePicker1, BorderLayout.CENTER);
frame.getContentPane().add(datePicker2, BorderLayout.EAST);
frame.getContentPane().add(fixedRangesComboBox, BorderLayout.PAGE_END);

//Display the window.
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}

最佳答案

您应该从两个 ActionListener 匿名类中删除 fixedRangesComboBox.setSelectedIndex(-1); :)

关于java - 为什么日期选择器会意外触发 JComboBox 的 actionPerformed?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8834207/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com