无法将JDateChooser组件添加到JFrame

时间:2013-09-06 19:16:05

标签: java jdatechooser

我正在尝试在我的JFrame中使用JDateChooser。(代码不是Beans IDE)。如何制作JDateChooser类并将其添加到JFrame。

import com.toedter.calendar.JDateChooser;
import com.toedter.calendar.JDateChooserBeanInfo;
import com.toedter.calendar.JDateChooserCellEditor;
import javax.swing.*;
import java.awt.*;
class main extends JFrame
{
JDateChooser cdate;

main()
{
cdate = new JDateChooser();
add(cdate);
setSize(400,200);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}

1 个答案:

答案 0 :(得分:1)

JDateChooser dateChooser = new JDateChooser();
dateChooser.setBounds(20, 20, 200, 30); // Modify depending on your preference
jFrame.getContentPane().add(dateChooser);

要获取日期,您可以 -

Date date = dateChooser.getDate();

修改:基于评论 -

com.toedter.calendar.JDateChooser dateChooser = new com.toedter.calendar.JDateChooser();
dateChooser.setDateFormatString("MM/dd/yyyy");

Date date =  dateChooser.getDate();
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
someJLabel.setText(dateFormat.format(date));