| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 553 人关注过本帖
标题:java新手..求救!求救!...请各位高手帮帮忙!..
只看楼主 加入收藏
squallff
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2008-6-25
收藏
 问题点数:0 回复次数:1 
java新手..求救!求救!...请各位高手帮帮忙!..
如何将以下小时钟从JFrame变成JPanel类:
import java.awt.*;
import javax.swing.*;
import java.util.*;

public class Clock extends JFrame {
    public Clock() {
        ClockPaint cp = new ClockPaint(20, 20, 70);
        
        this.add(cp);
        this.setSize(200, 200);
        this.setResizable(false);
        this.setLocation(260, 120);
        this.setTitle("小时钟");
        this.setVisible(true);
    }
    public static void main(String[] s) {
        new Clock();
    }
}
class ClockPaint extends JPanel implements Runnable {
    int x, y, r;
    int h, m, s;//小时,分钟,秒
    double rad = Math.PI / 180;
    public ClockPaint(int x, int y, int r) {
        this.x = x;
        this.y = y;
        this.r = r;
        Calendar now = new GregorianCalendar();
        s = now.get(Calendar.SECOND) * 6;//获得秒转换成度数
        m = now.get(Calendar.MINUTE) * 6;//获得分钟
        h = (now.get(Calendar.HOUR_OF_DAY) - 12) * 30 + now.get(Calendar.MINUTE) / 12 * 6;//获得小时
    
        Thread t = new Thread(this);
        t.start();
    }
    public void paint(Graphics g) {
        //清屏
        super.paint(g);
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, r * 3, r * 3);
        //画圆
        g.setColor(Color.WHITE);
        g.drawOval(x, y, r * 2, r * 2);
        //秒针
        g.setColor(Color.RED);
        int x1 = (int)((r - 10) * Math.sin(rad * s));
        int y1 = (int)((r - 10) * Math.cos(rad * s));
        g.drawLine(x + r, y + r, x + r + x1, y + r - y1);
        //分针
        g.setColor(Color.BLUE);
        x1 = (int)((r - r / 2.5) * Math.sin(rad * m));
        y1 = (int)((r - r / 2.5) * Math.cos(rad * m));
        g.drawLine(x + r, y + r, x + r + x1, y + r - y1);
        //时针
        g.setColor(Color.CYAN);
        x1 = (int)((r - r / 1.5) * Math.sin(rad * h));
        y1 = (int)((r - r / 1.5) * Math.cos(rad * h));
        g.drawLine(x + r, y + r, x + r + x1, y + r - y1);
        //数字
        g.setColor(Color.YELLOW);
        int d = 29;
        for (int i = 1; i <= 12; i++) {
            x1 = (int)((r - 10) * Math.sin(rad * d));
            y1 = (int)((r - 10) * Math.cos(rad * d));
            g.drawString(i + "", x + r + x1 - 4, x + r - y1 + 5);
            d+=30;
        }
        //小点
        d = 0;
        for (int i = 0; i < 60; i++) {
            x1 = (int)((r - 2) * Math.sin(rad * d));
            y1 = (int)((r - 2) * Math.cos(rad * d));
            g.drawString(".", x + r + x1 - 1, x + r - y1 + 1);
            d+=6;
        }
        //显示时间
        Calendar now1 = new GregorianCalendar();
        g.drawString(now1.get(Calendar.HOUR_OF_DAY) + ":" + now1.get(Calendar.MINUTE) + ":" + now1.get(Calendar.SECOND), 0, 10);
    
    }
    public void run() {
        while (true) {
            try {
                Thread.sleep(1000);
            }
            catch (Exception ex) {
            }
            s+=6;
            if (s >= 360) {
                s = 0;
                m+=6;
                if (m == 72 || m == 144 || m == 216 || m == 288) {
                    h+=6;
                }
                if (m >= 360) {
                    m = 0;
                    h+=6;
                }
                if (h >=360) {
                       h = 0;
                }
            }
            this.repaint();
        }
    }
}
然后插入一个拥有日历,记事本..的窗口里面.
日历记事本主程序如下:
import java.util.Calendar;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import *;
import java.util.Hashtable;
public class CalendarPad extends JFrame implements MouseListener
{
   int year,month,day;
   Hashtable hashtable;            
   File file;                       
   JTextField showDay[];            
   JLabel title[];                  
   Calendar 日历;
   int 星期几;
   NotePad notepad=null;            
   Month 负责改变月;
   Year  负责改变年;
   String 星期[]={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
   JPanel northPanel,southPanel;   
   public  CalendarPad(int year,int month,int day)
   {
     northPanel=new JPanel();    //左边日历
     JPanel northCenter=new JPanel();
     JPanel northNorth=new JPanel();
     northCenter.setLayout(new GridLayout(7,7));   
                                                  
     southPanel=new JPanel();
     this.year=year;
     this.month=month;
     this.day=day;
     负责改变年=new Year(this);
     负责改变年.setYear(year);
     负责改变月=new Month(this);
     负责改变月.setMonth(month);
  
     title=new JLabel[7];                        
     showDay=new JTextField[42];                  
     for(int j=0;j<7;j++)                        
       {
         title[j]=new JLabel();
         title[j].setText(星期[j]);
         title[j].setBorder(BorderFactory.createRaisedBevelBorder());
         northCenter.add(title[j]);
       }
     title[0].setForeground(Color.red);
     title[6].setForeground(Color.blue);

     for(int i=0;i<42;i++)                        
       {
         showDay[i]=new JTextField();
         showDay[i].addMouseListener(this);
         showDay[i].setEditable(false);
         northCenter.add(showDay[i]);
       }
         
     日历=Calendar.getInstance();
     Box box=Box.createHorizontalBox();         
     box.add(负责改变年);
     box.add(负责改变月);
     northNorth.add(box);
     northPanel.setLayout(new BorderLayout());
     northPanel.add(northNorth,BorderLayout.NORTH);
     northPanel.add(northCenter,BorderLayout.CENTER);
     northPanel.add(new Label("请在年份输入框输入所查年份(负数表示公元前),并回车确定"),
                  BorderLayout.SOUTH) ;
     northPanel.validate();
     Container con=getContentPane();
     JSplitPane split=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
                                     northPanel,southPanel);
     
     con.add(split,BorderLayout.CENTER);
     con.validate();
   
     hashtable=new Hashtable();
     file=new File("日历备忘录.txt");
      if(!file.exists())
      {
       try{
           FileOutputStream out=new FileOutputStream(file);
           ObjectOutputStream objectOut=new ObjectOutputStream(out);
           objectOut.writeObject(hashtable);
           objectOut.close();
           out.close();
          }
       catch(IOException e)
          {
          }
      }
   
     notepad=new NotePad(this);                                      
     southPanel.add(notepad);
     
     设置日历牌(year,month);
     addWindowListener(new WindowAdapter()
                    { public void windowClosing(WindowEvent e)
                       {
                         System.exit(0);
                       }
                    });
    setVisible(true);
    setBounds(100,50,524,285);
    validate();
   }
  public void 设置日历牌(int year,int month)
   {
     日历.set(year,month-1,1);              
     
     星期几=日历.get(Calendar.DAY_OF_WEEK)-1;
     if(month==1||month==2||month==3||month==5||month==7
                        ||month==8||month==10||month==12)
        {
            排列号码(星期几,31);         
        }
     else if(month==4||month==6||month==9||month==11)
        {
            排列号码(星期几,30);
        }
     else if(month==2)
        {
         if((year%4==0&&year%100!=0)||(year%400==0))  
           {
             排列号码(星期几,29);
           }
         else
           {
             排列号码(星期几,28);
           }
       }
   }
  public void 排列号码(int 星期几,int 月天数)
   {
      for(int i=星期几,n=1;i<星期几+月天数;i++)
             {
               showDay[i].setText(""+n);
               if(n==day)
                 {
                   showDay[i].setForeground(Color.green);
                   showDay[i].setFont(new Font("TimesRoman",Font.BOLD,20));
                 }
               else
                 {
                  showDay[i].setFont(new Font("TimesRoman",Font.BOLD,12));
                  showDay[i].setForeground(Color.black);
                 }
               if(i%7==6)
                 {
                  showDay[i].setForeground(Color.blue);  
                 }
               if(i%7==0)
                 {
                  showDay[i].setForeground(Color.red);  
                 }
               n++;
             }
       for(int i=0;i<星期几;i++)
             {
                showDay[i].setText("");
             }
       for(int i=星期几+月天数;i<42;i++)
             {
                showDay[i].setText("");
             }
   }
 public int getYear()
   {
    return year;
   }
 public void setYear(int y)
   {
     year=y;
     notepad.setYear(year);
   }
 public int getMonth()
   {
    return month;
   }
 public void setMonth(int m)
   {
     month=m;
     notepad.setMonth(month);
   }
 public int getDay()
   {
    return day;
   }
 public void setDay(int d)
   {
    day=d;
    notepad.setDay(day);
   }
 public Hashtable getHashtable()
   {
     return hashtable;
   }
 public File getFile()
   {
     return file;
   }
 public void mousePressed(MouseEvent e)            
   {
     JTextField source=(JTextField)e.getSource();
     try{
         day=Integer.parseInt(source.getText());
         notepad.setDay(day);
         notepad.设置信息条(year,month,day);
         notepad.设置文本区(null);
         notepad.获取备忘内容(year,month,day);
        }
      catch(Exception ee)
        {
        }
   }
 public void mouseClicked(MouseEvent e)
   {
   }
 public void mouseReleased(MouseEvent e)
   {
   }
 public void mouseEntered(MouseEvent e)
   {
   }
 public void mouseExited(MouseEvent e)
   {
   }
 
 public static void main(String args[])
   {
       Calendar calendar=Calendar.getInstance();   
       int y=calendar.get(Calendar.YEAR);           
       int m=calendar.get(Calendar.MONTH)+1;        
       int d=calendar.get(Calendar.DAY_OF_MONTH);
       new CalendarPad(y,m,d);
   }
}
搜索更多相关主题的帖子: java 
2008-06-30 12:32
squallff
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2008-6-25
收藏
得分:0 
大后天要交这个软件上去...
希望大家能帮帮忙..感激不尽啊..
2008-06-30 17:42
快速回复:java新手..求救!求救!...请各位高手帮帮忙!..
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.086981 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved