首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JOptionPane或JFrame从JCalendar返回值

JOptionPane或JFrame从JCalendar返回值
EN

Stack Overflow用户
提问于 2017-08-23 22:55:55
回答 1查看 182关注 0票数 0

是否有可能从JFrame或JOptionPane返回一个值,其中我有来自Flib的J日历?JCalendar需要自己的侦听器类。我有一个:

代码语言:javascript
复制
 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);

但我不知道如何添加侦听器类,以及如何从JOptionPane返回值。在JFrame中,我可以添加类侦听器,但我不知道如何将值返回给父JFrame。JFrame代码:

代码语言:javascript
复制
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());
    }
}

非常感谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-24 10:01:16

这是你的解决办法。我希望您可以在组件(JCalendar)中使用这种方法。

代码语言:javascript
复制
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class ListOptionTest {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                String[] data = new String[10];
                for (int i = 0; i < 10; i++) {
                    data[i] = "Item " + (i + 1);
                }
                final JList<String> list = new JList<>(data);
                list.addListSelectionListener(new ListSelectionListener() {

                    @Override
                    public void valueChanged(ListSelectionEvent e) {
                        if (!e.getValueIsAdjusting()) {
                            System.out.println("Current selection: " + list.getSelectedValuesList());
                        }
                    }
                });
                int result = JOptionPane.showConfirmDialog(null, new JScrollPane(list), "Make your choice",
                        JOptionPane.OK_CANCEL_OPTION);
                if (result == JOptionPane.OK_OPTION) {
                    System.out.println("Final selection: " + list.getSelectedValuesList());
                } else {
                    System.out.println("No choice");
                }
            }
        });
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45850504

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档