0

JCalendarを使用した単純な Java プログラムがあります。JCalendarcalendarioが空であるかどうかを知る必要があります。これは、ユーザーが日付を選択したかどうかを意味します。のような方法を考えていましたcalendario.isEmptyが、存在しません。比較できるかもしれませcalendarionullが、うまくいきませんでした。を使用してcom.toedter.calendar.JDateChooserいます。

4

2 に答える 2

4

を使用している場合は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);
  }

}
于 2014-03-24T17:29:43.503 に答える