0

我有这个带有切换按钮的基本示例:

public class test extends Application
{

    @Override
    public void start(Stage primaryStage)
    {

        final VBox vbox = new VBox();

        String pillButtonCss = DX57DC.class.getResource("PillButton.css").toExternalForm();

        // create 3 toggle buttons and a toogle group for them
        ToggleButton tb1 = new ToggleButton("Left Button");
        tb1.setId("pill-left");
        ToggleButton tb2 = new ToggleButton("Center Button");
        tb2.setId("pill-center");
        ToggleButton tb3 = new ToggleButton("Right Button");
        tb3.setId("pill-right");

        final ToggleGroup group = new ToggleGroup();
        tb1.setToggleGroup(group);
        tb2.setToggleGroup(group);
        tb3.setToggleGroup(group);
        // select the first button to start with
        group.selectToggle(tb1);

        final Rectangle rect1 = new Rectangle(300, 300);
        rect1.setFill(Color.DARKOLIVEGREEN);
        final Rectangle rect2 = new Rectangle(300, 300);
        rect2.setFill(Color.LAVENDER);
        final Rectangle rect3 = new Rectangle(300, 300);
        rect3.setFill(Color.LIGHTSLATEGREY);

        tb1.setUserData(rect1);
        tb2.setUserData(rect2);
        tb3.setUserData(rect3);

        group.selectedToggleProperty().addListener(new ChangeListener<Toggle>()
        {
            @Override
            public void changed(ObservableValue<? extends Toggle> ov, Toggle toggle, Toggle new_toggle)
            {
                if (new_toggle == null)
                {
                }
                else
                {
                    vbox.getChildren().set(1, (Node) group.getSelectedToggle().getUserData());
                }
            }
        });

        HBox hBox = new HBox();
        hBox.getChildren().addAll(tb1, tb2, tb3);
        hBox.setPadding(new Insets(10, 10, 10, 10));
        hBox.getStylesheets().add(pillButtonCss);

        vbox.getChildren().addAll(hBox, (Node) group.getSelectedToggle().getUserData());

        StackPane root = new StackPane();
        root.getChildren().add(vbox);

        Scene scene = new Scene(root, 800, 850);

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

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

我注意到,如果我在切换按钮时将繁重的业务逻辑放入按钮中,则应用程序似乎挂起。

切换按钮时是否可以显示“正在加载..”而不是按钮标签?

4

1 回答 1

1

我真的不知道它是如何挂起的,但您可以尝试在后台线程中更改工作区。与 Button 标签一起,这可能看起来像这样:

public class test
    extends Application
{

 BorderPane workspace = new BorderPane();

 @Override
 public void start(Stage primaryStage)
 {

     final VBox vbox = new VBox();

//  String pillButtonCss = DX57DC.class.getResource("PillButton.css").toExternalForm();

     // create 3 toggle buttons and a toogle group for them
     ToggleButton tb1 = new ToggleButton("Left Button");
     tb1.setId("pill-left");
     ToggleButton tb2 = new ToggleButton("Center Button");
     tb2.setId("pill-center");
     ToggleButton tb3 = new ToggleButton("Right Button"); 
     tb3.setId("pill-right");

     final ToggleGroup group = new ToggleGroup();
     tb1.setToggleGroup(group);
     tb2.setToggleGroup(group);
     tb3.setToggleGroup(group);

     final Rectangle rect1 = new Rectangle(300, 300);
     rect1.setFill(Color.DARKOLIVEGREEN);
     final Rectangle rect2 = new Rectangle(300, 300);
     rect2.setFill(Color.LAVENDER);
     final Rectangle rect3 = new Rectangle(300, 300);
     rect3.setFill(Color.LIGHTSLATEGREY);

     tb1.setUserData(rect1);
     tb2.setUserData(rect2);
     tb3.setUserData(rect3);

     group.selectedToggleProperty().addListener(new ChangeListener<Toggle>()
     {
        @Override
        public void changed(ObservableValue<? extends Toggle> ov,
                            Toggle toggle,
                            final Toggle new_toggle)
        {
            if (new_toggle != null)
            {
                final String toggleText = ((ToggleButton)new_toggle).getText();
                ((ToggleButton)new_toggle).setText("Loading...");

                Thread changeThread = new Thread(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        try
                        {
                            Thread.sleep(5000);
                        }
                        catch (InterruptedException e)
                        {
                        }

                        Platform.runLater(new Runnable()
                        {
                            @Override
                            public void run()
                            {
                                workspace.setCenter((Node)new_toggle.getUserData());
                                ((ToggleButton)new_toggle).setText(toggleText);
                            }
                        });
                    }
                });
                changeThread.setDaemon(true);
                changeThread.start();
            }
        }
     });

     HBox hBox = new HBox();
     hBox.getChildren().addAll(tb1, tb2, tb3);
     hBox.setPadding(new Insets(10, 10, 10, 10));
 //  hBox.getStylesheets().add(pillButtonCss);

     vbox.getChildren().addAll(hBox, workspace);

     Scene scene = new Scene(vbox, 800, 850);

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

     // select the first button to start with
     group.selectToggle(tb1);
 }

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

Thread.sleep(5000) 模拟切换时的“滞后”。

于 2013-07-23T12:17:06.450 回答