求Java 日历的小程序的源代码

求Java 日历的小程序的源代码,第1张

也不知道你具体需求是什么,以前改过一个日历程序,一共四个java类,放在同一个包里。经测试可以运行。

//Start.java

import java.awt.*

import javax.swing.*

class Start{

public static void main(String [] args){

DateFrame frame=new DateFrame()

frame.setLocationRelativeTo(frame)

frame.setResizable(false)

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

frame.setVisible(true)

}

}

//DateInfo.java

import java.util.*

public class DateInfo{

private int mYear, mMonth

private int mDayOfMonth, mFristWeek

public DateInfo(int year, int month) throws DateException{

mYear = year

if (month <0 || month >12){

throw (new DateException())

}

mMonth = month

mDayOfMonth = getDayOfMonth(mYear, mMonth)

mFristWeek = getFristWeek(mYear, mMonth)

}

private int getDayOfMonth(int year, int month){

int[][] ary = {{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},

{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}}

return (ary[isLeapYear(year)][month])

}

private int isLeapYear(int year){

if (year % 4 == 0 &&year % 100 != 0 ||year % 400 == 0){

return (1)

}

else{

return (0)

}

}

private int getFristWeek(int year, int month){

java.util.Calendar cal = Calendar.getInstance()

cal.set(year, month - 1, 1)

return (cal.get(Calendar.DAY_OF_WEEK) - 1)

}

public String toString(){

String str

str = "\t\t" + mYear + "年" + mMonth + "月\n"

str += "日\t一\t二\t三\t四\t五\t六\n"

int i

for (i = 1i <= mFristWeeki++){

str += " \t"

}

for (int j = 1j <= mDayOfMonthj++, i++){

str +=j+"\t"

if (i % 7 == 0){

str += "\n"

}

}

return (str)

}

}

//DateFrame.java

import java.awt.*

import java.awt.event.*

import javax.swing.*

import java.util.Calendar

class DateFrame extends JFrame implements Runnable{

Calendar date=Calendar.getInstance()

String[] str={"1","2","3","4","5","6","7","8","9","10","11","12"}

JLabel lblYear=new JLabel("年 ")

JLabel lblMonth=new JLabel("月 ")

JLabel lblDate=new JLabel("现在的时间是:")

JLabel lblShowDate=new JLabel()

// javax.swing.JTextField trxt =new JTextField(10)

// trxt.setHorizontalAlignment(JTextField.RIGHT) //设置文本从右边输入

JComboBox cboMonth=new JComboBox(str)

JComboBox cboYear=new JComboBox()

JTextArea txaShow=new JTextArea()

JPanel pnlNorth=new JPanel()

JPanel pnlSOUTH=new JPanel()

JButton btnShow=new JButton("显示")

JButton btnClose=new JButton("关闭")

JScrollPane jsp=new JScrollPane(txaShow)

Container c=this.getContentPane()

public DateFrame(){

Thread thread=new Thread(this)

thread.start()

this.setTitle("玩玩日历拉!!!")

this.setSize(300,260)

for (int i = 1990i<=2025i++) {

cboYear.addItem(""+i)

}

cboYear.setSelectedItem(""+(date.get(Calendar.YEAR)))

cboMonth.setSelectedItem(""+(date.get(Calendar.MONTH)+1))

pnlNorth.add(cboYear)

txaShow.setTabSize(4) //设置tab键的距离

txaShow.setForeground(Color.GREEN)

pnlNorth.add(lblYear)

pnlNorth.add(cboMonth)

pnlNorth.add(lblMonth)

pnlNorth.add(lblDate)

pnlNorth.add(lblShowDate)

c.add(pnlNorth,BorderLayout.NORTH)

c.add(jsp)

pnlSOUTH.add(btnShow)

pnlSOUTH.add(btnClose)

c.add(pnlSOUTH,BorderLayout.SOUTH)

btnShow.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

int year=Integer.parseInt((String)cboYear.getSelectedItem())

int month=Integer.parseInt((String)cboMonth.getSelectedItem())

try {

DateInfo date=new DateInfo(year,month)

txaShow.setText(""+date)

}

catch (DateException ex) {

ex.printStackTrace()

}

}

})

btnClose.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

System.exit(0)

}

})

}

public void run(){

try {

while(true){

Thread.sleep(1000)

int hour=date.get(Calendar.HOUR)

int minute=date.get(Calendar.MINUTE)

int second=date.get(Calendar.SECOND)

String str=hour+":"+minute+":"+second

lblShowDate.setText(str)

//this.repaint()

}

}

catch (Exception ex) {

ex.printStackTrace()

}

}

}

//DateException.java

public class DateException extends Exception{

public DateException(){

super("日期数据不合法.")

}

}

System.out.println(cal.get(Calendar.MONDAY)+1+"月"+(cal.get(Calendar.DAY_OF_MONTH)+1)+"日") //输出日期


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12082749.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存