Java 我如何实现 JDatePicker

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26794698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 03:18:24  来源:igfitidea点击:

How do I implement JDatePicker

javadatepicker

提问by Ashane Alvis

I am working with the latest release 1.3.4 of JDatePicker. How should it be implemented?

我正在使用 JDatePicker 的最新版本 1.3.4。应该如何实施?

I get a compiler error: The constructor JDatePanelImpl(UtilDateModel) is undefined. The suggested fix is to: add argument to match JDatePanelImpl(DateModel, Properties). What should be passed in as the properties argument?

我收到编译器错误:构造函数 JDatePanelImpl(UtilDateModel) 未定义。建议的修复方法是:添加参数以匹配 JDatePanelImpl(DateModel, Properties)。什么应该作为属性参数传入?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import java.util.Calendar;
import java.util.Date;
import org.jdatepicker.impl.*;
import org.jdatepicker.util.*;
import org.jdatepicker.*;

    //import org.jdatepicker.graphics.*;
class date2 {

    void GUI() {
        JFrame f1 = new JFrame();
        f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f1.setSize(300, 300);
        f1.setVisible(true);

        Container conn = f1.getContentPane();
        conn.setLayout(null);

        UtilDateModel model = new UtilDateModel();
        //model.setDate(20,04,2014);
        JDatePanelImpl datePanel = new JDatePanelImpl(model);
        JDatePickerImpl datePicker = new JDatePickerImpl(datePanel);
        f1.add(datePicker);

    }

}

...Runner...

...跑步者...

class testDate2 {

    public void main(String[] args) {

        date2 d1 = new date2();
        d1.GUI();

    }

}

采纳答案by MadProgrammer

Assuming you are using 1.3.4, then the constructor requirements have changed...

假设您使用的是 1.3.4,那么构造函数要求已更改...

UtilDateModel model = new UtilDateModel();
//model.setDate(20,04,2014);
// Need this...
Properties p = new Properties();
p.put("text.today", "Today");
p.put("text.month", "Month");
p.put("text.year", "Year");
JDatePanelImpl datePanel = new JDatePanelImpl(model, p);
// Don't know about the formatter, but there it is...
JDatePickerImpl datePicker = new JDatePickerImpl(datePanel, new DateLabelFormatter());

enter image description here

在此处输入图片说明

Using this AbstractFormatter...

使用这个AbstractFormatter...

public class DateLabelFormatter extends AbstractFormatter {

    private String datePattern = "yyyy-MM-dd";
    private SimpleDateFormat dateFormatter = new SimpleDateFormat(datePattern);

    @Override
    public Object stringToValue(String text) throws ParseException {
        return dateFormatter.parseObject(text);
    }

    @Override
    public String valueToString(Object value) throws ParseException {
        if (value != null) {
            Calendar cal = (Calendar) value;
            return dateFormatter.format(cal.getTime());
        }

        return "";
    }

}

回答by Ankur Saxena

Just use properties in the constructor of JDatePanelImpl

只需在构造函数中使用属性 JDatePanelImpl

Properties p = new Properties();
p.put("text.today", "Today");
p.put("text.month", "Month");
p.put("text.year", "Year");
JDatePanelImpl datePanel = new JDatePanelImpl(model, p);