javafx写了一个时钟代码,求大神进来看看

进击的zmj 2014-12-16 08:26:56
怎么加线程让时钟动起来?时钟原先指向特定时间,按开始后从当前时间运行。本人新手,代码不成熟的地方还请大神指教
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);
}
}
...全文
290 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
进击的zmj 2014-12-23
  • 打赏
  • 举报
回复
参考别的资料完善了,需要的同学可以看看,写的不是很好
import java.util.*;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
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.HBox;
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;
import javafx.util.Duration;

public class text extends Application {
	 class testpane extends Pane {//testpane继承pane用来布局界面
	    private Timeline timeline;
	    private Button Start,Stop;
	    private Line line1,line2,line3;//秒针,分针,时针
		private double x1,x2,x3;//当前时间秒、分、时指向的时间
	    public testpane(){//构造函数
			HBox hbox=new HBox();//新建HBOX布局,放按钮的
			hbox.setSpacing(20);
			hbox.setLayoutX(310);
			hbox.setLayoutY(520);
			Start = new Button("Start");//建立start按钮
			Start.setPrefSize(80, 40);
			Stop = new Button("Stop");//建立stop按钮
			Stop.setPrefSize(80, 40);
			hbox.getChildren().addAll(Start,Stop);//将按钮加入HBOX
			getChildren().add(hbox);
			
			Start.setOnAction(new EventHandler<ActionEvent>() {
				public void handle(ActionEvent e) {
				    System.out.println("Start button clicked"); 
		          timeline.play();//动画开始
				  }
			});//start按钮功能
			Stop.setOnAction(new EventHandler<ActionEvent>() {
				  public void handle(ActionEvent e) {
				    System.out.println("Stop button clicked"); 
		  		    timeline.stop();//动画停止
				  }
			});//stop按钮功能
						
			Circle circle = new Circle(400,300,200, Color.web("white", 0));//建立圆形,边长为200,白色,透明度为0
			circle.setStroke(Color.web("black", 1));//给园边框
			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)));
				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)));
				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]);
				getChildren().add(text);		
			}
			line1 = new Line();
			line2 = new Line();
			line3 = new Line();
			paint();//画出初始时钟指针
			getChildren().addAll(line1,line2,line3);
		    timeline = new Timeline();//时间轴
	        timeline.setCycleCount(Timeline.INDEFINITE);
	        Duration duration = Duration.millis(1000);//设定时钟动画每1秒变一次,关键帧时间间隔
	        KeyFrame keyFrame = new KeyFrame(duration, new EventHandler<ActionEvent>() {

	            public void handle(ActionEvent event) {//动画变化的代码
	            	paint();//画出指针
	            }
	        });
	        timeline.getKeyFrames().add(keyFrame);	//时间轴获取关键帧
	        timeline.play();
	    }

		public void paint(){//画指针的函数			
			Calendar c = Calendar.getInstance();//取当前时间
			x1=c.get(Calendar.SECOND);
			x2=c.get(Calendar.MINUTE);
			x3=c.get(Calendar.HOUR)*5; 
			line1.setStartX(400);
			line1.setStartY(300);
			line1.setEndX(400+160*Math.sin(Math.PI*x1/30));
			line1.setEndY(300-160*Math.cos(Math.PI*x1/30));//秒针
			line1.setStroke(Color.RED);
			line2.setStartX(400);
			line2.setStartY(300);
			line2.setEndX(400+120*Math.sin(Math.PI*x2/30));
			line2.setEndY(300-120*Math.cos(Math.PI*x2/30));//分针
			line2.setStroke(Color.BLUE);
			line3.setStartX(400);
			line3.setStartY(300);
			line3.setEndX(400+80*Math.sin(Math.PI*x3/30));
			line3.setEndY(300-80*Math.cos(Math.PI*x3/30));//时针
			line3.setStroke(Color.GREEN);
		}
	}
	
    public void start(Stage stage) {
    	testpane pane=new testpane();
		Scene scene = new Scene(pane,800,600);
		stage.setTitle("Myclock"); //设置stage标题    
		stage.setScene(scene); // 把scene放入stage中    
	    stage.show();
	    stage.setResizable(false);//界面大小固定
    }

	public static void main(String[] args) {
		System.out.println("launch application");
	    launch(args);   
	}
}

50,531

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧