如何通过单击JavaFX中的按钮来更改游标类型?

时间:2015-01-27 21:02:56

标签: javafx cursor

也许这个问题很愚蠢,但我对如何更改Cursor type中的JavaFX Application没有任何想法,但是当我运行应用程序并单击按钮时,它会执行任何操作设置Cursor_WAIT,然后在WebView中加载一个页面,然后返回Cursor_DEFAULT,这样就是我所追踪的代码:

  @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){ }
       }

}

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

1 个答案:

答案 0 :(得分:1)

您的代码不完整。这是你如何做到的:

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);
    }

}