我有一个带有JCalendar的简单Java程序。我需要知道我的JCalendar calendario是否为空,这意味着用户是否选择了日期。我在考虑一种像calendario.isEmpty这样的方法,但它并不存在。也许我可以将calendario和null进行比较,但它没有起作用。我正在使用com.toedter.calendar.JDateChooser。
发布于 2014-03-24 17:29:43
如果使用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);
}
}发布于 2017-04-26 01:54:30
你也可以试试这个
if(jDateChooser1.getDate()==null){
JOptionPane.showMessageDialog(this, "Date not selected");
}https://stackoverflow.com/questions/22615461
复制相似问题