重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

java简易时钟日历代码 java时间代码

JAVA画时钟代码

import java.awt.*;

创新互联公司-专业网站定制、快速模板网站建设、高性价比源城网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式源城网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖源城地区。费用合理售后完善,十载实体公司更值得信赖。

import java.awt.event.*;

import javax.swing.*;

import sun.util.calendar.Gregorian;

import java.util.Calendar;

import java.util.GregorianCalendar;

public class ClockPointer extends JFrame{

int x, y, x0, y0, r, h, olds_x, olds_y, oldm_x, oldm_y, oldh_x, oldh_y, 

ss,mm, hh, old_m, old_h, ang;

final double RAD = Math.PI/180;

public ClockPointer(){

super("Java时钟");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Image image = getToolkit().getImage("clock.gif");

setIconImage(image);

setSize(400,400);

setBackground(Color.white);

//setLocation(300,150);

this.setLocationRelativeTo(null);

setResizable(true);

int delay = 1000;

//创建一个监听事件

setVisible(true);

ActionListener drawClock = new ActionListener(){

public void actionPerformed(ActionEvent evt){

repaint();

}

};

//创建一个时间计数器,每一秒触发一次

new Timer(delay, drawClock).start();

}

java.text.SimpleDateFormat fmTime = new java.text.SimpleDateFormat("HH:mm:ss");

//绘制图形

public void paint(Graphics g){

super.paint(g);

g.setFont(null);

Graphics2D g2D = (Graphics2D)g;

Insets insets = getInsets();

int L = insets.left/2, T = insets.top/2;

h = getSize().height;

g.setColor(Color.white);

//画圆

g2D.setStroke(new BasicStroke(2.0f));

g.setColor(Color.gray);

g.drawOval(L+40, T+40, h-80, h-80);

r = h/2 - 40;

x0 = 40 + r - 5 + L;

y0 = 40 + r - 5 - T;

ang = 60;

//绘制时钟上的12个字

for(int i = 1;i = 12;i ++){

x = (int)((r+10)*Math.cos(RAD*ang)+x0);

y = (int)((r+10)*Math.sin(RAD*ang)+y0);

g.setColor(Color.black);

g.drawString(""+i, x, h-y);

ang -=30;

}

//获得现在的时间

Calendar now = new GregorianCalendar();

int nowh = now.get(Calendar.HOUR_OF_DAY);

int nowm = now.get(Calendar.MINUTE);

int nows = now.get(Calendar.SECOND);

String st=fmTime.format(now.getTime());

//在窗体上显示时间

g.setColor(Color.pink);

g.fillRect(L, T, 50, 28);

g.setColor(Color.blue);

g.drawString(st,L+2,T+26);

//计算时间与度数的关系

ss = 90 - nows*6;

mm = 90 - nowm*6;

hh = 90 - nowh*30 - nowm/2;

x0 = r+40+L;

y0 = r+40+T;

g2D.setStroke(new BasicStroke(1.2f));

//擦除秒针

//if(olds_x  0){

//    g.setColor(getBackground());

//    // g.setColor(Color.gray);

//    g.drawLine(x0, y0, olds_x, h-olds_y); // (?)

//}

//绘制秒针

x = (int)(r*0.9*Math.cos(RAD*ss))+x0;

y = (int)(r*0.9*Math.sin(RAD*ss))+y0-2*T;

g.setColor(Color.yellow);

g.drawLine(x0, y0, x, h-y);

olds_x = x;

olds_y = y;

g2D.setStroke(new BasicStroke(2.2f));

//擦除分针

//if(old_m!=mm){

//    g.setColor(getBackground());

//    g.drawLine(x0,y0,oldm_x,h-oldm_y);

//}

//绘制分针

x = (int)(r*0.7*Math.cos(RAD*mm))+x0;

y = (int)(r*0.7*Math.sin(RAD*mm))+y0-2*T;

g.setColor(Color.green);

g.drawLine(x0,y0,x,h-y);

oldm_x = x;

oldm_y = y;

old_m = mm;

g2D.setStroke(new BasicStroke(3.2f));

//擦除时针

//if(old_h!=hh){

//    g.setColor(getBackground());

//    g.drawLine(x0,y0,oldh_x,h-oldh_y);

//}

//绘制时针

x = (int)(r*0.5*Math.cos(RAD*hh))+x0;

y = (int)(r*0.5*Math.sin(RAD*hh))+y0-2*T;

g.setColor(Color.red);

g.drawLine(x0,y0,x,h-y);

oldh_x = x;

oldh_y = y;

old_h = hh;

}

public static void main(String[] args){

new ClockPointer();

}

}

//整理一下

JAVA日历代码,怎么做?

import java.util.Date;

import java.util.Calendar;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.text.ParseException;

import java.awt.Color;

import java.awt.Font;

import java.awt.Point;

import java.awt.Dimension;

import java.awt.BorderLayout;

import java.awt.FlowLayout;

import java.awt.GridLayout;

import java.awt.Cursor;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JDialog;

import javax.swing.JPanel;

import javax.swing.JLabel;

import javax.swing.JSpinner;

import javax.swing.SpinnerNumberModel;

import javax.swing.SwingUtilities;

import javax.swing.SwingConstants;

import javax.swing.event.ChangeListener;

import javax.swing.event.ChangeEvent;

import javax.swing.border.LineBorder;

/**

* @company:NEUSOFT

* @Title:日期选择控件

* @Description:在原有基础上修改了以下内容:

* 1. 将容器由Frame改为了Dialog,以便在基于对话框的程序中也能够使用

* 2. 将最小日期由1980改为了1950,考虑到目前球员的出生日期可能早于1980年

* 3. 将初始显示格式设置为 yyyy年MM月dd日 格式,原有的小时去掉了,不适合于出生日期字段

*/

public class DateChooserJButton extends JButton {

private DateChooser dateChooser = null;

private String preLabel = "";

public DateChooserJButton() {

this(getNowDate());

}

public DateChooserJButton(SimpleDateFormat df, String dateString) {

this();

setText(df, dateString);

}

public DateChooserJButton(Date date) {

this("", date);

}

public DateChooserJButton(String preLabel, Date date) {

if (preLabel != null)

this.preLabel = preLabel;

setDate(date);

setBorder(null);

setCursor(new Cursor(Cursor.HAND_CURSOR));

addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if (dateChooser == null)

dateChooser = new DateChooser();

Point p = getLocationOnScreen();

p.y = p.y + 30;

dateChooser.showDateChooser(p);

}

});

}

private static Date getNowDate() {

return Calendar.getInstance().getTime();

}

private static SimpleDateFormat getDefaultDateFormat() {

return new SimpleDateFormat("yyyy年MM月dd日");

}

// 覆盖父类的方法

public void setText(String s) {

Date date;

try {

date = getDefaultDateFormat().parse(s);

} catch (ParseException e) {

date = getNowDate();

}

setDate(date);

}

public void setText(SimpleDateFormat df, String s) {

Date date;

try {

date = df.parse(s);

} catch (ParseException e) {

date = getNowDate();

}

setDate(date);

}

public void setDate(Date date) {

super.setText(preLabel + getDefaultDateFormat().format(date));

}

public Date getDate() {

String dateString = this.getText().substring(preLabel.length());

try {

return getDefaultDateFormat().parse(dateString);

} catch (ParseException e) {

return getNowDate();

}

}

public String getDateString()

{

Date birth =getDate();

DateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");

return formatDate.format(birth).toString();

//return this.getText().substring(preLabel.length());

}

// 覆盖父类的方法使之无效

//public void addActionListener(ActionListener listener) {

//}

private class DateChooser extends JPanel implements ActionListener,

ChangeListener {

int startYear = 1950; // 默认【最小】显示年份

int lastYear = 2050; // 默认【最大】显示年份

int width = 200; // 界面宽度

int height = 200; // 界面高度

Color backGroundColor = Color.gray; // 底色

// 月历表格配色----------------//

Color palletTableColor = Color.white; // 日历表底色

Color todayBackColor = Color.orange; // 今天背景色

Color weekFontColor = Color.blue; // 星期文字色

Color dateFontColor = Color.black; // 日期文字色

Color weekendFontColor = Color.red; // 周末文字色

// 控制条配色------------------//

Color controlLineColor = Color.pink; // 控制条底色

Color controlTextColor = Color.white; // 控制条标签文字色

Color rbFontColor = Color.white; // RoundBox文字色

Color rbBorderColor = Color.red; // RoundBox边框色

Color rbButtonColor = Color.pink; // RoundBox按钮色

Color rbBtFontColor = Color.red; // RoundBox按钮文字色

JDialog dialog;

JSpinner yearSpin;

JSpinner monthSpin;

JSpinner hourSpin;

JButton[][] daysButton = new JButton[6][7];

DateChooser() {

setLayout(new BorderLayout());

setBorder(new LineBorder(backGroundColor, 2));

setBackground(backGroundColor);

JPanel topYearAndMonth = createYearAndMonthPanal();

add(topYearAndMonth, BorderLayout.NORTH);

JPanel centerWeekAndDay = createWeekAndDayPanal();

add(centerWeekAndDay, BorderLayout.CENTER);

}

private JPanel createYearAndMonthPanal() {

Calendar c = getCalendar();

int currentYear = c.get(Calendar.YEAR);

int currentMonth = c.get(Calendar.MONTH) + 1;

int currentHour = c.get(Calendar.HOUR_OF_DAY);

JPanel result = new JPanel();

result.setLayout(new FlowLayout());

result.setBackground(controlLineColor);

yearSpin = new JSpinner(new SpinnerNumberModel(currentYear,

startYear, lastYear, 1));

yearSpin.setPreferredSize(new Dimension(48, 20));

yearSpin.setName("Year");

yearSpin.setEditor(new JSpinner.NumberEditor(yearSpin, "####"));

yearSpin.addChangeListener(this);

result.add(yearSpin);

JLabel yearLabel = new JLabel("年");

yearLabel.setForeground(controlTextColor);

result.add(yearLabel);

monthSpin = new JSpinner(new SpinnerNumberModel(currentMonth, 1,

12, 1));

monthSpin.setPreferredSize(new Dimension(35, 20));

monthSpin.setName("Month");

monthSpin.addChangeListener(this);

result.add(monthSpin);

JLabel monthLabel = new JLabel("月");

monthLabel.setForeground(controlTextColor);

result.add(monthLabel);

hourSpin = new JSpinner(new SpinnerNumberModel(currentHour, 0, 23,

1));

hourSpin.setPreferredSize(new Dimension(35, 20));

hourSpin.setName("Hour");

hourSpin.addChangeListener(this);

result.add(hourSpin);

JLabel hourLabel = new JLabel("时");

hourLabel.setForeground(controlTextColor);

result.add(hourLabel);

return result;

}

private JPanel createWeekAndDayPanal() {

String colname[] = { "日", "一", "二", "三", "四", "五", "六" };

JPanel result = new JPanel();

// 设置固定字体,以免调用环境改变影响界面美观

result.setFont(new Font("宋体", Font.PLAIN, 12));

result.setLayout(new GridLayout(7, 7));

result.setBackground(Color.white);

JLabel cell;

for (int i = 0; i 7; i++) {

cell = new JLabel(colname[i]);

cell.setHorizontalAlignment(JLabel.RIGHT);

if (i == 0 || i == 6)

cell.setForeground(weekendFontColor);

else

cell.setForeground(weekFontColor);

result.add(cell);

}

int actionCommandId = 0;

for (int i = 0; i 6; i++)

for (int j = 0; j 7; j++) {

JButton numberButton = new JButton();

numberButton.setBorder(null);

numberButton.setHorizontalAlignment(SwingConstants.RIGHT);

numberButton.setActionCommand(String

.valueOf(actionCommandId));

numberButton.addActionListener(this);

numberButton.setBackground(palletTableColor);

numberButton.setForeground(dateFontColor);

if (j == 0 || j == 6)

numberButton.setForeground(weekendFontColor);

else

numberButton.setForeground(dateFontColor);

daysButton[i][j] = numberButton;

result.add(numberButton);

actionCommandId++;

}

return result;

}

private JDialog createDialog(JDialog owner) {

JDialog result = new JDialog(owner, "日期时间选择", true);

result.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);

result.getContentPane().add(this, BorderLayout.CENTER);

result.pack();

result.setSize(width, height);

return result;

}

void showDateChooser(Point position) {

JDialog owner = (JDialog) SwingUtilities

.getWindowAncestor(DateChooserJButton.this);

if (dialog == null || dialog.getOwner() != owner)

dialog = createDialog(owner);

dialog.setLocation(getAppropriateLocation(owner, position));

flushWeekAndDay();

dialog.setVisible(true);

}

Point getAppropriateLocation(JDialog owner, Point position) {

Point result = new Point(position);

Point p = owner.getLocation();

int offsetX = (position.x + width) - (p.x + owner.getWidth());

int offsetY = (position.y + height) - (p.y + owner.getHeight());

if (offsetX 0) {

result.x -= offsetX;

}

if (offsetY 0) {

result.y -= offsetY;

}

return result;

}

private Calendar getCalendar() {

Calendar result = Calendar.getInstance();

result.setTime(getDate());

return result;

}

private int getSelectedYear() {

return ((Integer) yearSpin.getValue()).intValue();

}

private int getSelectedMonth() {

return ((Integer) monthSpin.getValue()).intValue();

}

private int getSelectedHour() {

return ((Integer) hourSpin.getValue()).intValue();

}

private void dayColorUpdate(boolean isOldDay) {

Calendar c = getCalendar();

int day = c.get(Calendar.DAY_OF_MONTH);

c.set(Calendar.DAY_OF_MONTH, 1);

int actionCommandId = day - 2 + c.get(Calendar.DAY_OF_WEEK);

int i = actionCommandId / 7;

int j = actionCommandId % 7;

if (isOldDay)

daysButton[i][j].setForeground(dateFontColor);

else

daysButton[i][j].setForeground(todayBackColor);

}

private void flushWeekAndDay() {

Calendar c = getCalendar();

c.set(Calendar.DAY_OF_MONTH, 1);

int maxDayNo = c.getActualMaximum(Calendar.DAY_OF_MONTH);

int dayNo = 2 - c.get(Calendar.DAY_OF_WEEK);

for (int i = 0; i 6; i++) {

for (int j = 0; j 7; j++) {

String s = "";

if (dayNo = 1 dayNo = maxDayNo)

s = String.valueOf(dayNo);

daysButton[i][j].setText(s);

dayNo++;

}

}

dayColorUpdate(false);

}

public void stateChanged(ChangeEvent e) {

JSpinner source = (JSpinner) e.getSource();

Calendar c = getCalendar();

if (source.getName().equals("Hour")) {

c.set(Calendar.HOUR_OF_DAY, getSelectedHour());

setDate(c.getTime());

return;

}

dayColorUpdate(true);

if (source.getName().equals("Year"))

c.set(Calendar.YEAR, getSelectedYear());

else

// (source.getName().equals("Month"))

c.set(Calendar.MONTH, getSelectedMonth() - 1);

setDate(c.getTime());

flushWeekAndDay();

}

public void actionPerformed(ActionEvent e) {

JButton source = (JButton) e.getSource();

if (source.getText().length() == 0)

return;

dayColorUpdate(true);

source.setForeground(todayBackColor);

int newDay = Integer.parseInt(source.getText());

Calendar c = getCalendar();

c.set(Calendar.DAY_OF_MONTH, newDay);

setDate(c.getTime());

}

}

}

这是一个专门的选日期的类 ,你看看完了调用就行了

用java编程实现 2016年12月份的日历怎么写?

用Java实现的日历程序如下(图形界面程序)

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Font;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.Calendar;

import javax.swing.BorderFactory;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

public class CCI extends JFrame implements ActionListener{

JButton jb1=new JButton("");

JButton jb2=new JButton("");

JButton jb3=new JButton("");

JButton jb4=new JButton("");

JPanel jp1=new JPanel();

JPanel jp2=new JPanel();

JPanel jp3=new JPanel();

JPanel jp4=new JPanel();

JLabel jl1=new JLabel();

JLabel jl2=new JLabel();

JLabel[]jl=new JLabel[49];

String []week={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};

Calendar c=Calendar.getInstance();

int year,month,day;

int nowyear,nowmonth,nowday;

CCI(){

super("简单日历");

nowyear=c.get(Calendar.YEAR);

nowmonth=c.get(Calendar.MONTH)+1;

nowday=c.get(Calendar.DAY_OF_MONTH);

year=nowyear;

month=nowmonth;

day=nowday;

String s=year+"年"+month+"月";

jl1.setForeground(Color.RED);

jl1.setFont(new Font(null,Font.BOLD,20));

jl1.setText(s);

jb1.addActionListener(this);

jb2.addActionListener(this);

jb3.addActionListener(this);

jb4.addActionListener(this);

jp1.add(jb1);jp1.add(jb2);jp1.add(jl1);jp1.add(jb3);jp1.add(jb4);

jp2.setLayout(null);

createMonthPanel();

jp2.add(jp3);

jl2.setFont(new Font(null,Font.BOLD,20));

jl2.setText("今天是"+nowyear+"年"+nowmonth+"月"+nowday+"日");

jp4.add(jl2);

add(jp1,BorderLayout.NORTH);

add(jp2,BorderLayout.CENTER);

add(jp4,BorderLayout.SOUTH);

setSize(500,500);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLocationRelativeTo(null);

setVisible(true);

}

@Override

public void actionPerformed(ActionEvent ae) {

if(ae.getSource()==jb1){

year=year-1;

String s=year+"年"+month+"月";

jl1.setText(s);

jp3.removeAll();

createMonthPanel();

jp3.validate();

}

if(ae.getSource()==jb2){

if(month==1){

year=year-1;

month=12;

}else{

month=month-1;

}

String s=year+"年"+month+"月";

jl1.setText(s);

jp3.removeAll();

createMonthPanel();

jp3.validate();

}

if(ae.getSource()==jb3){

if(month==12){

year=year+1;

month=1;

}else{

month=month+1;

}

String s=year+"年"+month+"月";

jl1.setText(s);

jp3.removeAll();

createMonthPanel();

jp3.validate();

}

if(ae.getSource()==jb4){

year=year+1;

String s=year+"年"+month+"月";

jl1.setText(s);

jp3.removeAll();

createMonthPanel();

jp3.validate();

}

}

public static void main(String[] args) {

new CCI();

}

public int getMonthDays(int year, int month) { 

switch (month) {

case 1: 

case 3: 

case 5: 

case 7:

case 8: 

case 10: 

case 12:

return 31; 

case 2: 

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

return 29; 

} else { 

return 28; 

default: 

return 30; 

public void createMonthPanel(){

c.set(year, month-1, getMonthDays(year,month));

int weekOfMonth=c.get(Calendar.WEEK_OF_MONTH);

if(weekOfMonth==6){

jp3.setLayout(new GridLayout(7,7));

jp3.setBounds(50, 20, 420, 350);

}else{

jp3.setLayout(new GridLayout(6,7));

jp3.setBounds(50, 20, 420, 300);

}

jp3.setBorder(BorderFactory.createEtchedBorder());

for(int i=0;i7;i++){

jl[i]=new JLabel(week[i],JLabel.CENTER);

jl[i].setFont(new Font(null,Font.BOLD,20));

jl[i].setBorder(BorderFactory.createEtchedBorder());

jp3.add(jl[i]);

}

c.set(year, month-1, 1);

int emptyFirst=c.get(Calendar.DAY_OF_WEEK)-1;

int daysOfMonth=getMonthDays(year,month);

for(int i=6+emptyFirst;i=7;i--){

int intyear=year;

int intmonth=month;

if(intmonth==1){

intyear=intyear-1;

intmonth=12;

}else{

intmonth=intmonth-1;

}

int intdays=getMonthDays(intyear,intmonth);

jl[i]=new JLabel((intdays+7-i)+"",JLabel.CENTER);

jl[i].setFont(new Font(null,Font.BOLD,20));

jl[i].setForeground(Color.GRAY);

jl[i].setBorder(BorderFactory.createEtchedBorder());

jp3.add(jl[i]);

}

for(int i=7+emptyFirst;idaysOfMonth+7+emptyFirst;i++){

jl[i]=new JLabel((i-7-emptyFirst+1)+"",JLabel.CENTER);

jl[i].setFont(new Font(null,Font.BOLD,20));

if((i+1)%7==0 || (i+1)%7==1){

jl[i].setForeground(Color.RED);

}else if((i-7-emptyFirst+1)==nowdaymonth==nowmonthyear==nowyear)

jl[i].setForeground(Color.BLUE);

else

jl[i].setForeground(Color.BLACK);

jl[i].setBorder(BorderFactory.createEtchedBorder());

jp3.add(jl[i]);

}

if(weekOfMonth==6)

for(int i=48;i=daysOfMonth+emptyFirst+7;i--){

jl[i]=new JLabel((49-i)+"",JLabel.CENTER);

jl[i].setFont(new Font(null,Font.BOLD,20));

jl[i].setForeground(Color.GRAY);

jl[i].setBorder(BorderFactory.createEtchedBorder());

jp3.add(jl[i]);

}

else

for(int i=41;i=daysOfMonth+emptyFirst+7;i--){

jl[i]=new JLabel((42-i)+"",JLabel.CENTER);

jl[i].setFont(new Font(null,Font.BOLD,20));

jl[i].setForeground(Color.GRAY);

jl[i].setBorder(BorderFactory.createEtchedBorder());

jp3.add(jl[i]);

}

}

}

运行结果


当前标题:java简易时钟日历代码 java时间代码
浏览路径:http://cqcxhl.com/article/hgcioj.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP