我正在尝试在jpanel中添加jdatepicker,这个jpanel在jframe内部调用,问题是如果我尝试将jdatepicker直接添加到jpanel中,则不会显示,直到执行某个事件。
我尝试了this解决方案,但这没有任何帮助。当我使用它时,它还会再打开一个没有数据的窗口。
和我正在使用的其他方法是 -
使用框架
的类MainApppublic class MainApp extends JFrame implements ActionListener {
private JPanel panel;
private JMenuBar m_menuBar;
private JMenu m_menuSales;
private JMenuItem m_itmSalesNewBuisness;
private JMenuItem m_itmSalesAppointments;
private static MainApp instance = null;
public static MainApp getInstance() {
if(instance == null) {
instance = new MainApp();
}
return instance;
}
private MainApp() {
initComponents();
}
private void initComponents() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
m_menuBar = new JMenuBar();
m_menuSales = new JMenu("Sales");
m_itmSalesAppointments = new JMenuItem("Appointments");
m_itmSalesAppointments.addActionListener(this);
m_itmSalesNewBuisness = new JMenuItem("New Business");
m_itmSalesNewBuisness.addActionListener(this);
m_menuBar.add(m_menuSales);
setJMenuBar(m_menuBar);
setSize(1300, 700);
setVisible(true);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
UIManager.put("swing.boldMetal", Boolean.FALSE);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new MainApp();
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
if(m_itmSalesNewBuisness == e.getSource()){
if(panel != null) {
remove(panel);
invalidate();
}
revalidate();
repaint();
panel = new CopyOfNewBusiness();
panel.setBounds(0, 0, getWidth(), getHeight());
add(panel);
} else if(m_itmSalesAppointments == e.getSource()){
if(panel != null) {
remove(panel);
invalidate();
}
revalidate();
repaint();
panel = new AppointmentTracker();
panel.setBounds(0, 0, getWidth(), getHeight());
add(panel);
}
}
public JDatePickerImpl setDatePicker() {
UtilDateModel model = new UtilDateModel();
model.setSelected(true);
JDatePanelImpl datePanel = new JDatePanelImpl(model);
JDatePickerImpl datePicker = new JDatePickerImpl(datePanel, new DateLabelFormatter());
return datePicker;
}
NewBusiness.java
public class CopyOfNewBusiness extends JPanel implements ActionListener, ItemListener {
private JLabel m_lblDate;
private JDatePickerImpl m_dtpDate;
private JLabel m_lblFollowUpDate;
private UtilDateModel followUpDateModel;
private JDatePanelImpl followUpDatePanel;
private JDatePickerImpl m_dtpFollowUp;
public CopyOfNewBusiness() {
initComponents();
}
private void initComponents() {
setLayout(null);
m_lblDate = new JLabel("Date");
m_lblDate.setBounds(10, 10, 100, 30);
add(m_lblDate);
m_dtpDate = MainApp.getInstance().setDatePicker();
m_dtpDate.setBounds(120, 10, 120, 30);
add(m_dtpDate);
m_lblFollowUpDate = new JLabel("Follow Up Date");
m_lblFollowUpDate.setBounds(10, 370, 100, 30);
add(m_lblFollowUpDate);
followUpDateModel = new UtilDateModel();
followUpDatePanel = new JDatePanelImpl(followUpDateModel);
m_dtpFollowUp = new JDatePickerImpl(followUpDatePanel);
m_dtpFollowUp.setBounds(120, 370, 120, 30);
add(m_dtpFollowUp);
setPreferredSize(new Dimension(getWidth(), getHeight()));
}
}
答案 0 :(得分:1)
这可能对您有所帮助
请导入
import java.util.Date;
import net.sourceforge.jdatepicker.impl.JDatePanelImpl;
import net.sourceforge.jdatepicker.impl.JDatePickerImpl;
import net.sourceforge.jdatepicker.impl.UtilDateModel;
声明
JDatePickerImpl datePicker;
将Datepicker添加到JPanel
JLabel l22=new JLabel("DATE :");
l22.setBounds(100,350,100,20);
p5_assgn_work.add(l22);
UtilDateModel model=new UtilDateModel();
JDatePanelImpl datePanel = new JDatePanelImpl(model);
datePicker = new JDatePickerImpl(datePanel);
datePicker.setBounds(220,350,120,30);
p5_assgn_work.add(datePicker);
获取日期
Date selectedDate = (Date) datePicker.getModel().getValue();
String date=selectedDate+"";
p5_assgnwork是一个JPanel
NB:datepicker所需的jar文件