gpt4 book ai didi

java - 在jpanel中添加jdatepicker

转载 作者:行者123 更新时间:2023-12-01 12:29:41 24 4
gpt4 key购买 nike

我试图在jpanel中添加jdatepicker,这个jpanel是在jframe内部调用的,问题是,如果我尝试将jdatepicker直接添加到jpanel中,它不会显示,直到执行某个事件。

我已经尝试过this解决方案,但这没有帮助。当我使用它时,它还会打开一个没有数据的窗口。

我使用的其他方法是 -

使用框架的MainApp类

public 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()));
}

}

最佳答案

这可能对你有帮助

请导入

    import java.util.Date; 
import net.sourceforge.jdatepicker.impl.JDatePanelImpl;
import net.sourceforge.jdatepicker.impl.JDatePickerImpl;
import net.sourceforge.jdatepicker.impl.UtilDateModel;

声明

    JDatePickerImpl 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

注意:日期选择器需要 jar 文件

关于java - 在jpanel中添加jdatepicker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26034298/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com