0

我的程序使用JDialogs 打开表单,并以我希望JCalendar用户选择日期并在之后将其用于其他方法的表单中使用。

我已经下载了JCalendar图书馆。我阅读了一些示例代码,但仍然不知道该怎么做。I have an idea that in the form you press a button (Select Date) and like a small window opens with that JCalendarand when the date is selected it is displayed in the form as a TextField.

有人可以向我推荐一些麻烦最少的方法吗?

4

1 回答 1

3

I have an idea that in the form you press a button (Select Date) and like a small window opens with that JCalendar and when the date is selected it is displayed in the form as a TextField.

您可能想尝试图书馆中的JDateChooser课程JCalendar,它允许选择日期或手动输入。关于第二部分,您需要为日期选择器提供一个PropertyChangeListener以侦听“日期”属性更改并相应地更新文本字段的文本。例如这样的:

final JTextField textField = new JTextField(15);

JDateChooser chooser = new JDateChooser();
chooser.setLocale(Locale.US);

chooser.addPropertyChangeListener("date", new PropertyChangeListener() {
    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        JDateChooser chooser = (JDateChooser)evt.getSource();
        SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
        textField.setText(formatter.format(chooser.getDate()));
    }
});

JPanel content = new JPanel();
content.add(chooser);
content.add(textField);

JDialog dialog = new JDialog ();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.getContentPane().add(content);
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
于 2014-02-21T22:20:59.780 回答