如何通过点击JavaFX中的按钮来创建光标类型? [英] How to cange a the cursor type by clicking on a button in JavaFX?

查看:699
本文介绍了如何通过点击JavaFX中的按钮来创建光标类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许这个问题傻,但我没有想到如何改变游标类型在 JavaFX应用程序但是当我运行应用程序并点击按钮它做任何事情,它应该设置 Cursor_WAIT ,然后在 WebView 然后返回 Cursor_DEFAULT ,因此这是我已经传递的代码:

Maybe this questionis silly but i didn't get any idea about how to change the Cursor type in JavaFX Application but when i run the application and clicking on the button it does anything it should be set the Cursor_WAIT and then loading a page in the WebViewand then return the Cursor_DEFAULT so this is the code i've traied:

  @FXML
private void tabfirst (ActionEvent ee) throws IOException { // for tha Chooser frame text.

            String hh = text11.getText();
            Socket socket = new Socket();

    try {

        //do work
        URL url = new URL (hh);
        url.getContent();
        WebEngine myWebEngine = web1.getEngine();
        myWebEngine.load(url.toExternalForm());
        //close the window chooser
        Stage stage = new Stage();
          Parent root = FXMLLoader.load(getClass().getResource("Choose.fxml"));
          Scene scene = new Scene(root);
         stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
              @Override public void handle(WindowEvent t) { } });
        //close cursor
        ancpa.setCursor(Cursor.DEFAULT);
        web1.setCursor(Cursor.DEFAULT);
        web2.setCursor(Cursor.DEFAULT);
        web3.setCursor(Cursor.DEFAULT);
        web4.setCursor(Cursor.DEFAULT);
        web5.setCursor(Cursor.DEFAULT);
        web6.setCursor(Cursor.DEFAULT);
        web7.setCursor(Cursor.DEFAULT);
        web8.setCursor(Cursor.DEFAULT);
        web9.setCursor(Cursor.DEFAULT);
    }
   catch (IOException e){
       final  Stage stg = new Stage();           
        stg.initModality(Modality.APPLICATION_MODAL);
        stg.initOwner(stg);
        stg.setTitle("Cannot connect to the internet /n Please Verify your connection internet");
        labelno.setText("Cannot connect to the internet...");
        //close chooser
        Stage stage = new Stage();
         Parent root = FXMLLoader.load(getClass().getResource("Choose.fxml"));
         stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
              @Override public void handle(WindowEvent t) { } });

       //set cursor
         ancpa.setCursor(Cursor.DEFAULT);
        web1.setCursor(Cursor.DEFAULT);
        web2.setCursor(Cursor.DEFAULT);
        web3.setCursor(Cursor.DEFAULT);
        web4.setCursor(Cursor.DEFAULT);
        web5.setCursor(Cursor.DEFAULT);
        web6.setCursor(Cursor.DEFAULT);
        web7.setCursor(Cursor.DEFAULT);
        web8.setCursor(Cursor.DEFAULT);
        web9.setCursor(Cursor.DEFAULT);
   } finally{
       try{ socket.close(); } catch (Exception e){ }
       }

}

所以请任何人帮助我提前感谢:)

So Please can anybody help me Thanks in advance :)

推荐答案

您的代码不完整。以下是您的操作方式:

Your code is incomplete. Here's how you could do it:

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;


public class WaitCursorDemo extends Application {

    Stage primaryStage;

    @Override
    public void start(Stage primaryStage) {

        this.primaryStage = primaryStage;

        HBox root = new HBox();

        Button button = new Button( "Wait 10 Seconds on Scene");
        button.setOnAction(e -> waitAction());
        root.getChildren().add( button);

        Button button2 = new Button( "Wait 10 Seconds On Button 2");
        button2.setOnAction(e -> waitAction( button2));
        root.getChildren().add( button2);

        Button button3 = new Button( "Wait 10 Seconds On Button 3");
        button3.setOnAction(e -> waitAction( button3));
        root.getChildren().add( button3);

        Scene scene = new Scene( root, 1600, 900);

        primaryStage.setScene( scene);
        primaryStage.show();

    }

    private void waitAction() {

        primaryStage.getScene().setCursor(Cursor.WAIT);

        Thread thread = new Thread( new Runnable() {

            @Override
            public void run() {

                try {

                    sleep(3000);

                } catch( Throwable th) {

                    th.printStackTrace();

                } finally {

                    Platform.runLater( () -> { primaryStage.getScene().setCursor(Cursor.DEFAULT); });

                }



            }
        });
        thread.start();

    }

    private void waitAction( Button button) {

        button.setCursor(Cursor.WAIT);

        Thread thread = new Thread( new Runnable() {

            @Override
            public void run() {

                try {

                    sleep(3000);

                } catch( Throwable th) {

                    th.printStackTrace();

                } finally {

                    // null means that the cursor from the scene will be used, see documentation of setCursor
                    Platform.runLater( () -> { button.setCursor( null); });

                }

            }
        });
        thread.start();

    }

    private void sleep( long ms) throws InterruptedException {

       Thread.sleep( ms);

    }

    public static void main(String[] args) {
        launch(args);
    }

}

这篇关于如何通过点击JavaFX中的按钮来创建光标类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆