首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何知道JCalendar是否为空?

如何知道JCalendar是否为空?
EN

Stack Overflow用户
提问于 2014-03-24 16:43:45
回答 2查看 5K关注 0票数 0

我有一个带有JCalendar的简单Java程序。我需要知道我的JCalendar calendario是否为空,这意味着用户是否选择了日期。我在考虑一种像calendario.isEmpty这样的方法,但它并不存在。也许我可以将calendarionull进行比较,但它没有起作用。我正在使用com.toedter.calendar.JDateChooser

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-03-24 17:29:43

如果使用com.toedter.calendar.JDateChooser,最好的方法是检查调用实例方法getDate()的返回日期。如果返回的日期是null,这意味着用户没有选择日期。例如:

代码语言:javascript
复制
public class TestCalendar extends JFrame implements ActionListener {

  private JDateChooser dateChooser;

  public TestCalendar() {
    super("Simple");
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setLayout(new FlowLayout());
    dateChooser = new JDateChooser();
    add(dateChooser);
    JButton button = new JButton("Check");
    button.addActionListener(this);
    add(button);
    setSize(300, 100);
  }

  public void actionPerformed(ActionEvent e) {
    Date date = dateChooser.getDate();
    if (date == null) {
      JOptionPane.showMessageDialog(TestCalendar.this, "Date is required.");
    }
  }

  public static void main(String[] args) {
    new TestCalendar().setVisible(true);
  }

}
票数 4
EN

Stack Overflow用户

发布于 2017-04-26 01:54:30

你也可以试试这个

代码语言:javascript
复制
if(jDateChooser1.getDate()==null){
   JOptionPane.showMessageDialog(this, "Date not selected");
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22615461

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档