怎么设置使时钟和日历都在一个框架中
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!");
      
      
    }
 }
}

 

阅读更多
想对作者说点什么? 我来说一句

.net中js日历文件

2009年01月04日 187KB 下载

汇编语言写的时钟小程序

2009年02月15日 9KB 下载

没有更多推荐了,返回首页

不良信息举报

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

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭