1

我正在尝试将一个添加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);
}

令人沮丧...我错过了一些小东西吗?

4

1 回答 1

3

根据JavaDocs,您需要获取充当编辑器的 UI 组件JDateChooser

JDateChooser dateChooser = new JDateChooser(new Date());
IDateEditor editor = dateChooser.getDateEditor();
JComponent comp = editor.getUiComponent();
comp.addFocusListener(...);
于 2014-04-10T21:42:15.200 回答