我正在尝试将一个添加FocusAdapter
到一个JDateChooser
摆动项目,但该focusGained()
方法没有触发,既不是通过选项卡焦点也不是通过鼠标单击......
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);
}
令人沮丧...我错过了一些小东西吗?