導航:首頁 > 編程語言 > java日歷設計

java日歷設計

發布時間:2023-07-20 23:28:01

㈠ 用java做一個日歷顯示數據功能,在頁面上顯示日期數據。

MainFrame.java是顯示日歷程序,Clock.java是日歷計算程序(可以不要)。

編譯後運行MainFrame這個類即可。

swing窗口顯示萬年歷,jdk1.4以上環境編譯運行。

packageorg.test;
importjava.awt.BorderLayout;
importjava.awt.Color;
importjava.awt.GridLayout;
importjava.awt.Toolkit;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.sql.Date;
importjava.util.Calendar;
importjavax.swing.JComboBox;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
{
=-306484324260972141L;
JPanelpanel=newJPanel(newBorderLayout());
JPanelpanel1=newJPanel();
JPanelpanel2=newJPanel(newGridLayout(7,7));
JPanelpanel3=newJPanel();
JLabel[]label=newJLabel[49];
JLabely_label=newJLabel("年份");
JLabelm_label=newJLabel("月份");
JComboBoxcom1=newJComboBox();
JComboBoxcom2=newJComboBox();
intre_year,re_month,x_size,y_size;
Stringyear_num;
Calendarnow=Calendar.getInstance();//實例化Calendar

MainFrame(){
super("萬年歷");
setSize(300,350);
x_size=(int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth());
y_size=(int)(Toolkit.getDefaultToolkit().getScreenSize().getHeight());
setLocation((x_size-300)/2,(y_size-350)/2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel1.add(y_label);
panel1.add(com1);
panel1.add(m_label);
panel1.add(com2);
for(inti=0;i<49;i++){
label[i]=newJLabel("",JLabel.CENTER);//將顯示的字元設置為居中
panel2.add(label[i]);
}
panel3.add(newClock(this));
panel.add(panel1,BorderLayout.NORTH);
panel.add(panel2,BorderLayout.CENTER);
panel.add(panel3,BorderLayout.SOUTH);
panel.setBackground(Color.white);
panel1.setBackground(Color.white);
panel2.setBackground(Color.white);
panel3.setBackground(Color.white);
Init();
com1.addActionListener(newClockAction());
com2.addActionListener(newClockAction());
setContentPane(panel);
setVisible(true);
setResizable(false);
}

{
publicvoidactionPerformed(ActionEventarg0){
intc_year,c_month,c_week;
c_year=Integer.parseInt(com1.getSelectedItem().toString());//得到當前所選年份
c_month=Integer.parseInt(com2.getSelectedItem().toString())-1;//得到當前月份,並減1,計算機中的月為0-11
c_week=use(c_year,c_month);//調用函數use,得到星期幾
Resetday(c_week,c_year,c_month);//調用函數Resetday
}
}

publicvoidInit(){
intyear,month_num,first_day_num;
Stringlog[]={"日","一","二","三","四","五","六"};
for(inti=0;i<7;i++){
label[i].setText(log[i]);
}

for(inti=0;i<49;i=i+7){
label[i].setForeground(Color.red);//將星期日的日期設置為紅色
}

for(inti=6;i<49;i=i+7){
label[i].setForeground(Color.green);//將星期六的日期設置為綠色
}

for(inti=1;i<10000;i++){
com1.addItem(""+i);
}

for(inti=1;i<13;i++){
com2.addItem(""+i);
}

month_num=(int)(now.get(Calendar.MONTH));//得到當前時間的月份
year=(int)(now.get(Calendar.YEAR));//得到當前時間的年份
com1.setSelectedIndex(year-1);//設置下拉列表顯示為當前年
com2.setSelectedIndex(month_num);//設置下拉列表顯示為當前月
first_day_num=use(year,month_num);
Resetday(first_day_num,year,month_num);
}

publicintuse(intreyear,intremonth){
intweek_num;
now.set(reyear,remonth,1);//設置時間為所要查詢的年月的第一天
week_num=(int)(now.get(Calendar.DAY_OF_WEEK));//得到第一天的星期
returnweek_num;
}

@SuppressWarnings("deprecation")
publicvoidResetday(intweek_log,intyear_log,intmonth_log){
intmonth_day_score;//存儲月份的天數
intcount;
month_day_score=0;
count=1;
Datedate=newDate(year_log,month_log+1,1);//now
Calendarcal=Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH,-1);//前個月
month_day_score=cal.getActualMaximum(Calendar.DAY_OF_MONTH);//最後一天
for(inti=7;i<49;i++){//初始化標簽
label[i].setText("");
}
week_log=week_log+6;//將星期數加6,使顯示正確
month_day_score=month_day_score+week_log;
for(inti=week_log;i<month_day_score;i++,count++){
label[i].setText(count+"");
}
}

publicstaticvoidmain(String[]args){
JFrame.(true);
newMainFrame();
}
}
packageorg.test;
importjava.awt.Color;
importjava.util.Calendar;
importjava.awt.Canvas;
importjava.awt.Font;
importjava.awt.Graphics;
importjava.text.SimpleDateFormat;
{
=3660124045489727166L;
MainFramemf;
Threadt;
Stringtime;

publicClock(MainFramemf){
this.mf=mf;
setSize(280,40);
setBackground(Color.white);
t=newThread(this);//實例化線程
t.start();
//調用線程
}

publicvoidrun(){
while(true){
try{
Thread.sleep(1000);//休眠1秒鍾
}catch(InterruptedExceptione){
System.out.println("異常");
}
this.repaint(100);
}
}

publicvoidpaint(Graphicsg){
Fontf=newFont("宋體",Font.BOLD,16);
SimpleDateFormatSDF=newSimpleDateFormat("yyyy'年'MM'月'dd'日'HH:mm:ss");//格式化時間顯示類型
Calendarnow=Calendar.getInstance();
time=SDF.format(now.getTime());//得到當前日期和時間
g.setFont(f);
g.setColor(Color.orange);
g.drawString(time,45,25);
}
}

㈡ 怎麼用java寫日歷

以下是兩個類,請樓主分別存成兩個java文件:
其中
MainFrame.java是顯示日歷程序,Clock.java是日歷計算程序。編譯後運行MainFrame這個類即可。

1.MainFrame.java
---
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;
import java.util.Calendar;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class MainFrame extends JFrame {
/** *//**
*
*/
private static final long serialVersionUID = 1L;
JPanel panel = new JPanel(new BorderLayout());
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel(new GridLayout(7, 7));
JPanel panel3 = new JPanel();
JLabel[] label = new JLabel[49];
JLabel y_label = new JLabel("年份");
JLabel m_label = new JLabel("月份");
JComboBox com1 = new JComboBox();
JComboBox com2 = new JComboBox();
int re_year, re_month;
int x_size, y_size;
String year_num;
Calendar now = Calendar.getInstance(); // 實例化Calendar

MainFrame() {
super("萬年歷");
setSize(300, 350);
x_size = (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth());
y_size = (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight());
setLocation((x_size - 300) / 2, (y_size - 350) / 2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel1.add(y_label);
panel1.add(com1);
panel1.add(m_label);
panel1.add(com2);
for (int i = 0; i < 49; i++) {
label[i] = new JLabel("", JLabel.CENTER);// 將顯示的字元設置為居中
panel2.add(label[i]);
}
panel3.add(new Clock(this));
panel.add(panel1, BorderLayout.NORTH);
panel.add(panel2, BorderLayout.CENTER);
panel.add(panel3, BorderLayout.SOUTH);
panel.setBackground(Color.white);
panel1.setBackground(Color.white);
panel2.setBackground(Color.white);
panel3.setBackground(Color.white);
Init();
com1.addActionListener(new ClockAction());
com2.addActionListener(new ClockAction());

setContentPane(panel);
setVisible(true);
setResizable(false);
}

class ClockAction implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
int c_year, c_month, c_week;
c_year = Integer.parseInt(com1.getSelectedItem().toString()); // 得到當前所選年份
c_month = Integer.parseInt(com2.getSelectedItem().toString()) - 1; // 得到當前月份,並減1,計算機中的月為0-11
c_week = use(c_year, c_month); // 調用函數use,得到星期幾
Resetday(c_week, c_year, c_month); // 調用函數Resetday
}
}

public void Init() {
int year, month_num, first_day_num;
String log[] = { "日", "一", "二", "三", "四", "五", "六" };
for (int i = 0; i < 7; i++) {
label[i].setText(log[i]);
}
for (int i = 0; i < 49; i = i + 7) {
label[i].setForeground(Color.red); // 將星期日的日期設置為紅色
}
for (int i = 6; i < 49; i = i + 7) {
label[i].setForeground(Color.green);// 將星期六的日期設置為綠色
}
for (int i = 1; i < 10000; i++) {
com1.addItem("" + i);
}
for (int i = 1; i < 13; i++) {
com2.addItem("" + i);
}
month_num = (int) (now.get(Calendar.MONTH)); // 得到當前時間的月份
year = (int) (now.get(Calendar.YEAR)); // 得到當前時間的年份
com1.setSelectedIndex(year - 1); // 設置下拉列表顯示為當前年
com2.setSelectedIndex(month_num); // 設置下拉列表顯示為當前月
first_day_num = use(year, month_num);
Resetday(first_day_num, year, month_num);
}

public int use(int reyear, int remonth) {
int week_num;
now.set(reyear, remonth, 1); // 設置時間為所要查詢的年月的第一天
week_num = (int) (now.get(Calendar.DAY_OF_WEEK));// 得到第一天的星期
return week_num;
}

@SuppressWarnings("deprecation")
public void Resetday(int week_log, int year_log, int month_log) {
int month_day_score; // 存儲月份的天數
int count;
month_day_score = 0;
count = 1;

Date date = new Date(year_log, month_log + 1, 1); // now
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH, -1); // 前個月
month_day_score = cal.getActualMaximum(Calendar.DAY_OF_MONTH);// 最後一天

for (int i = 7; i < 49; i++) { // 初始化標簽
label[i].setText("");
}
week_log = week_log + 6; // 將星期數加6,使顯示正確
month_day_score = month_day_score + week_log;
for (int i = week_log; i < month_day_score; i++, count++) {
label[i].setText(count + "");
}
}

public static void main(String[] args) {
JFrame.(true);
new MainFrame();
}
}
2.Clock.java
-----
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;
import java.util.Calendar;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

//顯示時間的類:Clock

/** *//**
* Clock.java
* Summary 數字時間顯示
* Created on
* @author
* remark
*/

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.text.SimpleDateFormat;
import java.util.Calendar;
class Clock extends Canvas implements Runnable{
/** *//**
*
*/
private static final long serialVersionUID = 3660124045489727166L;
MainFrame mf;
Thread t;
String time;
public Clock(MainFrame mf){
this.mf=mf;
setSize(280,40);
setBackground(Color.white);
t=new Thread(this); //實例化線程
t.start(); //調用線程
}
public void run(){
while(true){
try{
Thread.sleep(1000); //休眠1秒鍾
}catch(InterruptedException e){
System.out.println("異常");
}
this.repaint(100);
}
}
public void paint(Graphics g){
Font f=new Font("宋體",Font.BOLD,16);
SimpleDateFormat SDF=new SimpleDateFormat("yyyy'年'MM'月'dd'日'HH:mm:ss");//格式化時間顯示類型
Calendar now=Calendar.getInstance();
time=SDF.format(now.getTime()); //得到當前日期和時間
g.setFont(f);
g.setColor(Color.orange);
g.drawString(time,45,25);
}
}

閱讀全文

與java日歷設計相關的資料

熱點內容
單片機的控制板 瀏覽:218
襄陽軟體編程 瀏覽:841
sshpass命令 瀏覽:106
logo伺服器怎麼下載 瀏覽:508
如何ftp連接伺服器 瀏覽:674
creo自動編程 瀏覽:161
雲伺服器在電腦怎麼開 瀏覽:432
ipad相冊如何在文件夾中建文件夾 瀏覽:621
和家親這個app有什麼用 瀏覽:575
什麼app裡面有種樹打折 瀏覽:374
編程外掛入門教學 瀏覽:974
pdf黑白轉彩色 瀏覽:725
英國投資加密貨幣嗎 瀏覽:887
看完程序員那麼可愛後的感受 瀏覽:131
廣播在什麼APP能聽 瀏覽:678
阿克曼小車連接什麼app 瀏覽:773
all100編程器 瀏覽:182
加密的內存卡能用嗎 瀏覽:923
linux在線環境 瀏覽:404
java保留兩位小數四捨五入 瀏覽:106