Java實(shí)現(xiàn)萬(wàn)年歷效果
本文實(shí)例為大家分享了Java實(shí)現(xiàn)萬(wàn)年歷效果的具體代碼,供大家參考,具體內(nèi)容如下
要求:
綜合運(yùn)用GUI編程、事件處理、Calendar類(lèi)應(yīng)用等知識(shí)設(shè)計(jì)一款月歷,要求能通過(guò)輸入(或選擇)年月的方式正確顯示當(dāng)前月份的所有日期。
一、分析需求:
1、設(shè)計(jì)一個(gè)窗體,該窗體為BorderLayout布局;
2、窗體的中心添加一個(gè)Panel容器:Panel容器的布局是7行7列的GridLayout布局,Panel容器中放置49個(gè)標(biāo)簽,用來(lái)顯示日歷;
3、實(shí)現(xiàn)判斷是否是閏年;
4、計(jì)算某一個(gè)月的天數(shù);
5、明白一個(gè)月的第一天是周幾,求出月歷前面需要空的格數(shù);
二、代碼實(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;//查詢(xún)年份 ? ? int year = calendar.get(Calendar.YEAR);//獲取當(dāng)前查詢(xún)年份,默認(rèn)為當(dāng)前年份 ? ? private JTextField text1;//查詢(xún)?cè)路? ? ? int month = calendar.get(Calendar.MONTH)+1;//獲取當(dāng)前查詢(xún)?cè)路荩J(rèn)為當(dāng)前月份 ? ?? ? ? private pro_one(){//構(gòu)造方法 ? ? ? ? //主要參數(shù)設(shè)置 ? ? ? ? setTitle("月歷"); ? ? ? ? setSize(600,480); ? ? ? ? setLocationRelativeTo(null);//窗體居中 ? ? ? ? setResizable(false);//關(guān)閉窗體大小可調(diào) ? ? ? ? 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("請(qǐng)輸入日期 年份:"); ? ? ? ? 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)聽(tīng)事件 ? ? ? ? String label=actionEvent.getActionCommand(); ? ? ? ? switch (label) { ? ? ? ? ? ? case "確定": ? ? ? ? ? ? ? ? System.out.println("進(jìn)入查詢(xún)"); ? ? ? ? ? ? ? ? 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ù)字異常已被捕獲,進(jìn)程正常!"); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case "上月": ? ? ? ? ? ? ? ? System.out.println("進(jìn)入上月"); ? ? ? ? ? ? ? ? if (month==1){ ? ? ? ? ? ? ? ? ? ? year--; ? ? ? ? ? ? ? ? ? ? month=12; ? ? ? ? ? ? ? ? }else ? ? ? ? ? ? ? ? ? ? month--; ? ? ? ? ? ? ? ? getDateInfo(String.valueOf(year)+"-"+String.valueOf(month)); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case "下月": ? ? ? ? ? ? ? ? System.out.println("進(jìn)入下月"); ? ? ? ? ? ? ? ? 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");// 日期格式化類(lèi) ? ? ? ? ? ? Date parse = dFormat.parse(date);// 把字符串類(lèi)型的日期轉(zhuǎn)換為date類(lèi)型的 ? ? ? ? ? ? Calendar calendar = new GregorianCalendar();// 創(chuàng)建一個(gè)公歷類(lèi)的實(shí)例 ? ? ? ? ? ? calendar.setTime(parse);// 把格式化好的日期對(duì)象放進(jìn)Calendar ? ? ? ? ? ? calendar.set(Calendar.DATE, 1);//重置日期為第一天 ? ? ? ? ? ? // 獲取這個(gè)月的第一天是周幾 ? ? ? ? ? ? int weekDay = calendar.get(Calendar.DAY_OF_WEEK); ? ? ? ? ? ? // 獲取每個(gè)月最大的天數(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);//將標(biāo)簽添加到下部容器 ? ? ? ? ? ? ? ? foot.revalidate(); ? ? ? ? ? ? } ? ? ? ? }catch (ParseException e){ ? ? ? ? ? ? System.out.println("日期異常亦已被捕獲,進(jìn)程正常!"); ? ? ? ? } ? ? } ? ? public static void main(String[] args){//主方法 ? ? ? ? JFrame jFrame=new pro_one(); ? ? ? ? jFrame.setVisible(true); ? ? } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JAVA HTTP反向代理實(shí)現(xiàn)過(guò)程詳解
這篇文章主要介紹了JAVA HTTP反向代理實(shí)現(xiàn)過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06SpringBoot整合Redis實(shí)現(xiàn)緩存分頁(yè)數(shù)據(jù)查詢(xún)功能
類(lèi)似淘寶首頁(yè),這些商品是從數(shù)據(jù)庫(kù)中查出來(lái)的嗎,答案肯定不是,本文我們就通過(guò)一個(gè)案例實(shí)操一下,首頁(yè)熱點(diǎn)數(shù)據(jù)怎么放到Redis中去查詢(xún),感興趣的同學(xué)可以參考一下2023-06-06Quarkus中RESTEasy?Reactive集成合并master分支
這篇文章主要為大家介紹了Quarkus中RESTEasy?Reactive集成合并master分支的詳細(xì)作用分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-02-02SpringBoot詳細(xì)講解視圖整合引擎thymeleaf
這篇文章主要分享了Spring Boot整合使用Thymeleaf,Thymeleaf是新一代的Java模板引擎,類(lèi)似于Velocity、FreeMarker等傳統(tǒng)引擎,關(guān)于其更多相關(guān)內(nèi)容,需要的小伙伴可以參考一下2022-06-06