我们java实训做的简单小日历,运行结果如图。
功能:
-
实现输入年份月份显示当前年的该月的日历,并且加了检测(年份不能小于1900,月份必须在1月到12月之间)
-
按上月和下月可以查询当前显示月份的上下月,并加检测(当1月时按上月显示去年12月,12月按下月显示下年1月)
-
无论用户输入的年月,还是通过按钮选择的月份,通过标签准确显示面板上的选择年月日(还可以根据按钮选择对应的日)
-
右上角的标签能准确显示当前 时、分、秒
-
能以灰色字体和灰色按钮,显示上个月的后几天和下个月的前几天(并且上下月份按钮不能点击)
程序代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class MyCalendar extends JFrame implements ActionListener {
private JPanel p1,p2,p3; //三个面板 p1最上面 p2中间 p3下面
private JLabel yearStr,monthStr; //标签
private JTextField inputYear,inputMonth;
private JButton confirm; //确认
private JButton lastMonth;
private JButton nextMonth;
private JLabel dayText;//文本框
private JLabel TimeText;//文本框
//p2面板里控件的声明
private String[] week = {"日","一","二","三","四","五","六"};
private JLabel[] weekLable = new JLabel[week.length];//数组的声明
//p3面板的42个按钮声明
private JButton[] dayBtn = new JButton[42];
private Calendar nowDate = new GregorianCalendar(); //Calendar是抽象类 new不出 用直接子类
private int nowYear = nowDate.get(Calendar.YEAR);
private int nowMonth = nowDate.get(Calendar.MONTH);
public MyCalendar() throws HeadlessException {
p1=new JPanel();
p2=new JPanel();
p3=new JPanel();
yearStr = new JLabel("Year:");
inputYear = new JTextField(4);
monthStr = new JLabel(" Month:");
inputMonth = new JTextField(3);
confirm = new JButton("确认");
lastMonth = new JButton("上月");
nextMonth = new JButton("下月");
dayText = new JLabel();
TimeText = new JLabel();
new Thread(){ //线程内部类用来实时显示时间
public void run(){
while(true) {
LocalDateTime dateTime = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss"); //大写的HH是24小时制的
String nowTime = dateTime.format(formatter);
TimeText.setText(nowTime);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
//初始化的方法
public void init(){
this.setTitle("日历");
this.setBounds(400,260,500,270);
this.setLayout(new BorderLayout());
this.setLayout(new BorderLayout());
this.add(p1,BorderLayout.NORTH);
this.add(p2,BorderLayout.CENTER);
this.add(p3,BorderLayout.SOUTH);
//最上面的面板p1
p1.setLayout(new FlowLayout());//流布局默认居中
p1.add(yearStr);
p1.add(inputYear);
inputYear.setText(nowYear+"");
p1.add(monthStr);
p1.add(inputMonth);
inputMonth.setText(nowMonth+1+"");
p1.add(confirm);
confirm.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int inputyear = Integer.parseInt(inputYear.getText());
if(inputyear<=1900){
JOptionPane.showMessageDialog(null,"请输入正确的年份(1900-2300");
inputYear.setText(null);
return;
}
int inputmonth = Integer.parseInt(inputMonth.getText());
if (inputmonth<=0||inputmonth>=13){
JOptionPane.showMessageDialog(null,"请输入正确的月份(1-12)");
inputMonth.setText(null);
return;
}
for (int i=0;i<42;i++){
dayBtn[i].setEnabled(true);
dayBtn[i].setForeground(Color.black);
dayBtn[i].setBackground(new Color(240, 239, 239));
}
setDay(inputyear,inputmonth);
dayText.setText(inputYear.getText()+"/"+inputMonth.getText()+"/"+nowDate.get(Calendar.DAY_OF_MONTH)+" ");
}
});
p1.add(lastMonth);
lastMonth.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for (int i=0;i<42;i++){
dayBtn[i].setEnabled(true);
dayBtn[i].setForeground(Color.black);
dayBtn[i].setBackground(new Color(240, 239, 239));
}
if ((Integer.parseInt(inputMonth.getText()))==1){
setDay(Integer.parseInt(inputYear.getText())-1,12);
inputYear.setText(Integer.parseInt(inputYear.getText())-1+"");
inputMonth.setText(12+"");
}else{
setDay(Integer.parseInt(inputYear.getText()),Integer.parseInt(inputMonth.getText())-1);
inputMonth.setText(Integer.parseInt(inputMonth.getText())-1+"");
}
dayText.setText(inputYear.getText()+"/"+inputMonth.getText()+"/"+nowDate.get(Calendar.DAY_OF_MONTH)+" ");
}
});
p1.add(nextMonth);
nextMonth.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for (int i=0;i<42;i++){
dayBtn[i].setEnabled(true);
dayBtn[i].setForeground(Color.black);
dayBtn[i].setBackground(new Color(240, 239, 239));
}
if ((Integer.parseInt(inputMonth.getText()))==12) {
setDay(Integer.parseInt(inputYear.getText()) + 1, 1);
inputYear.setText(Integer.parseInt(inputYear.getText()) + 1 + "");
inputMonth.setText(1 + "");
}else{
setDay(Integer.parseInt(inputYear.getText()),Integer.parseInt(inputMonth.getText())+1);
inputMonth.setText(Integer.parseInt(inputMonth.getText())+1+"");
}
dayText.setText(inputYear.getText()+"/"+inputMonth.getText()+"/"+nowDate.get(Calendar.DAY_OF_MONTH)+" ");
}
});
p1.add(dayText);
p1.add(TimeText);
dayText.setText(nowYear+"/"+(nowMonth+1)+"/"+nowDate.get(Calendar.DAY_OF_MONTH)+" ");
//中间的面板p2
p2.setLayout(new GridLayout(1,7,2,2)); //1行7列上下间距2
//面板里控件赋值
for (int i=0;i<weekLable.length;i++){
weekLable[i] = new JLabel(week[i]);
weekLable[i].setHorizontalAlignment(SwingConstants.CENTER);//文字水平居中
weekLable[i].setFont(new Font("黑体",Font.PLAIN,13));
p2.add(weekLable[i]);
}
//下面的面板p3
p3.setLayout(new GridLayout(6,7,2,2));
//p3的按钮加进去
for (int i=0;i<42;i++){
dayBtn[i]=new JButton(""+i); //暴力创建 强行改字符串
dayBtn[i].addActionListener(this);//给每个事件监听
p3.add(dayBtn[i]);
}
setDay(Integer.parseInt(inputYear.getText()),Integer.parseInt(inputMonth.getText()));
this.setVisible(true); //显示窗口
this.setResizable(false);//窗口不可拉伸
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//按xx可以退出
}
//确定日历上每天的方法
public void setDay(int yearSel,int monthSel){
GregorianCalendar dateSel = new GregorianCalendar(yearSel,monthSel-1,1); //构造器传选择那个月的1号
int weeknum = dateSel.get(Calendar.DAY_OF_WEEK); //然后通过方法获取选择月1号是星期几 要减去1 外国人从星期天开始
boolean isLeep = dateSel.isLeapYear(yearSel); //dateSel里面的方法:传入年,即可判断是否为闰年
int dayNum = getMonthDay(isLeep,monthSel); //选择该年该月的天数
int lastdayNum = getMonthDay(isLeep,monthSel-1);
//根据某月的天数 以及某月的1号是星期几,改变按钮上的字
//当前月1号之前的
int count1 = lastdayNum;
for (int i=weeknum-2;i>=0;i--,count1--){
dayBtn[i].setText(Integer.toString(count1));
dayBtn[i].setEnabled(false);//按钮不可以按
dayBtn[i].setForeground(Color.gray); //设置字体颜色为灰色
}
//当前月的日历
int count2 = 1;
for (int i=weeknum-1;i<weeknum-1+dayNum;i++,count2++){
dayBtn[i].setText(Integer.toString(count2));
dayBtn[i].setBackground(Color.WHITE);
}
//当前月之后的
int count3= 1 ;
for (int i=weeknum-1+dayNum;i<42;i++,count3++){
dayBtn[i].setText(Integer.toString(count3));
dayBtn[i].setEnabled(false);//按钮不可以按
dayBtn[i].setForeground(Color.gray);//设置字体颜色为灰色
}
}
//得到该年该月是多少天
private int getMonthDay(boolean isleepYear,int month){
int daynum = 0;
if (isleepYear){ //是闰年
if (month==2){
daynum = 29;
}else if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){
daynum = 31;
}else{ //小月
daynum = 30;
}
}else{
if (month==2){
daynum = 28;
}else if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){
daynum = 31;
}else{ //小月
daynum = 30;
}
}
return daynum;
}
@Override
public void actionPerformed(ActionEvent e) {
for (int i=0;i<42;i++){
if (e.getSource()==dayBtn[i]){ //事件源点了
dayText.setText(inputYear.getText()+"/"+inputMonth.getText()+"/"+dayBtn[i].getText()+" ");
}
}
}
}
测试类:
public class CalenderTest {
public static void main(String[] args) {
new MyCalendar().init();
}
}
如果对大家有用的话,麻烦点赞、收藏谢谢。当然如果能关注就更好啦。
我还会发一些平时做的小项目。