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

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

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

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

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

1.使用BorderLayout 進(jìn)行總體布局

2.在North 位置放置包含兩個按鈕( 上月和下月)的Panel

3.在South 位置放置一個Label 用于顯示當(dāng)前年份和月份

4.在Center 位置放置一個顯示日歷的Panel

5.顯示日歷的Panel 設(shè)置7 行7 列的GridLayout 布局,其中第1行放置7個按鈕顯示周“幾”,其他6 行放置42 個Label 用于顯示期。

6.啟動程序時日歷中默認(rèn)顯示當(dāng)前月份的日歷。

7.點(diǎn)擊“上月”和“下月”可翻看上個月和下個月的日歷。

8.程序運(yùn)行結(jié)果如下圖:

代碼:

CalendaBean.java

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++) {
?? ??? ??? ?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;
?? ?}
}

Test.java

import java.awt.*;
import java.awt.event.*;
?
import javax.swing.*;
?
public class Test extends JFrame {
?? ?JButton b1, b2, b3, b4, b5, b6, b7, bx, by;
?? ?CalendaBean cb = new CalendaBean();
?? ?JLabel[] label;
?? ?JLabel now;
?
?? ?public static void main(String[] args) {
?? ??? ?Test frame = new Test();
?? ??? ?frame.setSize(500, 400);
?? ??? ?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();
?? ??? ?bx = new JButton("上月");
?? ??? ?by = new JButton("下月");
?? ??? ?bx.setActionCommand("lastmonth");
?? ??? ?by.setActionCommand("nextmonth");
?? ??? ?bx.addActionListener(new ActionListener() {
?? ??? ??? ?public void actionPerformed(ActionEvent e) {
?? ??? ??? ??? ?cb.actionPerformed(e);
?? ??? ??? ?}
?? ??? ?});
?? ??? ?by.addActionListener(new ActionListener() {
?? ??? ??? ?public void actionPerformed(ActionEvent e) {
?? ??? ??? ??? ?cb.actionPerformed(e);
?? ??? ??? ?}
?? ??? ?});
?? ??? ?pNorth.add(bx);
?? ??? ?pNorth.add(by);
?? ??? ?add(pNorth, BorderLayout.NORTH);
?? ??? ?GridLayout grid = new GridLayout(7, 7);
?? ??? ?JPanel pCenter = new JPanel();
?? ??? ?b1 = new JButton("日");
?? ??? ?b2 = new JButton("一");
?? ??? ?b3 = new JButton("二");
?? ??? ?b4 = new JButton("三");
?? ??? ?b5 = new JButton("四");
?? ??? ?b6 = new JButton("五");
?? ??? ?b7 = new JButton("六");
?? ??? ?pCenter.add(b1);
?? ??? ?pCenter.add(b2);
?? ??? ?pCenter.add(b3);
?? ??? ?pCenter.add(b4);
?? ??? ?pCenter.add(b5);
?? ??? ?pCenter.add(b6);
?? ??? ?pCenter.add(b7);
?? ??? ?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++) {
?? ??? ??? ?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);
?? ?}
?
}

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

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

相關(guān)文章

  • Java詳解表格的創(chuàng)建與使用流程

    Java詳解表格的創(chuàng)建與使用流程

    這篇文章主要介紹了怎么用Java來創(chuàng)建和使用表格,表格是我們經(jīng)常要用的工具,但是你有想過自己怎么去實(shí)現(xiàn)它嗎,感興趣的朋友跟隨文章往下看看吧
    2022-04-04
  • Java編程在方法中哪些時候需要參數(shù)

    Java編程在方法中哪些時候需要參數(shù)

    這篇文章主要介紹了Java編程在方法中哪些時候需要參數(shù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Netty實(shí)現(xiàn)簡易版的RPC框架過程詳解

    Netty實(shí)現(xiàn)簡易版的RPC框架過程詳解

    這篇文章主要為大家介紹了Netty實(shí)現(xiàn)簡易版的RPC框架過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • 通過代碼實(shí)例了解SpringBoot啟動原理

    通過代碼實(shí)例了解SpringBoot啟動原理

    這篇文章主要介紹了通過代碼實(shí)例了解SpringBoot啟動原理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-12-12
  • SpringBoot是如何使用SQL數(shù)據(jù)庫的?

    SpringBoot是如何使用SQL數(shù)據(jù)庫的?

    今天給大家?guī)淼氖顷P(guān)于Springboot的相關(guān)知識,文章圍繞著SpringBoot是如何使用SQL數(shù)據(jù)庫的展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • 詳細(xì)了解java監(jiān)聽器和過濾器

    詳細(xì)了解java監(jiān)聽器和過濾器

    下面小編就為大家?guī)硪黄趈ava servlet過濾器和監(jiān)聽器(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2021-07-07
  • Mybatis-Plus使用updateById()、update()將字段更新為null

    Mybatis-Plus使用updateById()、update()將字段更新為null

    本文主要介紹了Mybatis-Plus使用updateById()、update()將字段更新為null,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • 教你怎么用Java回溯算法解數(shù)獨(dú)

    教你怎么用Java回溯算法解數(shù)獨(dú)

    一直不太會數(shù)獨(dú)問題,這次下決定搞明白,所以整理了本篇文章,文中有非常詳細(xì)的代碼示例,對不會算法的小伙伴們很有幫助,需要的朋友可以參考下
    2021-06-06
  • Java中如何將json字符串轉(zhuǎn)換成map/list

    Java中如何將json字符串轉(zhuǎn)換成map/list

    這篇文章主要介紹了Java中如何將json字符串轉(zhuǎn)換成map/list,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • java Socket實(shí)現(xiàn)多人群聊與私聊功能

    java Socket實(shí)現(xiàn)多人群聊與私聊功能

    這篇文章主要為大家詳細(xì)介紹了java Socket實(shí)現(xiàn)多人群聊與私聊功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07

最新評論