1 回答
TA贡献1829条经验 获得超4个赞
您可以JCalendar从中获取JDateChooser并自定义它以获得所需的结果。
JDateChooser date = new JDateChooser(new Date());
date.getJCalendar().getDayChooser().addDateEvaluator(new MyDateEvaluator());
您可以设置一个IDateEvaluator允许全部Sundays禁用的自定义。
@Override
public boolean isInvalid(Date date) {
return date.getDay() == 0;
}
这是禁用所有星期日的代码:
import com.toedter.calendar.IDateEvaluator;
import com.toedter.calendar.JDateChooser;
import javax.swing.*;
import java.awt.*;
import java.util.Date;
public class CustomizedDateChooser {
public static void main(String[] args) {
JFrame f = new JFrame("ComboBox Example");
JDateChooser date = new JDateChooser(new Date());
date.getJCalendar().getDayChooser().addDateEvaluator(new MyDateEvaluator());
date.setBounds(200,200,200,50);
JPanel p = new JPanel();
p.add(new JLabel("Choose a Date:"));
p.add(date);
f.add(p);
f.setLayout(new FlowLayout());
f.setSize(400, 500);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
private static class MyDateEvaluator implements IDateEvaluator {
@Override
public boolean isSpecial(Date date) {
return false;
}
@Override
public Color getSpecialForegroundColor() {
return null;
}
@Override
public Color getSpecialBackroundColor() {
return null;
}
@Override
public String getSpecialTooltip() {
return null;
}
@Override
public boolean isInvalid(Date date) {
return date.getDay() == 0;
}
@Override
public Color getInvalidForegroundColor() {
return null;
}
@Override
public Color getInvalidBackroundColor() {
return null;
}
@Override
public String getInvalidTooltip() {
return null;
}
}
}
添加回答
举报