在JDialog中使用JCalendar [英] Using JCalendar in a JDialog

查看:99
本文介绍了在JDialog中使用JCalendar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序使用 JDialog 来打开表单,并以我想要的形式为用户使用 JCalendar 选择一个日期,然后我将其用于其他方法。

My program uses JDialogs to open up forms and in the form I want to use JCalendar for the user to select a date and for me to use it for other methods afterwards.

我已下载 JCalendar 库。我读了一些示例代码,但仍然不知道该怎么做。我有一个想法,你在表单中按下一个按钮(选择日期),就像一个小窗口打开那个 JCalendar ,当选择日期时,它会显示在表单中作为TextField。

I have downloaded JCalendar library. I read some example codes but still not sure how to do it. 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.

有人可以推荐一些方法来解决这个问题吗?

Can someone recommend me some method of doing this with the least trouble?

推荐答案


我有一个想法,你在表单中按下一个按钮(选择日期)和
就像一个小窗口打开那个JCalendar,当日期是
选中它在表单中显示为TextField。

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 到日期选择器,以便监听日期属性更改并相应地更新文本字段的文本。例如:

You may want to try JDateChooser class present in JCalendar library, which allows selecting a date or type it manually. About the second part, you need to provide a PropertyChangeListener to the date chooser in order to listen the "date" property change and update the text field's text accordingly. For instance something like this:

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);

这篇关于在JDialog中使用JCalendar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆