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.点击“上月”和“下月”可翻看上个月和下个月的日历。
8.程序运行结果如下图:
代码:
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); } }
运行结果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)