0

我完全不知道我的时钟程序哪里出错了。

我的程序应该在几小时和几分钟内接受用户输入。然后用户的输入将时针和分针设置到其在弹出窗口中拉出的圆形时钟上的适当位置。

我的程序似乎没有接受用户输入,而是接受来自ClockApplet.java. 我不太确定如何让我的程序接受用户的输入。任何建议/输入/帮助将不胜感激!

我的代码如下:

时钟.java

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;

public class Clock
{
public Clock()
{
    this.anHour=12;
    this.aMin=0;
}

public Clock(double hr, double min)
{
    this.anHour=hr;
    this.aMin=min;
}

public void draw(Graphics g)
{
    Graphics2D g2=(Graphics2D)g;

    Ellipse2D.Double clock_face = new Ellipse2D.Double(0, 0, 2 * RADIUS, 
                                    2 * RADIUS);
    ((Graphics2D) g).draw(clock_face);

    Point2D.Double center=new Point2D.Double(RADIUS, RADIUS);
    double angle=Math.PI /2 - 2 * Math.PI * aMin / MINUTES_PER_HOUR;
    Point2D.Double minutePoint=new Point2D.Double(RADIUS + MINUTE_HAND
                                * Math.cos(angle), RADIUS - MINUTE_HAND * Math.sin(angle));
    Line2D.Double minuteHand=new Line2D.Double(center, minutePoint);
    ((Graphics2D) g).draw(minuteHand);

    angle=Math.PI / 2 - 2 * Math.PI * (anHour * MINUTES_PER_HOUR +
            aMin) / (MINUTES_PER_HOUR * HOURS_PER_DAY);
    Point2D.Double hourPoint=new Point2D.Double(RADIUS + HOUR_HAND *
                                Math.cos(angle), RADIUS - HOUR_HAND * Math.sin(angle));
    Line2D.Double hourHand=new Line2D.Double(center, hourPoint);
    ((Graphics2D) g).draw(hourHand);

    Clock clock1=new Clock(anHour, aMin);
}

private double anHour;
private double aMin;

final double RADIUS = 100;
final double MINUTES_PER_HOUR = 60;
final double HOURS_PER_DAY = 12;
final double HOUR_HAND = 75;
final double MINUTE_HAND = 90;
}

ClockApplet.java

import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JApplet;
import javax.swing.JOptionPane;

/**
 * This applet draws the clock shape.
 */

public class ClockApplet extends JApplet
{
    public ClockApplet()
    {
        String input;

    input=JOptionPane.showInputDialog(null, "Please enter the time (hh:mm):","Clock Applet", JOptionPane.QUESTION_MESSAGE);

    if((input==null) || (input.equals("")))
    {
        JOptionPane.showMessageDialog(null, "Invalid input", "Alert", JOptionPane.WARNING_MESSAGE);
        System.exit(0);
    }
    else
    {
        int colon1=input.indexOf(":");
        int colon2=input.lastIndexOf(":");

        double hours=findHour(colon1, input);
        double minutes=findMin(colon2, input);

        if(hours!=0 && minutes!=0)
        {
            Clock clock1=new Clock(hours, minutes);
        }
        else
        {
            JOptionPane.showMessageDialog(null, "Invalid input", "Alert", JOptionPane.WARNING_MESSAGE);
            System.exit(0);
        }
    }
}

public double findHour(int colon1, String input)
{
    String userHour=input.substring(colon1+1);
    double aHour=Double.parseDouble(userHour);
    double temp1=0.0;

    if(aHour>0 && aHour<=12)
    {
        temp1=aHour;
    }
    else
    {
        return 0;
    }

    return temp1;
}

public double findMin(int colon2, String input)
{
    String userMin=input.substring(colon2+1);
    double aMin=Double.parseDouble(userMin);
    double temp1=0.0;

    if(aMin>0 && aMin<=60)
    {
        temp1=aMin;
    }
    else
    {
        return 0;
    }

    return temp1;
}

public void paint(Graphics g)
{
    Graphics2D g2=(Graphics2D)g;

    Clock clock1=new Clock(10,10);

    clock1.draw(g2);

}

}

ClockApplet.html

<applet code="ClockApplet.class" width="50" height="50"> </applet>
4

2 回答 2

1

问题在于小时的解析。如果按照提示指定时间05:15,则会得到错误的子串。做这样的陈述:

System.out.println(/* the object I now have */);

..当它通过findHour()方法时,检查发生了什么。或者,使用调试器。

于 2012-06-04T05:49:38.687 回答
1

你需要将接受用户输入的代码添加到.so的life cycle方法中applet。所以将代码添加到applet的init()方法中,但我认为这里缺少它。所以oberride init()方法并添加代码:

 @Override
    public void init(){
    input=JOptionPane.showInputDialog(null, "Please enter the time (hh:mm):","Clock Applet", JOptionPane.QUESTION_MESSAGE); 

    }
于 2012-06-04T05:02:04.537 回答