欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java編寫實(shí)現(xiàn)窗體程序顯示日歷

 更新時(shí)間:2022年06月13日 10:56:00   作者:秋刀山名魚、  
這篇文章主要為大家詳細(xì)介紹了Java編寫實(shí)現(xiàn)窗體程序顯示日歷,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Java實(shí)現(xiàn)窗體程序顯示日歷的具體代碼,供大家參考,具體內(nèi)容如下

實(shí)訓(xùn)要求:

代碼:

Test類:

import java.awt.*; ?
import java.awt.event.*; ?
import javax.swing.*; ?
??
public class Test extends JFrame { ?
? ? JButton week1, week2, week3, week4, week5, week6, week7, next, pro; ?
? ? CalendaBean cb = new CalendaBean(); ?
? ? JLabel[] label; ?
? ? JLabel now; ?
??
? ? public static void main(String[] args) { ?
? ? ? ? Test frame = new Test(); ?
? ? ? ? frame.setBounds(650,300,550,550);?
? ? ? ? frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ?
? ? ? ? frame.setTitle("日歷"); ?
? ? ? ? frame.setVisible(true); ?
??
? ? } ?
??
? ? public Test() { ?
? ? ? ? int year, month; ?
? ? ? ? setLayout(new BorderLayout()); ?
? ? ? ? JPanel pNorth = new JPanel(); ?
? ? ? ? cb = new CalendaBean(); ?
? ? ? ? cb.setYear(2017); ?
? ? ? ? cb.setMonth(11); ?
? ? ? ? String[] a = cb.getCalendar(); ?
? ? ? ? next = new JButton("上月"); ?
? ? ? ? pro = new JButton("下月"); ?
? ? ? ? next.setActionCommand("lastmonth"); ?
? ? ? ? pro.setActionCommand("nextmonth"); ?
? ? ? ? next.addActionListener(new ActionListener() { ?
? ? ? ? ? ? public void actionPerformed(ActionEvent e) { ?
? ? ? ? ? ? ? ? cb.actionPerformed(e); ?
? ? ? ? ? ? } ?
? ? ? ? }); ?
? ? ? ? pro.addActionListener(new ActionListener() { ?
? ? ? ? ? ? public void actionPerformed(ActionEvent e) { ?
? ? ? ? ? ? ? ? cb.actionPerformed(e); ?
? ? ? ? ? ? } ?
? ? ? ? }); ?
? ? ? ? pNorth.add(next); ?
? ? ? ? pNorth.add(pro); ?
? ? ? ? add(pNorth, BorderLayout.NORTH); ?
? ? ? ? GridLayout grid = new GridLayout(8, 7); ?
? ? ? ? JPanel pCenter = new JPanel(); ?
? ? ? ? week1 = new JButton("日"); ?
? ? ? ? week2 = new JButton("一"); ?
? ? ? ? week3 = new JButton("二"); ?
? ? ? ? week4 = new JButton("三"); ?
? ? ? ? week5 = new JButton("四"); ?
? ? ? ? week6 = new JButton("五"); ?
? ? ? ? week7 = new JButton("六"); ?
? ? ? ? pCenter.add(week1); ?
? ? ? ? pCenter.add(week2); ?
? ? ? ? pCenter.add(week3); ?
? ? ? ? pCenter.add(week4); ?
? ? ? ? pCenter.add(week5); ?
? ? ? ? pCenter.add(week6); ?
? ? ? ? pCenter.add(week7); ?
? ? ? ? label = new JLabel[42]; ?
? ? ? ? for (int i = 0; i < 42; i++) { ?
? ? ? ? ? ? label[i] = new JLabel(); ?
? ? ? ? ? ? pCenter.add(label[i]); ?
? ? ? ? } ?
? ? ? ? cb.label = this.label; ?
? ? ? ? for (int i = 0; i < a.length; i++) { ?
? ? ? ? ? ? if (i % 7 == 0) { ?
? ? ? ? ? ? ? ? label[i].setText(""); ?
? ? ? ? ? ? } ?
? ? ? ? ? ? label[i].setText(" ? ? ? ? ?"+a[i]); ?
? ? ? ? } ?
? ? ? ? pCenter.setLayout(grid); ?
? ? ? ? add(pCenter, BorderLayout.CENTER); ?
? ? ? ? JPanel pSouth = new JPanel(); ?
? ? ? ? now = new JLabel(); ?
? ? ? ? now.setText("日歷:" + cb.year + "年" + cb.month + "月"); ?
? ? ? ? cb.now = now; ?
? ? ? ? pSouth.add(now); ?
? ? ? ? add(pSouth, BorderLayout.SOUTH); ?
? ? } ?
??
}?

CalendaBean類:

import java.awt.event.ActionEvent; ?
import java.awt.event.ActionListener; ?
import java.util.Calendar; ?
??
import javax.swing.*; ?
public class CalendaBean implements ActionListener { ?
? ? JLabel[] label; ?
? ? JLabel now; ?
? ? String[] day; ?
? ? int year = 0, month = 0; ?
? ? public void setYear(int year) { ?
? ? ? ? this.year = year; ?
? ? } ?
??
? ? public void setMonth(int month) { ?
? ? ? ? this.month = month; ?
? ? } ?
??
? ? public void actionPerformed(ActionEvent e) { ?
? ? ? ? String str = e.getActionCommand(); ?
? ? ? ? if (str.equals("lastmonth")) { ?
? ? ? ? ? ? month--; ?
? ? ? ? ? ? if (month == 0) { ?
? ? ? ? ? ? ? ? month = 12; ?
? ? ? ? ? ? ? ? year--; ?
? ? ? ? ? ? } ?
? ? ? ? } ?
? ? ? ? else if (str.equals("nextmonth")) { ?
? ? ? ? ? ? month++; ?
? ? ? ? ? ? if (month == 13) { ?
? ? ? ? ? ? ? ? month = 1; ?
? ? ? ? ? ? ? ? year++; ?
? ? ? ? ? ? } ?
? ? ? ? } ?
? ? ? ? now.setText("日歷:" + year + "年" + month + "月"); ?
? ? ? ? String[] a = getCalendar(); ?
? ? ? ? for (int i = 0; i < a.length; i++) { ?
? ? ? ? ? ? if (i % 7 == 0) { ?
? ? ? ? ? ? ? ? label[i].setText(""); ?
? ? ? ? ? ? } ?
? ? ? ? ? ? label[i].setText(" ? ? ? ? ?"+a[i]); ?
? ? ? ? } ?
? ? ? ? ??
? ? } ?
??
? ? public String[] getCalendar() { ?
? ? ? ? String[] a = new String[42]; ?
? ? ? ? Calendar rili = Calendar.getInstance(); ?
? ? ? ? rili.set(year, month - 1, 1); ?
? ? ? ? int weekDay = rili.get(Calendar.DAY_OF_WEEK) - 1; ?
? ? ? ? int day = 0; ?
? ? ? ? if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 ?
? ? ? ? ? ? ? ? || month == 10 || month == 12) { ?
? ? ? ? ? ? day = 31; ?
? ? ? ? } ?
? ? ? ? if (month == 4 || month == 6 || month == 9 || month == 11) { ?
? ? ? ? ? ? day = 30; ?
? ? ? ? } ?
? ? ? ? if (month == 2) { ?
? ? ? ? ? ? if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) ?
? ? ? ? ? ? ? ? day = 29; ?
? ? ? ? ? ? else ?
? ? ? ? ? ? ? ? day = 28; ?
? ? ? ? } ?
? ? ? ? for (int i = 0; i < weekDay; i++) ?
? ? ? ? ? ? a[i] = ""; ?
? ? ? ? for (int i = weekDay, n = 1; i < weekDay + day; i++) { ?
? ? ? ? ? ? a[i] = String.valueOf(n); ?
? ? ? ? ? ? n++; ?
? ? ? ? } ?
? ? ? ? for (int i = weekDay + day; i < a.length; i++) ?
? ? ? ? ? ? a[i] = ""; ?
? ? ? ? return a; ?
? ? } ?
}?

運(yùn)行結(jié)果:

小結(jié):

學(xué)習(xí)了Calendar類,其他的,界面的話順著來就好。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java中MessageFormat的使用詳解

    Java中MessageFormat的使用詳解

    本文主要介紹了Java中MessageFormat的使用詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • Java之一文詳解String字符串的用法

    Java之一文詳解String字符串的用法

    本文將給大家重點(diǎn)講解一下String的用法,因?yàn)檫@個(gè)太常用,也太??剂恕tring字符串的內(nèi)容是比較多的,需要初學(xué)者進(jìn)行專門的學(xué)習(xí),尤其是它的一些底層原理更需要我們來了解,需要的同學(xué)跟著小編一起學(xué)習(xí)吧
    2023-05-05
  • 關(guān)于如何正確地定義Java內(nèi)部類方法詳解

    關(guān)于如何正確地定義Java內(nèi)部類方法詳解

    在Java中,我們通常是把不同的類創(chuàng)建在不同的包里面,對(duì)于同一個(gè)包里的類來說,它們都是同一層次的,但其實(shí)還有另一種情況,有些類可以被定義在另一個(gè)類的內(nèi)部,本文將詳細(xì)帶你了解如何正確地定義Java內(nèi)部類,需要的朋友可以參考下
    2023-05-05
  • 解決使用httpclient傳遞json數(shù)據(jù)亂碼的問題

    解決使用httpclient傳遞json數(shù)據(jù)亂碼的問題

    這篇文章主要介紹了解決使用httpclient傳遞json數(shù)據(jù)亂碼的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • SpringBoot項(xiàng)目@Async方法問題解決方案

    SpringBoot項(xiàng)目@Async方法問題解決方案

    這篇文章主要介紹了SpringBoot項(xiàng)目@Async方法問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • Java的四種引用方式

    Java的四種引用方式

    這篇文章主要介紹了Java的四種引用方式,Java的引用方式主要包括強(qiáng)引用、軟引用、弱引用、虛引用;下面文章便來詳細(xì)介紹這四種引用方式,需要的朋友可以參考一下
    2021-10-10
  • Springboot全局異常捕獲及try catch區(qū)別解析

    Springboot全局異常捕獲及try catch區(qū)別解析

    這篇文章主要介紹了Springboot全局異常捕獲及try catch區(qū)別解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • java實(shí)現(xiàn)簡(jiǎn)單的ATM項(xiàng)目

    java實(shí)現(xiàn)簡(jiǎn)單的ATM項(xiàng)目

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)單的ATM項(xiàng)目,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-10-10
  • SpringBoot整合neo4j使用詳解

    SpringBoot整合neo4j使用詳解

    這篇文章將和大家詳細(xì)聊聊如何在springboot應(yīng)用中集成和使用neo4j,文中有詳細(xì)的實(shí)現(xiàn)流程和實(shí)例代碼,具有一定的借鑒價(jià)值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-10-10
  • 詳解Java中的線程模型與線程調(diào)度

    詳解Java中的線程模型與線程調(diào)度

    這篇文章主要介紹了詳解Java中的線程模型與線程調(diào)度的相關(guān)資料,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2021-02-02

最新評(píng)論