我有一个学校作业,我必须创建一个简单的模拟和数字时钟,显示像这样“XX:XX”这样的时间并绘制一个时钟。如本例所示:
我用 Java 写了一些东西,但是我得到了各种各样的错误。这项任务的目标是学习实现类和定义方法等。如果有人能给我一些提示(不是实际的解决方案)关于什么是错误的或我可以改进的地方,我将非常感激。
主要代码
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class ShowClock {
public static void main (String[] args) {
double hour = Double.parseDouble(JOptionPane.showInputDialog("What time is it (hours)?"));
double minutes = Double.parseDouble(JOptionPane.showInputDialog("What time is it (minutes)?"));
String time= String.valueOf(hour) + String.valueOf(minutes) ;
JFrame frame = new JFrame("test app");
frame.setSize(300,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Hoe laat is het?");
Klok k = new Klok(hour, minutes);
frame.add(k);
frame.setVisible(true);;
}
}
类克洛克
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
class Klok extends JComponent {
private double hour;
private double minutes;
private String time;
int anglehour = (int)((90 - (hour + minutes / 60) * 30 ) * Math.PI / 180);
int angleminutes = (int)((90 - minutes * 6.0) * Math.PI / 180);
int xendpointhour = (int)(150+(75*Math.cos(anglehour)));
int yendpointhour = (int)(150-(75*Math.sin(anglehour)));
int xendpointminutes = (int)(150+(75*Math.cos(angleminutes)));
int yendpointminutes = (int)(150-(75*Math.sin(angleminutes)));
public void draw (Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.drawString(time, 0, 400);
g2.drawOval(75,75, 150, 150);
g2.drawLine(150,150, xendpointhour, yendpointhour);
g2.drawLine(150, 150, xendpointminutes, yendpointminutes);
}
}
更新:我还是不太明白。我想我需要一个关于构造函数和方法如何工作的简单解释,因为我的书不是很具描述性。很抱歉用这些菜鸟问题打扰任何人......
主班
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class KlokTonen {
public static void main(String[] args) {
double hour = Double.parseDouble(JOptionPane.showInputDialog("What time is it (hours)?"));
double minutes = Double.parseDouble(JOptionPane.showInputDialog("What time is it (minutes)?"));
String time= String.valueOf(hour) + String.valueOf(minutes) ;
JFrame frame = new JFrame("test app"); frame.setSize(300,400); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setTitle("Hoe laat is het?");
Klok k = new Klok(hour, minutes);
frame.add(k);
frame.setVisible(true);;
}
}
二等舱
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
public class Klok extends JComponent {
private double hour;
private double minutes;
private String time;
public void draw (Graphics g) {
Graphics2D g2 = (Graphics2D) g;
int angleHour = (int)((90 - (hour + minutes / 60) * 30 ) * Math.PI / 180);
int angleMinutes = (int)((90 - minutes * 6.0) * Math.PI / 180);
int xEndPointHour = (int)(150+(75*Math.cos(angleHour)));
int yEndPointHour = (int)(150-(75*Math.sin(angleHour)));
int xEndPointMinutes = (int)(150+(75*Math.cos(angleMinutes)));
int yEndPointMinutes = (int)(150-(75*Math.sin(angleMinutes)));
g2.drawString(time, 0, 400);
g2.drawOval(75,75, 150, 150);
g2.drawLine(150,150, xEndPointHour, yEndPointHour);
g2.drawLine(150, 150, xEndPointMinutes, yEndPointMinutes);
}
}