在JavaFX 2.0中创建未修饰的阶段 [英] creating undecorated stage in javafx 2.0

查看:66
本文介绍了在JavaFX 2.0中创建未修饰的阶段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在javafx 2.0中创建一个自定义阶段.我希望我的舞台像其他窗口一样在屏幕上投下阴影...我尝试使用以下代码:

I am trying to create a custom stage in javafx 2.0. I want that my stage drops shadow on the screen as dropped by other windows... I tried with following code :

public class ChatWindow {
final private Stage stage = new Stage(StageStyle.UNDECORATED);
private Scene scene;
private Group rg;
private Text t = new Text();
private double initx = 0, inity = 0;

public ChatWindow() {

    rg = new Group();
    scene = new Scene(rg, 320, 240);
    //scene.setFill(null);
    scene.setFill(new Color(0, 0, 0, 0));
    stage.setScene(scene);
    stage.show();
    setupStage();
}

private void setupStage() {
    Rectangle r = new Rectangle(5, 5, stage.getWidth() - 10, stage.getHeight() - 10);
    r.setFill(Color.STEELBLUE);
    r.setEffect(new DropShadow());
    rg.setOnMousePressed(new EventHandler<MouseEvent>() {
        public void handle(MouseEvent me) {
            initx = me.getScreenX() - stage.getX();// - me.getSceneX(); 
            inity = me.getScreenY() - stage.getY();
        }
    });
    rg.setOnMouseDragged(new EventHandler<MouseEvent>() {

        public void handle(MouseEvent me) {
            stage.setX(me.getScreenX() - initx);
            stage.setY(me.getScreenY() - inity);
        }
    });
    rg.getChildren().add(r);
    rg.getChildren().add(t);
}

public void setVisible() {
    stage.show();
}
}

我可以看到阴影下降,但实际上它们是白色背景,阴影在其上下降. 因此,它是无用的,因为在彩色屏幕上缺陷是可见的,这会使它看起来很丑.

I can see the shadow fall, but actually their is a white background on which its falling. So, its useless, as on colored screen the defect will be visible, will make it look ugly..

这是白屏上的屏幕截图:

This is the screen shot on white screen :

在彩色屏幕上:

如何解决此问题?请帮忙.

HOw to resolve this issue?? Please help.

推荐答案

您应设置样式StageStyle.TRANSPARENT,请参见下面的代码:

You should set style StageStyle.TRANSPARENT, see next code:

public class ChatWindow extends Application {

    @Override
    public void start(final Stage stage) throws Exception {
        stage.initStyle(StageStyle.TRANSPARENT); // here it is

        Group rg = new Group();
        Scene scene = new Scene(rg, 320, 240, Color.TRANSPARENT);
        stage.setScene(scene);
        stage.show();

        Rectangle r = new Rectangle(5, 5, stage.getWidth() - 10, stage.getHeight() - 10);
        r.setFill(Color.STEELBLUE);
        r.setEffect(new DropShadow());
        rg.getChildren().add(r);
    }

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

这篇关于在JavaFX 2.0中创建未修饰的阶段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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