java - JDatePicker 没有出现在 JFrame 上

标签 java swing date datepicker

晚安,大家好,我正在尝试在我的个人项目中实现 JDatePicker 库,我是这个库的初学者,我以前没有使用过它,我一直在使用它。阅读了一些示例和教程,但我没有在我的 JFrame 中显示此组件,如果你们能帮助我找到错误,我真的很感谢你们的帮助:

package Frames;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdatepicker.impl.JDatePanelImpl;
import org.jdatepicker.impl.JDatePickerImpl;
import org.jdatepicker.impl.UtilDateModel;


public class BillForm extends javax.swing.JFrame {

/**
 * Creates new form BillForm
 */
public BillForm() {
    initComponents();
    crearJDatePicker();
}


private void crearJDatePicker (){
    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());
    this.add(datePicker);
}

private Date getDate(){
    DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
    Date date = new Date();
    try {
        return dateFormat.parse(dateFormat.format(date));
    } catch (ParseException ex) {
        Logger.getLogger(BillForm.class.getName()).log(Level.SEVERE, null, ex);
    }
    return date;
}
public static void main(String args[]) {

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new BillForm().setVisible(true);
        }
    });
}

最佳答案

问题出在您的 initComponents 方法中,可能是使用了 GroupLayout,这是手动尝试和管理的最糟糕的布局管理器之一。

更好的选择是开始手动编写布局(或者至少将布局管理器更改为更容易更新的东西)

enter image description here

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Properties;
import javax.swing.JFormattedTextField.AbstractFormatter;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.jdatepicker.impl.JDatePanelImpl;
import org.jdatepicker.impl.JDatePickerImpl;
import org.jdatepicker.impl.UtilDateModel;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            UtilDateModel model = new UtilDateModel();
            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);
            JDatePickerImpl datePicker = new JDatePickerImpl(datePanel, new DateLabelFormatter());
            setLayout(new GridBagLayout());
            add(datePicker);
        }

    }

    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 "";
        }

    }
}

关于java - JDatePicker 没有出现在 JFrame 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29044080/

相关文章:

java - 从文本区域获取文本并在标签上将其设置为粗体

javascript - 创建日期对象并与今天的日期进行比较

python - 如何在 Django 日期范围过滤器中放置占位符

java - Tomcat 7 中错误代码 500 的自定义错误页面

java - 如何检查字符串数组特定单元格字符串的长度?

Java Swing : Controlling focus with textfield and autocompletion dialog/menu

swing - 调整绘图大小以匹配框架大小

javascript - AngularJS 日期过滤器不产生结果

java - getContentType 方法始终返回 'application/force-download'

java - Windows Server 2008 64位中的Tomcat 5.5 https配置