gpt4 book ai didi

java - 使用Java的时钟程序(JApplet)

转载 作者:行者123 更新时间:2023-12-02 07:45:54 26 4
gpt4 key购买 nike

我完全不知道我的时钟程序出了什么问题。

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

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

我的代码如下:

时钟.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>

最佳答案

您需要将接受用户输入的代码添加到小程序的生命周期方法中。因此将代码添加到小程序的 init() 方法中,但是我认为这里缺少它。因此,使用 init() 方法并添加代码:

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

}

关于java - 使用Java的时钟程序(JApplet),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10876446/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com