0
是否有可能從JFrame或JOptionPane中返回一個來自Flib的Jcalendar的值? JCalendar需要自己的Listener類。我有這個:JOptionPane或JFrame從JCalendar返回值
org.freixas.jcalendar.JCalendar jd = new org.freixas.jcalendar.JCalendar(org.freixas.jcalendar.JCalendar.DISPLAY_DATE | org.freixas.jcalendar.JCalendar.DISPLAY_TIME,true);
KalendarPanel kalendar = new KalendarPanel();
String message = "Choose start date:\n";
Object[] params = {message, jd};
JOptionPane.showConfirmDialog(null, params, "Start date", JOptionPane.PLAIN_MESSAGE);
但我不知道如何添加Listener類和如何從JOptionPane返回值。在JFrame中,我可以添加類監聽器,但我不知道如何將值返回給父JFrame。 JFrame代碼:
public class Kalendar extends javax.swing.JFrame {
private Calendar date;
private SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:MM:ss");
private String datum;
public Kalendar() {
initComponents();
jCalendar1.addDateListener(new MyDateListener());
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jCalendar1 = new org.freixas.jcalendar.JCalendar();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jCalendar1, javax.swing.GroupLayout.PREFERRED_SIZE, 315, javax.swing.GroupLayout.PREFERRED_SIZE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jCalendar1, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)
);
pack();
}// </editor-fold>
// Variables declaration - do not modify
private org.freixas.jcalendar.JCalendar jCalendar1;
// End of variables declaration
class MyDateListener implements DateListener {
@Override
public void dateChanged(DateEvent e) {
datum = sdf.format(e.getSelectedDate().getTime());
}
}
非常感謝。
謝謝你的解決方案 – newim123