该怎样写一个钟表的GUI?就是像系统时间里面的那个,每一秒秒针转动,每1min分针转动一下,1hour时针转一下
这样的GUI会不会很复杂?用Java自带的gui的类可不可以?

解决方案 »

  1.   

    jdk自带的demo不就有吗%JAVA_HOME%\demo\applets\Clock 到上面那个文件夹看看 
      

  2.   

    package com.clock;import javax.swing.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.*;
    import java.util.Calendar;
    import java.util.GregorianCalendar;@SuppressWarnings("serial")
    class Clock 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 Clock() {
    super("Java时钟");
    setDefaultCloseOperation(3);
    setSize(200, 200);
    setBackground(Color.white);
    setLocation(300, 150);
    setResizable(false);
    setVisible(true);
    int delay = 1000;
    //创建一个监听事件
    ActionListener drawClock = new ActionListener() {
    /* (非 Javadoc)
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    public void actionPerformed(ActionEvent evt) {
    repaint();
    }
    };
    //创建一个时间计数器
    new Timer(delay, drawClock).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.BLACK);
    //画圆
    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;
    }
    //获得现在时间
    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;
    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;
    //在窗体上显示时间
    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.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) {
    @SuppressWarnings("unused")
    Clock c = new Clock();
    } public void clear(Graphics g, int i, int j, int k, Color background) {
    // TODO 自动生成方法存根 }
    }
      

  3.   

    下面是一个简单的例子,复杂的明天整理一下再发到这来import java.awt.Dimension;
    import java.awt.FontMetrics;
    import java.awt.Graphics;
    import java.text.DecimalFormat;
    import java.util.Calendar;/** A simple Clock */
    public class Clock extends javax.swing.JComponent {
      protected DecimalFormat tflz, tf;  protected boolean done = false;  public Clock() {
        new Thread(new Runnable() {
          public void run() {
            while (!done) {
              Clock.this.repaint(); // request a redraw
              try {
                Thread.sleep(1000);
              } catch (InterruptedException e) { /* do nothing */
              }
            }
          }
        }).start();
        tf = new DecimalFormat("#0");
        tflz = new DecimalFormat("00");
      }  public void stop() {
        done = true;
      }  /* paint() - get current time and draw (centered) in Component. */
      public void paint(Graphics g) {
        Calendar myCal = Calendar.getInstance();
        StringBuffer sb = new StringBuffer();
        sb.append(tf.format(myCal.get(Calendar.HOUR)));
        sb.append(':');
        sb.append(tflz.format(myCal.get(Calendar.MINUTE)));
        sb.append(':');
        sb.append(tflz.format(myCal.get(Calendar.SECOND)));
        String s = sb.toString();
        FontMetrics fm = getFontMetrics(getFont());
        int x = (getSize().width - fm.stringWidth(s)) / 2;
        // System.out.println("Size is " + getSize());
        g.drawString(s, x, 10);
      }  public Dimension getPreferredSize() {
        return new Dimension(100, 30);
      }  public Dimension getMinimumSize() {
        return new Dimension(50, 10);
      }
      

  4.   

    6楼的编译通不过啊
    netbeans编译的结果:
    init:
    deps-jar:
    Warning: clock\ClockApp.java modified in the future.
    Compiling 3 source files to E:\code\clock\clock\build\classes
    E:\code\clock\clock\src\clock\ClockApp.java:8: 类 Clock 是公共的,应在名为 Clock.java 的文件中声明
    public class Clock extends javax.swing.JComponent { 
    E:\code\clock\clock\src\clock\ClockAboutBox.java:41: 找不到符号
    符号: 类 ClockApp
    位置: 软件包 clock
            org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(clock.ClockApp.class).getContext().getResourceMap(ClockAboutBox.class);
    E:\code\clock\clock\src\clock\ClockAboutBox.java:47: 找不到符号
    符号: 类 ClockApp
    位置: 软件包 clock
            javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(clock.ClockApp.class).getContext().getActionMap(ClockAboutBox.class, this);
    E:\code\clock\clock\src\clock\ClockView.java:87: 找不到符号
    符号: 变量 ClockApp
    位置: 类 clock.ClockView
                JFrame mainFrame = ClockApp.getApplication().getMainFrame();
    E:\code\clock\clock\src\clock\ClockView.java:91: 找不到符号
    符号: 变量 ClockApp
    位置: 类 clock.ClockView
            ClockApp.getApplication().show(aboutBox);
    E:\code\clock\clock\src\clock\ClockView.java:130: 找不到符号
    符号: 类 ClockApp
    位置: 软件包 clock
            org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(clock.ClockApp.class).getContext().getResourceMap(ClockView.class);
    E:\code\clock\clock\src\clock\ClockView.java:134: 找不到符号
    符号: 类 ClockApp
    位置: 软件包 clock
            javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(clock.ClockApp.class).getContext().getActionMap(ClockView.class, this);
    7 错误
    生成失败(总时间:0 秒)