Java 为什么JDateChooser不接收焦点事件
我正在尝试将Java 为什么JDateChooser不接收焦点事件,java,swing,jdatechooser,focuslistener,Java,Swing,Jdatechooser,Focuslistener,我正在尝试将FocusAdapter添加到JDateChooserswing项,但是focusGovered()方法没有启动,也没有通过选项卡焦点或鼠标单击 public static void main(String[] args) { JPanel panel = new JPanel(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); c.fill = GridBagConst
FocusAdapter
添加到JDateChooser
swing项,但是focusGovered()
方法没有启动,也没有通过选项卡焦点或鼠标单击
public static void main(String[] args) {
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1;
JTextField textField = new JTextField();
panel.add(textField, c);
JDateChooser dateChooser = new JDateChooser(new Date());
dateChooser.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent evt) {
System.out.println(evt.getSource()); // This line never runs
}
});
c.gridy = 1;
panel.add(dateChooser, c);
JFrame frame = new JFrame();
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
令人沮丧的。。。我遗漏了一些小的东西吗?基于,您需要获得充当JDateChooser的编辑器的UI组件
JDateChooser dateChooser = new JDateChooser(new Date());
IDateEditor editor = dateChooser.getDateEditor();
JComponent comp = editor.getUiComponent();
comp.addFocusListener(...);
这是因为JDateChooser
的编辑器没有接收焦点does@MadProgrammer,应该是我可以接受的答案。必须先查找;)为了避免混淆,我之前(我正在查看代码)肯定直接在JDateChooser
中添加了一个FocusAdapter
,它起了作用,但我无法复制该功能。不确定为什么它在一个程序中有效,但在另一个程序中无效…无论哪种方式,你都是对的,这是正确的方法,即使另一种方式在侥幸中有效。。。