怎么设置使时钟和日历都在一个框架中

转载 2011年01月05日 08:42:00
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
import java.util.Calendar;
import java.util.GregorianCalendar;
 
class TestClock extends JFrame implements ActionListener{
 
       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 TestClock(){
           super("Java时钟");
           setDefaultCloseOperation(3);
           setSize(500,200);
           setBackground(Color.BLACK);
           setLocation(300,150);
           setResizable(false);
           setVisible(true);
           int delay = 1000;
           //创建一个监听事件
           ActionListener drawTestClock = new ActionListener(){
               public void actionPerformed(ActionEvent evt){
                   repaint();
               }
           };
           //创建一个时间计数器
           new Timer(delay,drawTestClock).start();
       }
       //实现ActionListener接口必须实现的方法
       public void actionPerformed(ActionEvent e){
       }
  
       //绘制图形
       public void paint(Graphics g){
           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(4.0f));
           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.drawString(""+i,x,h-y);
               ang-=30;
           }
           //获得现在时间
           JButton a=new JButton("设置时间");
           Calendar now= new GregorianCalendar();
           int nowh= now.get(Calendar.HOUR_OF_DAY);
           int nowm= now.get(Calendar.MINUTE);
           int nows= now.get(Calendar.SECOND);
           int nowmi= now.get(Calendar.MILLISECOND);
           String st;
           if(nowh<10) st="0"+nowh;else st=""+nowh;
           if(nowm<10) st+=":0"+nowm;else st+=":"+nowm;
           if(nows<10) st+=":0"+nows;else st+=":"+nows;
          
           String str;
           if(nowh<10) str="0"+nowh;else str=""+nowh;
           if(nowm<10) str+=":0"+nowm;else str+=":"+nowm;
           if(nows<10) str=":0"+nows;else str+=":"+nows;
           if(nowmi<10) str+=":0"+str;else str+=":"+nowmi;
           //在窗体上显示时间
           g.setColor(Color.pink);
           g.fillRect(300,50,100,30);
           g.setColor(Color.blue);
           g.drawString(st,330,70);
          
          
           g.setColor(Color.pink);
           g.fillRect(300,100,120,30);
           g.setColor(Color.blue);
           g.drawString(str,330,120);
          
          
           //计算时间与度数的关系
           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.drawLine(x0,y0,olds_x,h-olds_y);
           }
           else{
               old_m = mm;
               old_h = hh;
           }
           //绘制秒针
           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.4f));
           //擦除时针
           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 TestClock();
         }     
       } 
 
 
Calender.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
 
import java.util.*;
import java.awt.BorderLayout;
 
public class Calender extends JPanel implements ActionListener {
    public static final String HOUR_OF_DAY = null;
    //定义
    JComboBox Month = new JComboBox(); 
    JComboBox Year = new JComboBox(); 
    JLabel Year_l = new JLabel("年");
    JLabel Month_l = new JLabel("月");    
    Date now_date = new Date();
    JLabel[] Label_day = new JLabel[49];  
 
    int now_year = now_date.getYear() + 1900;
    int now_month = now_date.getMonth();
    boolean bool = false;
    String year_int = null;
    int month_int;
    JPanel pane_ym = new JPanel();
    JPanel pane_day = new JPanel();
   
    public Calender() { 
       super();
       //设定年月
       for (int i = now_year - 10; i <= now_year + 20; i++) {
           Year.addItem(i + "");
       }
 
       for (int i = 1; i < 13; i++) {
           Month.addItem(i + "");
       }
 
       Year.setSelectedIndex(10);
       pane_ym.add(new JLabel("        "));
       pane_ym.add(Year);
       pane_ym.add(Year_l);
       Month.setSelectedIndex(now_month);    
       pane_ym.add(Month);
       pane_ym.add(Month_l);
       pane_ym.add(new JLabel("        "));
      
       Month.addActionListener(this);
       Year.addActionListener(this); 
      
      
       //初始化日期并绘制
       pane_day.setLayout(new GridLayout(7, 7, 10, 10));
       for (int i = 0; i < 49; i++) {
           Label_day[i] = new JLabel(" ");
           pane_day.add(Label_day[i]);
       }
 
       this.setDay();
 
       this.setLayout(new BorderLayout());
//     setContentPane(pane_day);
//     setContentPane(pane_ym);
//     setContentPane(pane_parent);
        this.add(pane_day, BorderLayout.CENTER);
       this.add(pane_ym, BorderLayout.NORTH);
       this.setSize(100,200);
       this.setBorder(new TitledBorder("日历"));
 
       setSize(300,300);
      
 
    }
 
    void setDay() {
       if (bool) {
           year_int = now_year + "";
           month_int = now_month;
       }
       else {
           year_int = Year.getSelectedItem().toString();
           month_int = Month.getSelectedIndex();
       }
      
       int year_sel = Integer.parseInt(year_int) - 1900; //获得年份值
       Date dt = new Date(year_sel, month_int, 1); //构造一个日期
       GregorianCalendar cal = new GregorianCalendar(); //创建一个Calendar实例
       cal.setTime(dt);
 
       String week[] = { "日", "一","二", "三", "四", "五", "六" };
      
       int day = 0;
       int day_week = 0;
 
       for (int i = 0; i < 7; i++) {
           Label_day[i].setText(week[i]);
       }
 
 
       //月份
       if (month_int == 0||month_int == 2 ||month_int == 4 ||
              month_int == 6 ||
              month_int == 9 ||month_int == 11) {
           day = 31;
       }
       else if (month_int == 3 ||month_int == 5 || month_int == 7||
              month_int == 8 ||month_int == 10|| month_int == 1) {
           day = 30;
       } else {
           if (cal.isLeapYear(year_sel)) {
              day = 29;
           }
           else {
              day = 28;
           }
       }
 
       day_week = 7 + dt.getDay();
       int count = 1;
 
       for (int i = day_week; i < day_week + day; count++, i++) {
           if (i % 7 == 0 ||i == 13||i == 20||i == 27||
                  i == 48 ||i == 34 ||i == 41) {
              if (i == day_week + now_date.getDate() - 1) {
                  Label_day[i].setForeground(Color.blue);
                  Label_day[i].setText(count + "");
              } else {
                  Label_day[i].setForeground(Color.red);
                  Label_day[i].setText(count + "");
              }
           } else {
              if (i == day_week + now_date.getDate() - 1) {
                  Label_day[i].setForeground(Color.blue);
                                                         
                  Label_day[i].setText(count + "");
              } else {
                  Label_day[i].setForeground(Color.black);
                  Label_day[i].setText(count + "");
              }
           }
       }
       if (day_week == 0) {
           for (int i = day; i < 49; i++) {
              Label_day[i].setText(" ");
           }
       } else {
          
           for (int i = 7; i < day_week; i++) {
              Label_day[i].setText(" ");
           }
 
           for (int i = day_week + day; i < 49; i++) {
              Label_day[i].setText(" ");
           }
       }
    }
    public void actionPerformed(ActionEvent e) {
       if (e.getSource() == Year || e.getSource() == Month) {
           bool = false;
           this.setDay();
       }
    }
 
}
---------------------------------------------------
Test001.java
import java.awt.BorderLayout;
import java.awt.GridLayout;
 
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
 
public class Test001 extends JFrame{
   
    public Test001(){
       Clock clock =new Clock();
       Calender cal = new Calender();
       JPanel jp2 = new JPanel();
      
       setLocationRelativeTo(null);
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       setSize(560,300);
       setVisible(true);
       this.setContentPane(clock);
      
      
       this.getContentPane().add(cal,BorderLayout.WEST);
       setResizable(false);
    }
   
    public static void main(String[] args){
      
    try{
       Test001 frame = new Test001();
       frame.setTitle("测试");
 
    }
    catch (Exception e){
       System.out.print("run error!");
      
      
    }
 }
}

 

相关文章推荐

Java中的日历类Calendar的常用方法(所有方法都在客户端测试过)

欢迎使用Markdown编辑器写博客本Markdown编辑器使用StackEdit修改而来,用它写博客,将会带来全新的体验哦: Markdown和扩展Markdown简洁的语法 代码块高亮 图片链接和...

android 仿日历翻页特效、仿htc时钟翻页特效、数字翻页切换

废话不多说,效果图: 自定义控件找自网络,使用相对简单,具体还没有来得及深入研究,只是先用笨方法大概实现了想要的效果,后续有空会仔细研究再更新文章, 本demo切换方法是用的笨...

C51数字电子日历/时钟设计

数字电子日历/时钟设计      设计一个基于MCS51的电子日历和时钟。 基本要求 (1) 可通过按键在日历和时间之间切换显示; (2) 可由按键调整日期和时间 (3) 可整点报时(“...

JS特效--跟随鼠标移动的日历时钟

跟随鼠标移动的日历时钟

基于单片机的带有日历功能的数字时钟

基于单片机的带有日历功能的数字时钟 功能: 1.年、月、日,以及24小时制时间显示,均可调 2.自动判断是否闰年以及闰月,理论上至2100年...

stm32f103 rtc 获取 日历 时钟

STM32的RTC实现日历功能程序发布时间:2011-09-30 14:53:05   STM32的RTC只有一个32位的计数器用来计时,没有寄存器来存年月日时分秒等。通过设置可以让这个计数器...

VS2012 MFC 单文档 模拟时钟日历程序

第一次写博客,就把这第一次献给亲爱的MFC吧~作为一个MFC初学者,我懂得各位同是初学者的同学朋友们的不易,网上关于从MFC 0基础开始讲解MFC的书籍并不多,在这里就对我练习的这个小程序尽可能地做详...

分别实现数字时钟和日历的js代码

用js 分别实现数字时钟和日历 但是日历的实现有问题 时间更新时有跳动
  • Q_004
  • Q_004
  • 2017年08月18日 16:39
  • 116

【日志】Java 流行框架(Spring/Struts2/Hibernate/iBatis)都在使用什么日志组件

做 Java Web 应用我想多会采用这两种框架组合,SSH 和 SSI,其实我是不太喜欢用这两种缩写方式: SSH = Spring + Struts + Hibernate,SSH 由久已久所代表...

[置顶] Android开源库V - Layout:淘宝、天猫都在用的UI框架,赶紧用起来吧!

转自:http://blog.csdn.net/carson_ho/article/details/71077193 前言 V- Layout 是阿里出品的基础 UI 框架,用...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:怎么设置使时钟和日历都在一个框架中
举报原因:
原因补充:

(最多只允许输入30个字)