javafx实现轮播图

javafx实现轮播图_第1张图片
下面是轮播图的主体代码

Button buttonTest = new Button("");
buttonTest.setPrefSize(1300, 510);
//用线程实现轮播图
new Thread(new Runnable() {
@Override
public void run(){
while(true){

               //用button上文字的长度来实现循环,缺点是轮播图上会多几个小点
                if (buttonTest.getText().trim().length() == 0){
                    text = ".";
                    buttonTest.setStyle("-fx-background-image:url('/centerA.jpg')");**//图片1,用这种方式不支持png格式的图片
                   /* 
                   这是对这张图片时的事件编程,打开一个新窗口
                    buttonTest.setOnAction((ActionEvent event) -> { 
                        Center1 open = new Center1();
                        open.start(new Stage());
                    });   
                    */
                   
                }
                else if(buttonTest.getText().trim().length() == 1){
                    text = "..";
                    buttonTest.setStyle("-fx-background-image:url('/centerB.jpg')");
                    /*
                    对第二张图片的事件编程
                    buttonTest.setOnAction((ActionEvent event) -> { 
                        Center2 open = new Center2();
                        open.start(new Stage());
                    });    
                    */
     
                }
                else{
                    text = "";
                    buttonTest.setStyle("-fx-background-image:url('/centerC.jpg')");
                    */
                    对第三张照片时的事件编程
                    buttonTest.setOnAction((ActionEvent event) -> { 
                        Center3 open = new Center3();
                        open.start(new Stage());
                    });   
                    */
                      
                }
                
                Platform.runLater(new Runnable(){
                    @Override
                    public void run(){
                        buttonTest.setText(text);             
                    }
                });
                try {
                    Thread.sleep(6000);	//图片多少时间换一次
                } catch (InterruptedException ex) {
                }
            }
        }
    }).start();

你可能感兴趣的:(javafx实现轮播图)