java简单窗口日历程序,第一次写博文

       第一次写博文,现在还是刚刚开始接触java窗口,所以写的有些不够严谨,希望大家能多多执导。

       刚开始的时候想到可以调用calendar类来实现时间的规划,但是还不够,还得自己创建一个calendarbb类实现年里每个月的天数,然后就是研究了一个下午的calendar类的用法,关键就是calendar对象的声明要用getinstance来实例化,因为calendar是抽象类。然后就是调用calendar的set和get方法,用法还是很简单的,关键是参数要调整好,就好像set的话,之后的日期是变成下一天,并且不在参数范围内的将会自动往前递推日期。

       最后就是网格布局的问题,我只做了一年的日历,不过我在calendarbb类里面实现了年份的改变,所以有兴趣的可以再calendarFrame里面实现年份的改变。

附上代码:

package test11;


import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;


class calendarbb{
<span style="white-space:pre">	</span>String [] day;
<span style="white-space:pre">	</span>int year=2014,month=11;
<span style="white-space:pre">	</span>public void setyear(int year) {
<span style="white-space:pre">		</span>this.year=year;<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>public int getyear() {
<span style="white-space:pre">		</span>return year;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>public void setmonth(int month){
<span style="white-space:pre">		</span>this.month=month;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>public int getmonth() {
<span style="white-space:pre">		</span>return month;<span style="white-space:pre">		</span>
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>public String[] getCalendar(){
<span style="white-space:pre">		</span>String [] a=new String[42];
<span style="white-space:pre">		</span>Calendar r=Calendar.getInstance();
<span style="white-space:pre">		</span>
<span style="white-space:pre">		</span>r.set(year, month-1,-1);
<span style="white-space:pre">		</span>int weekday=r.get(Calendar.DAY_OF_WEEK);
<span style="white-space:pre">		</span>int day=0;
<span style="white-space:pre">		</span> if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
<span style="white-space:pre">			</span>   day = 31;
<span style="white-space:pre">			</span>  }
<span style="white-space:pre">			</span>  if (month == 4 || month == 6 || month == 9 || month == 11) {
<span style="white-space:pre">			</span>   day = 30;
<span style="white-space:pre">			</span>  }
<span style="white-space:pre">			</span>  if (month == 2) {
<span style="white-space:pre">			</span>   if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
<span style="white-space:pre">			</span>    day = 29;
<span style="white-space:pre">			</span>   } else {
<span style="white-space:pre">			</span>    day = 28;
<span style="white-space:pre">			</span>   }
<span style="white-space:pre">			</span>  }
<span style="white-space:pre">		</span>for(int i=weekday,n=1;i<weekday+day;i++)
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>a[i] = String.valueOf(n);
<span style="white-space:pre">			</span> n++;
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>return a;<span style="white-space:pre">		</span>
<span style="white-space:pre">	</span>}
}


class CalendarFrame extends JFrame implements ActionListener{
<span style="white-space:pre">	</span>JLabel labelday[]=new JLabel[42];
<span style="white-space:pre">	</span>JButton titleButton[]=new JButton[7];
<span style="white-space:pre">	</span>String title[]={"一","二","三","四","五","六","日"};
<span style="white-space:pre">	</span>JButton nextmonth,previousmonth;
<span style="white-space:pre">	</span>int year=2014,month=11;
<span style="white-space:pre">	</span>calendarbb calendar;
<span style="white-space:pre">	</span>JLabel showmessageJLabel=new JLabel("",JLabel.CENTER);
<span style="white-space:pre">	</span>JLabel year1=new JLabel("年份:");
<span style="white-space:pre">	</span>JLabel month1=new JLabel("月份");
<span style="white-space:pre">	</span>JButton ok=new JButton("确定");
<span style="white-space:pre">	</span>JTextField y1=new JTextField(10);
<span style="white-space:pre">	</span>JTextField m1=new JTextField(10);
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>public CalendarFrame() {
<span style="white-space:pre">		</span>JPanel panel=new JPanel();
<span style="white-space:pre">		</span>panel.setLayout(new GridLayout(7,7));
<span style="white-space:pre">		</span>for(int i=0;i<7;i++)
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>titleButton[i]=new JButton(title[i]);
<span style="white-space:pre">			</span>panel.add(titleButton[i]);
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>for(int i=0;i<42;i++)
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>labelday[i]=new JLabel("",JLabel.CENTER);
<span style="white-space:pre">			</span>panel.add(labelday[i]);
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>calendar=new calendarbb();
<span style="white-space:pre">		</span>calendar.setyear(year);
<span style="white-space:pre">		</span>calendar.setmonth(month);
<span style="white-space:pre">		</span>String []day=calendar.getCalendar();
<span style="white-space:pre">		</span>for(int i=0;i<42;i++)
<span style="white-space:pre">		</span>{ 
<span style="white-space:pre">			</span>labelday[i].setText(day[i]);
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>nextmonth=new JButton("下月");
<span style="white-space:pre">		</span>previousmonth=new JButton("上月");
<span style="white-space:pre">		</span>nextmonth.addActionListener(this);
<span style="white-space:pre">		</span>previousmonth.addActionListener(this);
<span style="white-space:pre">		</span>m1.addActionListener(this);
<span style="white-space:pre">		</span>y1.addActionListener(this);
<span style="white-space:pre">		</span>ok.addActionListener(this);
<span style="white-space:pre">		</span>
<span style="white-space:pre">		</span>JPanel norJPanel=new JPanel();
<span style="white-space:pre">		</span>JPanel souPanel=new JPanel();
<span style="white-space:pre">		</span>norJPanel.add(year1);
<span style="white-space:pre">		</span>norJPanel.add(y1);<span style="white-space:pre">		</span>
<span style="white-space:pre">		</span>norJPanel.add(month1);
<span style="white-space:pre">		</span>norJPanel.add(m1);
<span style="white-space:pre">		</span>norJPanel.add(ok);
<span style="white-space:pre">		</span>norJPanel.add(previousmonth);
<span style="white-space:pre">		</span>norJPanel.add(nextmonth);
<span style="white-space:pre">		</span>souPanel.add(showmessageJLabel);
<span style="white-space:pre">		</span>showmessageJLabel.setText("日历:"+calendar.getyear()+"年"+calendar.getmonth()+"月");
<span style="white-space:pre">		</span>add(panel,BorderLayout.CENTER);
<span style="white-space:pre">		</span>add(norJPanel,BorderLayout.NORTH);
<span style="white-space:pre">		</span>add(souPanel,BorderLayout.SOUTH);
<span style="white-space:pre">		</span>
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>public void actionPerformed(ActionEvent e) {
<span style="white-space:pre">	</span>if(e.getSource()==nextmonth)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>month=month+1;
<span style="white-space:pre">		</span>if(month>12)
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>month=1;
<span style="white-space:pre">			</span>year++;
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>calendar.setyear(year);
<span style="white-space:pre">		</span>calendar.setmonth(month);
<span style="white-space:pre">		</span>String day[]=calendar.getCalendar();
<span style="white-space:pre">		</span>for(int i=0;i<42;i++)
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>labelday[i].setText(day[i]);<span style="white-space:pre">	</span>
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>else if(e.getSource()==previousmonth)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>month=month-1;
<span style="white-space:pre">		</span>if(month<1)
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>month=12;
<span style="white-space:pre">			</span>year--;
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>calendar.setyear(year);<span style="white-space:pre">	</span>
<span style="white-space:pre">		</span>calendar.setmonth(month);
<span style="white-space:pre">		</span>String day[]=calendar.getCalendar();
<span style="white-space:pre">		</span>for(int i=0;i<42;i++)
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>labelday[i].setText(day[i]);
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>else if (e.getSource()==ok) {
<span style="white-space:pre">		</span>month=Integer.valueOf(m1.getText());
<span style="white-space:pre">		</span>if(month<0||month>12)
<span style="white-space:pre">			</span>JOptionPane.showMessageDialog(null, "请输入1-12的月份");
<span style="white-space:pre">		</span>year=Integer.valueOf(y1.getText());
<span style="white-space:pre">		</span>if(year<0)
<span style="white-space:pre">			</span>JOptionPane.showMessageDialog(null, "请输入非负年份");
<span style="white-space:pre">		</span>calendar.setyear(year);
<span style="white-space:pre">		</span>calendar.setmonth(month);
<span style="white-space:pre">		</span>String day[]=calendar.getCalendar();
<span style="white-space:pre">		</span>for(int i=0;i<42;i++)
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>labelday[i].setText(day[i]);
<span style="white-space:pre">		</span>}<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>showmessageJLabel.setText("日历:"+calendar.getyear()+"年"+calendar.getmonth()+"月");<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>}<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>


}


public class calendartest {
<span style="white-space:pre">	</span>public static void main(String[] args)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>CalendarFrame calendarFrame=new CalendarFrame();
<span style="white-space:pre">		</span>calendarFrame.setTitle("nickming的日历");
<span style="white-space:pre">		</span>calendarFrame.setBounds(300, 200, 600, 300);
<span style="white-space:pre">		</span>calendarFrame.setVisible(true);
<span style="white-space:pre">		</span>calendarFrame.validate();
<span style="white-space:pre">	</span>}


}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
### 回答1: 你好,我可以为你提供一个简单java程序来创建一个日历。以下是程序的代码: import java.util.Calendar; public class CalendarExample { public static void main(String[] args) { Calendar cal = Calendar.getInstance(); System.out.println("当前日期: " + cal.getTime()); // add 10 days to the calendar cal.add(Calendar.DATE, 10); System.out.println("十天后的日期: " + cal.getTime()); } } ### 回答2: 首先,让我们来编一个简单java日历程序。 ```java import java.time.LocalDate; public class CalendarProgram { public static void main(String[] args) { // 获取当前日期 LocalDate currentDate = LocalDate.now(); // 获取当前月份和年份 int currentMonth = currentDate.getMonthValue(); int currentYear = currentDate.getYear(); // 打印日历标题 System.out.println(" " + currentMonth + "月 " + currentYear); System.out.println("日 一 二 三 四 五 六"); // 获取当前月份的第一天,并确定它是星期几 LocalDate firstDayOfMonth = LocalDate.of(currentYear, currentMonth, 1); int dayOfWeek = firstDayOfMonth.getDayOfWeek().getValue(); // 打印日历 for (int i = 1; i < dayOfWeek; i++) { System.out.print(" "); } int daysInMonth = currentDate.lengthOfMonth(); for (int i = 1; i <= daysInMonth; i++) { System.out.printf("%2d ", i); if ((i + dayOfWeek - 1) % 7 == 0) { System.out.println(); } } } } ``` 这个程序使用了java.time包中的LocalDate类来获取当前日期,然后通过当前日期获取当前月份和年份。接下来,程序通过LocalDate类的静态方法of()来获取当前月份的第一天,并使用getDayOfWeek().getValue()方法获取它是星期几。根据这个星期几的值,程序在打印日历之前打印出一个空格。 程序接下来使用currentDate.lengthOfMonth()方法获取当前月份的天数,并使用for循环依次打印每一天的日期。如果当前日期是一个星期的最后一天,程序会换行打印下一行。 最后,程序会输出一个形如" 3月 2022"的标题,并在打印星期标题之后打印出日历。 ### 回答3: 日历是一种记录日期的工具,我们可以使用Java一个简单日历程序。下面是一个示例代码: ```java import java.util.Calendar; public class CalendarProgram { public static void main(String[] args) { Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH) + 1; //月份从0开始,需要加1 int day = calendar.get(Calendar.DAY_OF_MONTH); int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); // 打印日历的月份和年份 System.out.println(year + "年" + month + "月"); // 打印日历的表头 System.out.println("日 一 二 三 四 五 六"); // 计算该月份第一天是星期几 calendar.set(Calendar.DAY_OF_MONTH, 1); int firstDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); // 打印日历的空白格 for (int i = 1; i < firstDayOfWeek; i++) { System.out.print(" "); } // 打印日历的日期 for (int i = 1; i <= daysInMonth; i++) { System.out.printf("%2d ", i); // 每行打印7个日期 if ((firstDayOfWeek + i - 1) % 7 == 0) { System.out.println(); } } } } ``` 这个程序会打印当前月份的日历。它首先获取当前的年份、月份和日期,并计算出这个月份有多少天。然后,它打印出月份和年份,并在第一行打印出日历的表头。接下来,它计算出这个月份的第一天是星期几,并打印出日历的空白格。最后,它打印出这个月份的日期,并确保每行打印7个日期。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值