Java實現(xiàn)萬年歷效果
本文實例為大家分享了Java實現(xiàn)萬年歷效果的具體代碼,供大家參考,具體內容如下
要求:
綜合運用GUI編程、事件處理、Calendar類應用等知識設計一款月歷,要求能通過輸入(或選擇)年月的方式正確顯示當前月份的所有日期。

一、分析需求:
1、設計一個窗體,該窗體為BorderLayout布局;
2、窗體的中心添加一個Panel容器:Panel容器的布局是7行7列的GridLayout布局,Panel容器中放置49個標簽,用來顯示日歷;
3、實現(xiàn)判斷是否是閏年;
4、計算某一個月的天數(shù);
5、明白一個月的第一天是周幾,求出月歷前面需要空的格數(shù);
二、代碼實現(xiàn)
package pro_one;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
@SuppressWarnings("serial")
public class pro_one extends JFrame implements ActionListener {
?? ?JPanel head=new JPanel();//上部容器
? ? JPanel body=new JPanel();//中部容器
? ? JPanel foot=new JPanel();//下部容器
? ? Calendar calendar=Calendar.getInstance();
? ? int dayNow=calendar.get(Calendar.DATE);
? ? int monthNow=calendar.get(Calendar.MONTH)+1;
? ? int yearNow=calendar.get(Calendar.YEAR);
? ??
? ? private JTextField text;//查詢年份
? ? int year = calendar.get(Calendar.YEAR);//獲取當前查詢年份,默認為當前年份
? ? private JTextField text1;//查詢月份
? ? int month = calendar.get(Calendar.MONTH)+1;//獲取當前查詢月份,默認為當前月份
? ??
? ? private pro_one(){//構造方法
? ? ? ? //主要參數(shù)設置
? ? ? ? setTitle("月歷");
? ? ? ? setSize(600,480);
? ? ? ? setLocationRelativeTo(null);//窗體居中
? ? ? ? setResizable(false);//關閉窗體大小可調
? ? ? ? setDefaultCloseOperation(EXIT_ON_CLOSE);
? ? ? ? //界面布局
? ? ? ? //上部容器
? ? ? ? head.setLayout(new FlowLayout());
? ? ? ? JButton searchJButton=new JButton("確定");
? ? ? ? searchJButton.setFont(new Font("SimHei",Font.BOLD,15));
? ? ? ? searchJButton.setPreferredSize(new Dimension(50,35));
? ? ? ? searchJButton.setBorder(null);
? ? ? ? JButton upJButton=new JButton("上月");
? ? ? ? upJButton.setFont(new Font("SimHei",Font.BOLD,15));
? ? ? ? upJButton.setBorder(null);
? ? ? ? upJButton.setPreferredSize(new Dimension(50,35));
? ? ? ? JButton downJButton=new JButton("下月");
? ? ? ? downJButton.setFont(new Font("SimHei",Font.BOLD,15));
? ? ? ? downJButton.setBorder(null);
? ? ? ? downJButton.setPreferredSize(new Dimension(50,35));
? ? ? ? JLabel jLabelShow=new JLabel("請輸入日期 年份:");
? ? ? ? jLabelShow.setFont(new Font("SimHei",Font.BOLD,15));
? ? ? ? JLabel jLabelShow1=new JLabel("月份:");
? ? ? ? jLabelShow1.setFont(new Font("SimHei",Font.BOLD,15));
? ? ? ??
? ? ? ? text=new JTextField(4);
? ? ? ? text1=new JTextField(2);
? ? ? ? head.add(jLabelShow);
? ? ? ? head.add(text);
? ? ? ? head.add(jLabelShow1);
? ? ? ? head.add(text1);
? ? ? ? head.add(searchJButton);
? ? ? ? head.add(upJButton);
? ? ? ? head.add(downJButton);
? ? ? ? searchJButton.addActionListener(this);
? ? ? ? upJButton.addActionListener(this);
? ? ? ? downJButton.addActionListener(this);
? ? ? ? //中部容器
? ? ? ? body.applyComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);//每行添加組件的順序
? ? ? ? body.setLayout(new GridLayout(7,7,1,1));
? ? ? ? getDateInfo(String.valueOf(year)+"-"+String.valueOf(month));
? ? ? ? //下部容器
? ? ? ? foot.setSize(new Dimension(500,200));
? ? ? ? foot.setLayout(new FlowLayout(FlowLayout.CENTER));
? ? ? ? Container integralContainer=this.getContentPane();//創(chuàng)建全局容器
? ? ? ? integralContainer.add(head,BorderLayout.NORTH);
? ? ? ? integralContainer.add(body,BorderLayout.CENTER);
? ? ? ? integralContainer.add(foot,BorderLayout.SOUTH);
? ? }
? ? @Override
? ? public void actionPerformed(ActionEvent actionEvent) {//監(jiān)聽事件
? ? ? ? String label=actionEvent.getActionCommand();
? ? ? ? switch (label) {
? ? ? ? ? ? case "確定":
? ? ? ? ? ? ? ? System.out.println("進入查詢");
? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? year = Integer.parseInt(text.getText());
?? ??? ??? ??? ??? ?month=Integer.parseInt(text1.getText());
? ? ? ? ? ? ? ? ? ? getDateInfo(String.valueOf(year)+"-"+String.valueOf(month));
? ? ? ? ? ? ? ? }catch (NumberFormatException e){
? ? ? ? ? ? ? ? ? ? System.out.println("非數(shù)字異常已被捕獲,進程正常!");
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case "上月":
? ? ? ? ? ? ? ? System.out.println("進入上月");
? ? ? ? ? ? ? ? if (month==1){
? ? ? ? ? ? ? ? ? ? year--;
? ? ? ? ? ? ? ? ? ? month=12;
? ? ? ? ? ? ? ? }else
? ? ? ? ? ? ? ? ? ? month--;
? ? ? ? ? ? ? ? getDateInfo(String.valueOf(year)+"-"+String.valueOf(month));
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case "下月":
? ? ? ? ? ? ? ? System.out.println("進入下月");
? ? ? ? ? ? ? ? if (month==12){
? ? ? ? ? ? ? ? ? ? year++;
? ? ? ? ? ? ? ? ? ? month=1;
? ? ? ? ? ? ? ? }else
? ? ? ? ? ? ? ? ? ? month++;
? ? ? ? ? ? ? ? getDateInfo(String.valueOf(year)+"-"+String.valueOf(month));
? ? ? ? ? ? ? ? break;
? ? ? ? }
? ? }
? ? private void getDateInfo(String date) {//獲取日期信息
? ? ? ? try {
? ? ? ? ? ? SimpleDateFormat dFormat = new SimpleDateFormat("yyyy-MM");// 日期格式化類
? ? ? ? ? ? Date parse = dFormat.parse(date);// 把字符串類型的日期轉換為date類型的
? ? ? ? ? ? Calendar calendar = new GregorianCalendar();// 創(chuàng)建一個公歷類的實例
? ? ? ? ? ? calendar.setTime(parse);// 把格式化好的日期對象放進Calendar
? ? ? ? ? ? calendar.set(Calendar.DATE, 1);//重置日期為第一天
? ? ? ? ? ? // 獲取這個月的第一天是周幾
? ? ? ? ? ? int weekDay = calendar.get(Calendar.DAY_OF_WEEK);
? ? ? ? ? ? // 獲取每個月最大的天數(shù)
? ? ? ? ? ? int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
? ? ? ? ? ? body.removeAll();
? ? ? ? ? ? body.repaint();
? ? ? ? ? ? String[] title = {"日", "一", "二", "三", "四", "五", "六"};
? ? ? ? ? ? for (String label : title) {
? ? ? ? ? ? ? ? JLabel jLabel = new JLabel(label);
? ? ? ? ? ? ? ? jLabel.setHorizontalAlignment(JLabel.CENTER);
?? ??? ??? ??? ?jLabel.setBorder(BorderFactory.createLineBorder(Color.black));
? ? ? ? ? ? ? ? jLabel.setFont(new Font("SimHei", Font.BOLD, 18));
? ? ? ? ? ? ? ? body.add(jLabel);
? ? ? ? ? ? ? ? body.revalidate();
? ? ? ? ? ? }
? ? ? ? ? ? for (int i = 1; i <= 42; i++) {
? ? ? ? ? ? ? ? if (i >= weekDay && i <= (maxDay + weekDay - 1)) {
? ? ? ? ? ? ? ? ? ? JLabel jLabel = new JLabel(String.valueOf(i - weekDay + 1));
? ? ? ? ? ? ? ? ? ? jLabel.setFont(new Font("SimHei", Font.BOLD, 15));
? ? ? ? ? ? ? ? ? ? jLabel.setHorizontalAlignment(JLabel.CENTER);
? ? ? ? ? ? ? ? ? ? if ((year==yearNow)&&(month==monthNow)&&(i - weekDay + 1==dayNow)){
? ? ? ? ? ? ? ? ? ? ? ? System.out.println("00");
? ? ? ? ? ? ? ? ? ? ? ? jLabel.setBorder(BorderFactory.createLineBorder(Color.WHITE));
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? body.add(jLabel);
? ? ? ? ? ? ? ? ? ? body.revalidate();
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? JLabel jLabel = new JLabel("");
? ? ? ? ? ? ? ? ? ? jLabel.setHorizontalAlignment(JLabel.CENTER);
? ? ? ? ? ? ? ? ? ? jLabel.setFont(new Font("SimHei", Font.BOLD, 15));
? ? ? ? ? ? ? ? ? ? body.add(jLabel);
? ? ? ? ? ? ? ? ? ? body.revalidate();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? if (year > 0 && year <= 9999) {
? ? ? ? ? ? ? ? foot.removeAll();
? ? ? ? ? ? ? ? foot.repaint();
? ? ? ? ? ? ? ? JLabel show = new JLabel(year + "年" + month + "月");
? ? ? ? ? ? ? ? show.setFont(new Font("SimHei", Font.BOLD, 20));
? ? ? ? ? ? ? ? foot.add(show);//將標簽添加到下部容器
? ? ? ? ? ? ? ? foot.revalidate();
? ? ? ? ? ? }
? ? ? ? }catch (ParseException e){
? ? ? ? ? ? System.out.println("日期異常亦已被捕獲,進程正常!");
? ? ? ? }
? ? }
? ? public static void main(String[] args){//主方法
? ? ? ? JFrame jFrame=new pro_one();
? ? ? ? jFrame.setVisible(true);
? ? }
}以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
SpringBoot整合Redis實現(xiàn)緩存分頁數(shù)據(jù)查詢功能
類似淘寶首頁,這些商品是從數(shù)據(jù)庫中查出來的嗎,答案肯定不是,本文我們就通過一個案例實操一下,首頁熱點數(shù)據(jù)怎么放到Redis中去查詢,感興趣的同學可以參考一下2023-06-06
Quarkus中RESTEasy?Reactive集成合并master分支
這篇文章主要為大家介紹了Quarkus中RESTEasy?Reactive集成合并master分支的詳細作用分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2022-02-02

