Java實(shí)現(xiàn)簡單日歷小程序 Java圖形界面小日歷開發(fā)
今天給大家介紹一下如何用Java swing開發(fā)一款簡單的小日歷,下面我們來看代碼:
首先創(chuàng)建一個CalendarBean類,用于基本的日期計算:
package other1;
import java.util.Calendar;
public class CalendarBean
{
String day[];
int year=2005,month=0;
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];
Calendar date=Calendar.getInstance();
date.set(year,month-1,1);
int week=date.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=week,n=1;i<week+day;i++)
{
a[i]=String.valueOf(n) ;
n++;
}
return a;
}
}
然后再創(chuàng)建一個主界面類,用于界面的實(shí)現(xiàn):
package other1;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CalendarFrame extends JFrame implements ActionListener
{
JLabel labelDay[]=new JLabel[42];
JTextField text=new JTextField(10);
JButton titleName[]=new JButton[7];
JButton button = new JButton();
String name[]={"日","一","二","三", "四","五","六"};
JButton nextMonth,previousMonth;
int year=1996,month=1; //啟動程序顯示的日期信息
CalendarBean calendar;
JLabel showMessage=new JLabel("",JLabel.CENTER);
JLabel lbl1 = new JLabel("請輸入年份:");
JLabel lbl2=new JLabel(" ");
public CalendarFrame()
{
setBackground(new Color(0, 128, 128));
JPanel pCenter=new JPanel();
pCenter.setBackground(new Color(0, 139, 139));
//將pCenter的布局設(shè)置為7行7列的GridLayout 布局。
pCenter.setLayout(new GridLayout(7,7));
//pCenter添加組件titleName[i]
for(int i=0;i<7;i++)
{
titleName[i]=new JButton(name[i]);
pCenter.add(titleName[i]);
}
//pCenter添加組件labelDay[i]
for(int i=0;i<42;i++)
{
labelDay[i]=new JLabel("",JLabel.CENTER);
pCenter.add(labelDay[i]);
}
text.addActionListener(this);
calendar=new CalendarBean();
calendar.setYear(year);
calendar.setMonth(month);
String day[]=calendar.getCalendar();
for(int i=0;i<42;i++)
{
labelDay[i].setText(day[i]);
}
nextMonth=new JButton("下月");
previousMonth=new JButton("上月");
button=new JButton("確定");
//注冊監(jiān)聽器
nextMonth.addActionListener(this);
previousMonth.addActionListener(this);
button.addActionListener(this);
JPanel pNorth=new JPanel(),
pSouth=new JPanel();
pNorth.add(showMessage);
pNorth.add(lbl2);
pNorth.add(previousMonth);
pNorth.add(nextMonth);
pSouth.add(lbl1);
pSouth.add(text);
pSouth.add(button);
showMessage.setText("日歷:"+calendar.getYear()+"年"+ calendar.getMonth()+"月" );
ScrollPane scrollPane=new ScrollPane();
scrollPane.add(pCenter);
getContentPane().add(scrollPane,BorderLayout.CENTER);// 窗口添加scrollPane在中心區(qū)域
getContentPane().add(pNorth,BorderLayout.NORTH);// 窗口添加pNorth 在北面區(qū)域
getContentPane().add(pSouth,BorderLayout.SOUTH);// 窗口添加pSouth 在南區(qū)域。
}
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 if(e.getSource()==button)
{
month=month+1;
if(month>12)
month=1;
calendar.setYear(Integer.parseInt(text.getText()));
String day[]=calendar.getCalendar();
for(int i=0;i<42;i++)
{
labelDay[i].setText(day[i]);
}
}
showMessage.setText("日歷:"+calendar.getYear()+"年"+calendar.getMonth()+"月" );
}
}
最后用一個類調(diào)用即可:
package other1;
import javax.swing.JFrame;
import javax.swing.UIManager;
public class CalendarMainClass
{
public static void main(String args[])
{
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); //windows界面風(fēng)格
}catch (Exception e) {
e.printStackTrace();
}
CalendarFrame frame=new CalendarFrame();
frame.setBounds(100,100,360,300);
frame.setTitle("日歷小程序");
frame.setLocationRelativeTo(null);//窗體居中顯示
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
運(yùn)行結(jié)果如下:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Mybatis 中Mapper使用package方式配置報錯的解決方案
這篇文章主要介紹了Mybatis 中Mapper使用package方式配置報錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
Spring?Get請求與post請求的實(shí)現(xiàn)
在Spring中,GET請求和POST請求是兩種常見的HTTP請求方法,用于與服務(wù)器進(jìn)行交互,本文詳細(xì)的介紹一下Spring?Get請求與post請求的實(shí)現(xiàn),感興趣的可以了解一下2023-10-10
Java獲取一維數(shù)組的最小值實(shí)現(xiàn)方法
這篇文章主要介紹了Java獲取一維數(shù)組的最小值實(shí)現(xiàn)方法,需要的朋友可以參考下2014-02-02
Java應(yīng)用打包后運(yùn)行需要注意編碼問題
這篇文章主要介紹了 Java應(yīng)用打包后運(yùn)行需要注意編碼問題的相關(guān)資料,需要的朋友可以參考下2016-12-12
Android應(yīng)用開發(fā)的一般文件組織結(jié)構(gòu)講解
這篇文章主要介紹了Android應(yīng)用開發(fā)的一般文件組織結(jié)構(gòu)講解,同時附帶介紹了一個獲取Android的文件列表的方法,需要的朋友可以參考下2015-12-12
MybatisPlus #{param}和${param}的用法詳解
這篇文章主要介紹了MybatisPlus #{param}和${param}的用法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09

