java gui-设计日历软件

我们java实训做的简单小日历,运行结果如图。

java gui-设计日历软件_第1张图片

功能:

  • 实现输入年份月份显示当前年的该月的日历,并且加了检测(年份不能小于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=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

测试类: 

public class CalenderTest {
    public static void main(String[] args) {
        new MyCalendar().init();
    }
}

如果对大家有用的话,麻烦点赞、收藏谢谢。当然如果能关注就更好啦。

我还会发一些平时做的小项目。

你可能感兴趣的:(java)