javafx写了一个时钟代码,求大神进来看看
怎么加线程让时钟动起来?时钟原先指向特定时间,按开始后从当前时间运行。本人新手,代码不成熟的地方还请大神指教
import java.util.*;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class 时钟 extends Application{
public void start(final Stage primaryStage) {
class btStartHandlerClass implements EventHandler<ActionEvent> {
public void handle(ActionEvent e) {
System.out.println("Start button clicked");
Calendar c = Calendar.getInstance();
x1=c.get(Calendar.SECOND);
x2=c.get(Calendar.MINUTE);
x3=c.get(Calendar.HOUR)*5;
start(primaryStage);
}
}
class btStopHandlerClass implements EventHandler<ActionEvent>{
public void handle(ActionEvent e) {
System.out.println("Stop button clicked");
x1=3;x2=39.5;x3=3.2;
start(primaryStage);
}
}
Pane pane = new Pane();
Button btStart = new Button("Start");
btStart.setLayoutX(410);
btStart.setLayoutY(520);
btStart.setPrefSize(80, 40);
Button btStop = new Button("Stop");
btStop.setLayoutX(310);
btStop.setLayoutY(520);
btStop.setPrefSize(80, 40);
btStartHandlerClass handler1=new btStartHandlerClass();
btStopHandlerClass handler2=new btStopHandlerClass();
btStart.setOnAction(handler1);
btStop.setOnAction(handler2);
pane.getChildren().add(btStart);
pane.getChildren().add(btStop);
Circle circle = new Circle(400,300,200, Color.web("white", 0));//建立圆形,边长为200,白色,透明度为0
circle.setStroke(Color.web("black", 1));//给园边框
pane.getChildren().add(circle);
for (int i = 0; i < 60; i++) {//短的表盘指针
Line line = new Line(400+200*Math.sin(Math.PI*i/30),(300-200*Math.cos(Math.PI*i/30)),400+190*Math.sin(Math.PI*i/30),(300-190*Math.cos(Math.PI*i/30)));
pane.getChildren().add(line);
}
for (int i = 0,j = 0; i < 60; i=i+5,j++) {//长的表盘指针和 表盘数字
String[] n={"12","1","2","3","4","5","6","7","8","9","10","11"};
Line line = new Line(400+200*Math.sin(Math.PI*i/30),(300-200*Math.cos(Math.PI*i/30)),400+180*Math.sin(Math.PI*i/30),(300-180*Math.cos(Math.PI*i/30)));
pane.getChildren().add(line);
Text text = new Text(395+170*Math.sin(Math.PI*i/30),(300-170*Math.cos(Math.PI*i/30)),n[j]);
pane.getChildren().add(text);
}
Line line3 = new Line(400,300,400+160*Math.sin(Math.PI*x1/30),(300-160*Math.cos(Math.PI*x1/30)));
pane.getChildren().add(line3);//秒针
line3.setStroke(Color.RED);
Line line2 = new Line(400,300,400+120*Math.sin(Math.PI*x2/30),(300-120*Math.cos(Math.PI*x2/30)));
pane.getChildren().add(line2);//分针
line2.setStroke(Color.BLUE);
Line line1 = new Line(400,300,400+80*Math.sin(Math.PI*x3/30),(300-80*Math.cos(Math.PI*x3/30)));
pane.getChildren().add(line1);//时针
line1.setStroke(Color.GREEN);
Scene scene = new Scene(pane, 800, 600);
primaryStage.setTitle("Myclock"); //设置stage标题
primaryStage.setScene(scene); // 把scene放入stage中
primaryStage.show();
}
double x1=3,x2=39.5,x3=3.2;
public static void main(String[] args) {
System.out.println("launch application");
launch(args);
}
}
...全文
相关推荐
精致javaFX模拟时钟
学习javaFX非常不错的示例,非常精致、代码也非常精练
利用javaFX写万年历
记事万年历。 设计一个记事万年历(如图3所示),可显示当前月份的日历、当前时间和记事本窗口。 (1)日历可以根据窗口提供的文本框中输入的年份和月份显示不同年月的日历信息; (2)时钟是动态的,即根据系统时间实时更新显示信息。 (3)点击“保存日志”按钮将所写日志以当前日志编辑区的日期为文件名(如20090411.txt)保存至磁盘中。 (4)点击“读取日志”按钮将从磁盘中找到指定日期的日志文件显示在日志编辑区中。 (5)点击“删除日志”按钮将日志编辑区中的字符内容清空。
javafx写的记事本
javafx写的记事本
用JavaFX做的一个登录注册界面
自己做的东西,第一次接错,看了点视频就做了,尤其是那个图片,还要他规定的格式,很难搞,要下个软件给ps装插件,再用ps转格式
中南大学JAVA实验报告-运用JavaFx画时钟运用JavaFx实现时钟动画.doc
中南大学JAVA实验报告-运用JavaFx画时钟运用JavaFx实现时钟动画


