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

转载 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();
         }     
       } 
 
 
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();
       }
    }
 
}
---------------------------------------------------
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!");
      
      
    }
 }
}

 

在你的博客中加入日历,时钟和天气预报

1.加入日历在公告中加入如下代码:http://www.xise.cn/qita/rili/cal7.htm" scrolling="no" width="164" height="174">http...
  • wxfy1977
  • wxfy1977
  • 2006年06月09日 13:55
  • 2417

JavaFX制作一个简单的时钟

工具:Idea和JavaFX Scene Builder,与Eclipse+JavaFX没什么区别一、创建JavaFX项目1. 选择javafx项目 要求:JDK8及其以上 2. javafx项目清...
  • qiyuanguren
  • qiyuanguren
  • 2017年10月03日 18:25
  • 912

简单的个人博客日历代码,时钟演示

Html日历插
  • cometwo
  • cometwo
  • 2016年03月15日 21:53
  • 1948

iframe中点击子框架里面的超链接在新网页中打开想要打开的页面

在子框网页的标签中加 这样你网页内所有链接都在新窗口打开     若是要在同一页面打开则是在子框网页的标签中加...
  • chuan9966
  • chuan9966
  • 2014年03月18日 17:14
  • 1094

简易js网页实时时钟日历

function timeF(i){ if(i
  • maskice
  • maskice
  • 2016年02月02日 14:20
  • 713

使用JavaFX设置一个简单日历

1.先看效果: 2.代码: 1)ClockEdge.java类 这个类(Pane)主要是用来设置时钟边缘(为了美观对直了半天,其实想想也没必要~。~) package com.javaBasic....
  • Smart_snail
  • Smart_snail
  • 2016年10月17日 23:00
  • 770

Java SSH 项目总结——设定工作日日历

前言    今天文章的主题就是日历,先介绍下背景,这个日历是为一个在线预约系统服务的,在线预约就是选择工作日,然后进行具体业务的预约,而工作日的是设定就是通过我们这个日历来完成的.     正题 ...
  • zwk626542417
  • zwk626542417
  • 2015年01月24日 15:56
  • 4570

前端模型--纯js实现简易日历&&电子时钟

本文主要介绍纯JS实现简易日历,以及js调用本地时间 模型图: 1.html 简易年历 ...
  • qq_38209578
  • qq_38209578
  • 2017年12月15日 15:25
  • 152

使用iPhone来提醒自己-日历和提醒事项

在iPhone中,一些系统自带的应用的使用体验也是不错的。日历和提醒事项就是我今天要推荐的两个系统自带应用。...
  • u011803341
  • u011803341
  • 2016年12月26日 14:25
  • 2467

Android Launcher 动态icon【时钟】【日历】

首先在IconCache.java 中的修改 private static class CacheEntry { public Bitmap icon; publ...
  • PinSengJiXuHeZhou
  • PinSengJiXuHeZhou
  • 2015年05月21日 13:20
  • 2069
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:怎么设置使时钟和日历都在一个框架中
举报原因:
原因补充:

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