日期不可为空

huangapple go评论80阅读模式
英文:

Date Must not be null

问题

我想在我的应用程序中使用DateChooser。我已经安装并在我的代码中实现了所有内容,没有编译错误,但是当我尝试运行代码时,控制台显示错误消息,说"日期不能为Null"。我做错了什么?我需要首先实现一个"基础"日期吗?

kalender = new JPanel();
kalender.setBackground(new Color(135, 206, 235));
kalender.setBounds(0, 0, 506, 250);
LayeredPane.add(kalender);
kalender.setLayout(null);

Termin = new Termin();

SimpleDateFormat sdl = new SimpleDateFormat("dd-MM-yyyy");
JDateChooser dateChooser = new JDateChooser();
dateChooser.setBounds(169, 119, 70, 20);
Termin.setDate(sdl.format(dateChooser.getDate())); // 错误可能在这里,因为没有这行,一切都能正常工作
kalender.add(dateChooser);
System.out.println(Termin);

JButton btnNext3 = new JButton("Next");
btnNext3.setBounds(215, 200, 70, 20);
kalender.add(btnNext3);
btnNext3.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e) {
        LayeredPane.moveToFront(kontakt);
        kalender.removeAll();
    }

});

所以,我有一个单独的类"Termin"来定义约会的所有属性,我想将日期属性设置为用户在Date Chooser中选择的日期。

我尝试通过硬编码日期属性来设置日期,但是当用户使用Date Chooser进行选择时,它不会更改。

英文:

I want to use the DateChooser in my application. I have already installed and implemented everything in my code and there are no compilation errors, but when I try to run the code the console shows an error that, "Date Must not be Null". What am I doing wrong? Do I have to implement a "base" date first?

kalender = new JPanel();
kalender.setBackground(new Color(135, 206, 235));
kalender.setBounds(0, 0, 506, 250);
LayeredPane.add(kalender);
kalender.setLayout(null);
	
Termin = new Termin();
	
SimpleDateFormat sdl = new SimpleDateFormat("dd-MM-yyyy");
JDateChooser dateChooser = new JDateChooser();
dateChooser.setBounds(169, 119, 70, 20);
Termin.setDate(sdl.format(dateChooser.getDate())); // the erro must be here because with out this     
kalender.add(dateChooser);                          //line everything works
System.out.println(Termin);
	
JButton btnNext3 = new JButton("Next");
btnNext3.setBounds(215, 200, 70, 20);
kalender.add(btnNext3);
btnNext3.addActionListener(new ActionListener() {

		public void actionPerformed(ActionEvent e) {
			LayeredPane.moveToFront(kontakt);
			kalender.removeAll();
		}
		
	});

So I have a separate Class "Termin" to define all the attributes of an appointment, and I want to set the attributes date to whatever the user chooses in the Date Chooser.

I have tried to set the date in the attribute by hard coding it, but then it would not change when the user makes a selection with the date chooser.

答案1

得分: 2

在这行代码中:

Termin.setDate(sdl.format(dateChooser.getDate()));

因为尚未选择日期,dateChooser.getDate() 返回 null。您需要检测用户何时更改了日期。您可以使用以下代码,就像这个 Stack Overflow 页面中所示:https://stackoverflow.com/questions/4156305/is-it-possible-to-detect-a-date-change-on-a-jcalendar-jdatechooser-field

代码应该类似于:

dateChooser.getDateEditor().addPropertyChangeListener(
    new PropertyChangeListener() {
        @Override
        public void propertyChange(PropertyChangeEvent e) {
           if ("date".equals(e.getPropertyName())) {
                java.util.Date date = (java.util.Date)e.getNewValue();
                Termin.setDate(sdl.format(date));
            }
        }
    }
);
英文:

In this line:

Termin.setDate(sdl.format(dateChooser.getDate()));

dateChooser.getDate() returns null because because no date has yet been selected. You need to detect when user has changed the date. You should use code as in this SO: https://stackoverflow.com/questions/4156305/is-it-possible-to-detect-a-date-change-on-a-jcalendar-jdatechooser-field

It should look something like:

dateChooser.getDateEditor().addPropertyChangeListener(
    new PropertyChangeListener() {
        @Override
        public void propertyChange(PropertyChangeEvent e) {
           if ("date".equals(e.getPropertyName())) {
                java.util.Date date = (java.util.Date)e.getNewValue();
                Termin.setDate(sdl.format(date));
            }
        }
    }
);

huangapple
  • 本文由 发表于 2023年7月11日 03:31:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76656779.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定