[英]How to know if a JCalendar is empty?
我有一个带有JCalendar的简单Java程序。 我需要知道JCalendar calendario
是否为空,这意味着用户是否选择了日期。 我在考虑一个类似calendario.isEmpty
的方法,但它不存在。 也许我可以将calendario
与null
进行比较,但是没有用。 我正在使用com.toedter.calendar.JDateChooser
。
如果使用的是com.toedter.calendar.JDateChooser
,最好的方法是检查调用实例方法getDate()
的返回日期。 如果返回的日期为null
,则表示用户尚未选择日期。 例如:
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);
}
}
你也可以尝试
if(jDateChooser1.getDate()==null){
JOptionPane.showMessageDialog(this, "Date not selected");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.