JFXPanel,无法在线程中显示阴影

时间:2017-06-08 01:57:22

标签: java swing javafx

当你调整JWindow的大小时,会错误地显示DropShadow效果。

错误在线程中,因为如果你显示没有线程是正确的。

with thread without the thread

这是代码。

package prueba;

import java.awt.Window;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.effect.BlurType;
import javafx.scene.effect.DropShadow;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javax.swing.JWindow;

public class Prueba extends Application {
    JWindow jWindow = new JWindow();
    JFXPanel jFXPanel = new JFXPanel();
    AnchorPane root = new AnchorPane();

    @Override
    public void start(Stage primaryStage) {
        Scene scene = new Scene(createContent());
        scene.setFill(javafx.scene.paint.Color.TRANSPARENT);
        jFXPanel.setScene(scene);

        jWindow.setContentPane(jFXPanel);
        jWindow.getContentPane().setBackground(new java.awt.Color(1.0f,1.0f,1.0f,0.0f));
        jWindow.setBackground(new java.awt.Color(1.0f,1.0f,1.0f,0.0f));

        jWindow.setType(Window.Type.UTILITY);
        jWindow.setVisible(true);

        new Thread(() -> {
                for (int i = 0; i < 400; i++) {
                    jWindow.setSize(i, 40);
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(Prueba.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                for (int i = 40; i < 300; i++) {
                    jWindow.setSize(jWindow.getSize().width, i);
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(Prueba.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
        }).start();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

    private Parent createContent() {
        root.setStyle("-fx-background-color: transparent;");
        StackPane stackPane = new StackPane();
        stackPane.setStyle("-fx-background-color: green;");

        AnchorPane.setTopAnchor(stackPane, 5.0);
        AnchorPane.setBottomAnchor(stackPane, 5.0);
        AnchorPane.setLeftAnchor(stackPane, 5.0);
        AnchorPane.setRightAnchor(stackPane, 5.0);

        DropShadow dropShadow = new DropShadow(BlurType.ONE_PASS_BOX, javafx.scene.paint.Color.GRAY, 3, 0, 0, 0);
        dropShadow.setWidth(20);
        dropShadow.setHeight(20);
        stackPane.setEffect(dropShadow);
        Button btnAgregarContenido = new Button("Agregar contenido");
        stackPane.getChildren().add(btnAgregarContenido);

        root.getChildren().add(stackPane);
        return root;
    }
}

感谢您的帮助。

0 个答案:

没有答案