旋转标签后,JavaFX UI冻结

时间:2017-05-29 23:27:29

标签: java javafx

在start(Stage primaryStage)之外的函数中的标签上使用setRotate()后,旋转起作用,但在标签上应用旋转后UI会冻结。

try {
    Platform.runLater(() -> 
        textLabel.setRotate((new Random().nextInt(30 - (-30) + 1) + (-30)))
    );
} catch (Exception e1) {
    e1.printStackTrace();
}

我需要更改什么才能使用户界面不会冻结?

编辑:根据要求,这是我的代码的更详细的部分。基本上发生的是用户可以单击按钮,然后文本应该旋转。它会旋转,但随后JavaFX UI完全冻结。

public class Main extends Application {

    @FXML
    private TextArea TextArea1;
    @FXML
    private Button Button1;
    @FXML
    private ImageView BackgroundImgView;

    @FXML
    public void doSomethingAfterPressingButton1() {
        try {
            String userText = TextArea1.getText();
            if (userText == null || userText.isEmpty())
                TextArea1.setText("Please enter a text into this field");
            else {
                TextArea1.setText(userText);
                doRotate(userText);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void doRotate(String text) {
        try {
            if (Button1.getText().equals("Rotate")) {
                Button1.setText("New");
                textLabel.setText(text);

                String[] colorArray = { "#ffa500", "#ffd106", "#ffffc0", "#ffffa8", "#ffff00", "#ffffc4", "#ffffdd",
                        "#fddc68", "#fee911", "#f9ed37", "#fcfa85", "#fcf852", "#d7fdec", "#d7edec", "#d7ddec",
                        "#d7cdec", "#d7bdec", "#00ffec", "#ff0000", "#89ff00", "#fff400", "#e300ff", "#fc59a3",
                        "#87c830", "#ffd400", "#fe7e0f", "#8e3ccb", "#00f9ff", "#5ff4da", "#ffe441", "#f2f411",
                        "#b8ff75", "#ffc100", "#c356ea", "#8ff243", "#71aef2", "#ea5645", "#ff0000", "#1dff00",
                        "#ff8a00", "#fcff00", "#ffca31", "#00fff9", "#ffdb00", "#15ff00", "#ff3300", "#8900ff",
                        "#1322a6", "#e81111", "#5a36a3", "#106125", "#abd91f", "#f4f1af", "#f7f960", "#fff400",
                        "#e3ff00", "#a7a10a", "#ff5b5b", "#ffc75d", "#b8ff52", "#91f7ff", "#ff82d4", "#91aaff",
                        "#ff9e9e", "#ff80c5", "#7afbff", "#8aff9c", "#fff518", "#ffc100", "#55b6ff", "#76ff0c",
                        "#bd6fff", "#ff7272", "#ffca78", "#fdff58", "#99ff2d", "#31ffdc", "#9600ff", "#bd00ff",
                        "#ff00e7", "#ff008d", "#ff005a", "#e1e6f5", "#efefef", "#f3ffea", "#ffe9e9", "#ffd8f8",
                        "#ff0000", "#1dff00", "#ff8a00", "#fcff00", "#ffca31", "#ba1818", "#336927", "#a3ff00",
                        "#008080", "#9500d8", "#daf0aa", "#b5e37f", "#9fc360", "#ffec7b", "#ecd860",
                        "#ffffff" };
                textLabel.setTextFill(Color.web(colorArray[new Random().nextInt(colorArray.length)]));

                textLabel.setVisible(true);
                TextArea1.setVisible(false);
                addrandomBackgroundImg();
                BackgroundImgView.setVisible(true);

                Platform.runLater(() -> textLabel.setRotate((new Random().nextInt(30 - (-30) + 1) + (-30))));

            } else {
                Button1.setText("Rotate");
                textLabel.setVisible(false);
                TextArea1.setVisible(true);
                BackgroundImgView.setVisible(false);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

0 个答案:

没有答案