当前位置: 首页>編程日記>正文

【Java小项目实训】编写一个窗体程序显示的日历 万年历

【Java小项目实训】编写一个窗体程序显示的日历 万年历

【实训要求】

 * 1.使用BorderLayout进行总体布局 
 * 2.在North位置放置包含两个按钮(上月和下月)的Panel
 * 3.在South位置放置一个Label用于显示当前年份和月份 
 * 4.在Center位置放置一个显示日历的Panel 
 * 5.显示日历的Panel设置7行7列的GridLayout布局,其中第1行放置7个按钮显示周“几”,其他6行放置42个Label用于显示日期
 * 6.启动程序时日历中默认显示当前月份的日历 
 * 7.点击“上月”和“下月”可翻看上个月和下个月的日历

 

【程序运行效果】

 

【代码】

package com.practise.Calendar;import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;import javax.swing.JLabel;/*** 实训要求: * 1.使用BorderLayout进行总体布局 * 2.在North位置放置包含两个按钮(上月和下月)的Panel* 3.在South位置放置一个Label用于显示当前年份和月份 * 4.在Center位置放置一个显示日历的Panel * 5.显示日历的Panel设置7行7列的GridLayout布局,其中第1行放置7个按钮显示周“几”,其他6行放置42个Label用于显示日期* 6.启动程序时日历中默认显示当前月份的日历 * 7.点击“上月”和“下月”可翻看上个月和下个月的日历* * @author DoubleShift**/
public class CalendarBean implements ActionListener {JLabel now; // 显示当前日期JLabel[] label; // 显示日历// String[] day;int year = 0;int month = 0;public void setYear(int year) {this.year = year;}public void setMonth(int month) {this.month = month;}@Overridepublic 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]; // 6*7Calendar 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;}}
package com.practise.Calendar;import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;/*** * @author DoubleShift**/
public class MyCalendar extends JFrame {private static final long serialVersionUID = 1L;JButton bx, by, b1, b2, b3, b4, b5, b6, b7;CalendarBean cbBean = new CalendarBean();JLabel[] label;JLabel now;public static void main(String[] args) {MyCalendar myCalendar = new MyCalendar();myCalendar.setSize(500, 400);myCalendar.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);myCalendar.setTitle("万年历");myCalendar.setVisible(true);}public MyCalendar() {int year, month;setLayout(new BorderLayout());// 北面設置上月和下月的按钮JPanel pNorth = new JPanel();cbBean = new CalendarBean();cbBean.setYear(2019);cbBean.setMonth(12);String[] a = cbBean.getCalendar();bx = new JButton("上月");bx.setActionCommand("lastmonth");bx.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {cbBean.actionPerformed(e);}});by = new JButton("下月");by.setActionCommand("nextmonth");by.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {cbBean.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]);}cbBean.label = this.label;for (int i = 0; i < a.length; i++) {label[i].setText("          " + a[i]);}pCenter.setLayout(grid);add(pCenter, BorderLayout.CENTER);// 面板南面設置当前日期JPanel southJPanel = new JPanel();now = new JLabel();now.setText("日历:" + cbBean.year + "年" + cbBean.month + "月");cbBean.now = this.now;southJPanel.add(now);add(southJPanel, BorderLayout.SOUTH);}
}

 


https://www.fengoutiyan.com/post/14813.html

相关文章:

  • 编写窗体应用程序
  • java设计一个窗体
  • c语言校内实训日志
  • 编写窗体单击事件
  • java窗体小程序
  • java编写日历
  • c窗体程序设计
  • java窗体代码
  • 鏡像模式如何設置在哪,圖片鏡像操作
  • 什么軟件可以把圖片鏡像翻轉,C#圖片處理 解決左右鏡像相反(旋轉圖片)
  • 手機照片鏡像翻轉,C#圖像鏡像
  • 視頻鏡像翻轉軟件,python圖片鏡像翻轉_python中鏡像實現方法
  • 什么軟件可以把圖片鏡像翻轉,利用PS實現圖片的鏡像處理
  • 照片鏡像翻轉app,java實現圖片鏡像翻轉
  • 什么軟件可以把圖片鏡像翻轉,python圖片鏡像翻轉_python圖像處理之鏡像實現方法
  • matlab下載,matlab如何鏡像處理圖片,matlab實現圖像鏡像
  • 圖片鏡像翻轉,MATLAB:鏡像圖片
  • 鏡像翻轉圖片的軟件,圖像處理:實現圖片鏡像(基于python)
  • canvas可畫,JavaScript - canvas - 鏡像圖片
  • 圖片鏡像翻轉,UGUI優化:使用鏡像圖片
  • Codeforces,CodeForces 1253C
  • MySQL下載安裝,Mysql ERROR: 1253 解決方法
  • 勝利大逃亡英雄逃亡方案,HDU - 1253 勝利大逃亡 BFS
  • 大一c語言期末考試試題及答案匯總,電大計算機C語言1253,1253《C語言程序設計》電大期末精彩試題及其問題詳解
  • lu求解線性方程組,P1253 [yLOI2018] 扶蘇的問題 (線段樹)
  • c語言程序設計基礎題庫,1253號C語言程序設計試題,2016年1月試卷號1253C語言程序設計A.pdf
  • 信奧賽一本通官網,【信奧賽一本通】1253:抓住那頭牛(詳細代碼)
  • c語言程序設計1253,1253c語言程序設計a(2010年1月)
  • 勝利大逃亡英雄逃亡方案,BFS——1253 勝利大逃亡
  • 直流電壓測量模塊,IM1253B交直流電能計量模塊(艾銳達光電)
  • c語言程序設計第三版課后答案,【渝粵題庫】國家開放大學2021春1253C語言程序設計答案
  • 18轉換為二進制,1253. 將數字轉換為16進制
  • light-emitting diode,LightOJ-1253 Misere Nim
  • masterroyale魔改版,1253 Dungeon Master
  • codeformer官網中文版,codeforces.1253 B
  • c語言程序設計考研真題及答案,2020C語言程序設計1253,1253計算機科學與技術專業C語言程序設計A科目2020年09月國家開 放大學(中央廣播電視大學)
  • c語言程序設計基礎題庫,1253本科2016c語言程序設計試題,1253電大《C語言程序設計A》試題和答案200901
  • 肇事逃逸車輛無法聯系到車主怎么辦,1253尋找肇事司機