代码:显示查询的日历
package myjavase;
import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;
?
import javax.swing.JOptionPane;
?
class CalendarBean { ?//定義一個日歷類
?String day[]; //天數(shù)數(shù)組
?int year = 2011, month = 9; //給定一個初始年月
?
?public void setYear(int year) {?
? this.year = year;
?}
?
?public int getYear() {
? return year;
?}
?
?public void setMonth(int month) {
? this.month = month;
?}
?
?public int getMonth() {
? return month;
?}
?
?public String[] getCalendar() { //獲得日歷
? String a[] = new String[42]; //定義一個以字符串?dāng)?shù)組
? Calendar 日歷 = Calendar.getInstance();?
? 日歷.set(year, month - 1, 1);
? int 星期幾 = 日歷.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 = 星期幾, n = 1; i < 星期幾 + day; i++) {
? ?a[i] = String.valueOf(n);
? ?n++;
? }
? return a;
?}
}
?
class CalFrame extends Frame implements ActionListener {
?Label labelDay[] = new Label[42]; //用來輸出日歷的天的數(shù)組
?Button titleName[] = new Button[7]; //周一到周天的按鍵
?String name[] = { "日", "一", "二", "三", "四", "五", "六" };
?TextField text1, text2; //定義輸入的年份和月份
?Button nextMonth, previousMonth, Enter; //下個月,上個月,確定
?Label lab1, lab2, lab3; //幾個文本類型的字符串
?int year = 2012, month = 9;
?CalendarBean calendar; //頂一個量
?Label showMessage = new Label("", Label.CENTER); // ? 定義一個用于顯示 當(dāng)前年月的label 表明,標(biāo)簽上應(yīng)為中心。
?
?public CalFrame() { ? ?//窗體類
? Panel pCenter = new Panel(); ?//鑲嵌
? pCenter.setLayout(new GridLayout(7, 7));
? for (int i = 0; i < 7; i++) { ? ? ?//給周日到周六的button添加顯示文本
? ?titleName[i] = new Button(name[i]);
? ?pCenter.add(titleName[i]);
? }
? for (int i = 0; i < 42; i++) { ? ? ?
? ?labelDay[i] = new Label("", Label.CENTER);
? ?pCenter.add(labelDay[i]);
? }
? calendar = new CalendarBean();
? calendar.setYear(year); ? ? //設(shè)定年
? calendar.setMonth(month);
? String day[] = calendar.getCalendar(); ?//設(shè)定天
? for (int i = 0; i < 42; i++) { ? ? //給日歷位置循環(huán)添加顯示日歷天
? ?labelDay[i].setText(day[i]);
? }
? lab1 = new Label("請輸入日期"); ?//調(diào)用一個方法 ? ?new一個對象?
? lab2 = new Label("年份");
? lab3 = new Label("月份");
? Enter = new Button("確定");
? text1 = new TextField(10);
? text2 = new TextField(5);
? nextMonth = new Button("下月");
? previousMonth = new Button("上月");
? Enter.addActionListener(this); ?//添加指定的動作偵聽器
? nextMonth.addActionListener(this);
? previousMonth.addActionListener(this);
? Panel pNorth = new Panel(), pSouth = new Panel();
? pNorth.add(lab1);
? pNorth.add(lab2);
? pNorth.add(text1);
? pNorth.add(lab3);
? pNorth.add(text2);
? pNorth.add(Enter);
? pNorth.add(previousMonth);
? pNorth.add(nextMonth);
? pSouth.add(showMessage);
? showMessage.setText("日歷:" + calendar.getYear() + "年" + calendar.getMonth() + "月");
? ScrollPane scrollPane = new ScrollPane(); ?//實現(xiàn)單個子組件的自動水平和/或垂直滾動的容器類
? scrollPane.add(pCenter);?
? add(scrollPane, BorderLayout.CENTER);
? add(pNorth, BorderLayout.NORTH);
? add(pSouth, BorderLayout.SOUTH);
?}
?
?public void actionPerformed(ActionEvent e) {
? if (e.getSource() == nextMonth) { //如果獲得的操作時下個月的
? ?month = month + 1;
? ?if (month > 12)
? ? month = 1;
? ?calendar.setMonth(month);
? ?String day[] = calendar.getCalendar();
? ?for (int i = 0; i < 42; i++) {
? ? labelDay[i].setText(day[i]);
? ?}
? } else if (e.getSource() == previousMonth) {
? ?month = month - 1;
? ?if (month < 1)
? ? month = 12;
? ?calendar.setMonth(month);
? ?String day[] = calendar.getCalendar();
? ?for (int i = 0; i < 42; i++) {
? ? labelDay[i].setText(day[i]);
? ?}
? } else {
? ?String yea = text1.getText();
? ?String mon = text2.getText();
? ?try {
? ? year = Integer.parseInt(yea); //把string轉(zhuǎn)成int類型?
? ? month = Integer.parseInt(mon);
? ? if (month > 12 || month < 1 || year < 1) { ?//錯誤輸入的處理
? ? ?JOptionPane.showMessageDialog(null, "請輸入正確月份或月份");?
? ? ?return;
? ? } else {
? ? ?calendar.setYear(year);
? ? ?calendar.setMonth(month);
? ? }
? ? String day[] = calendar.getCalendar();
? ? for (int i = 0; i < 42; i++) {
? ? ?labelDay[i].setText(day[i]);
? ? }
? ?} catch (NumberFormatException ee) {
? ? JOptionPane.showMessageDialog(null, "請輸入正確的年份及月份");
? ?}
? }
? showMessage.setText("日歷:" + calendar.getYear() + "年" + calendar.getMonth() + "月");
?}
?
}
?
public class dayly {
?public static void main(String args[]) {
? CalFrame frame = new CalFrame();
? frame.setTitle("日歷");
? frame.setBounds(300, 200, 500, 300);
? frame.setVisible(true); ?//顯示窗口
? frame.validate(); //使生效
? frame.addWindowListener(new java.awt.event.WindowAdapter() { ?//點(diǎn)擊叉號關(guān)閉frame程序
? ?public void windowClosing(java.awt.event.WindowEvent e) {
? ? System.exit(0);?
? ?}
? });
?}
}
轉(zhuǎn)載于:https://blog.51cto.com/vipfang/1709224
總結(jié)
以上是生活随笔為你收集整理的代码:显示查询的日历的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【参数】REMOTE_LOGIN_PAS
- 下一篇: tomcat实现session集群及to