gpt4 book ai didi

java - JDatePicker 没有出现在 JFrame 上

转载 作者:行者123 更新时间:2023-12-01 11:47:30 25 4
gpt4 key购买 nike

晚安,大家好,我正在尝试在我的个人项目中实现 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/

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