简单地创建一个新控件后,Stage会丢失填充(渐变)

时间:2016-05-30 10:17:41

标签: javafx

我已将此重新创建减少到以下内容。 ToggleButton被实例化的行会导致我的舞台失去填充色;它变白了。我刚刚开始使用JavaFX,所以如果我在做一些我不应该做的事情,请告诉我。这是在Windows 7 sp1上使用jre1.8.0_92和Eclipse Neon(jfx8_2.3.0插件)。

public class Main extends Application {

    public static void main(String[] args) {

        if(args.length > 0) { 
            String s = args[0].toLowerCase();
            if(s.equals("full"))
                    Machine.isFullScreen = true;
        }
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        Machine.startMachine(primaryStage);
    } 
}

public class Machine {

    static boolean isFullScreen = false;
    static Rectangle2D screenRect, backRect;
    static Stage backStage;
    static Scene backScene;
    static Pane backPane;

    private Machine() {}

    static public void startMachine(Stage primaryStage) {

        // backscreen
        startScene(primaryStage);

        // This line causes the fill to be lost
        ToggleButton foo = new ToggleButton("hi");
    }

    static private void startScene(Stage primaryStage) {

        // Stage
        backStage = primaryStage;
        backStage.initStyle(StageStyle.UNDECORATED);
        backStage.setFullScreen(isFullScreen);
        screenRect = Screen.getPrimary().getBounds();
        if(!isFullScreen) {
            int w = 1000, h = 500, t = 20;
            backStage.setWidth(w);
            backStage.setHeight(h);
            backStage.setX((screenRect.getWidth() - w)/2);
            backStage.setY(t);
        }
        backRect = new Rectangle2D(backStage.getX(), backStage.getY(),
                backStage.getWidth(), backStage.getHeight());

        // Scene
        backScene = new Scene(backPane = new Pane());
        // backScene.getStylesheets().add(Machine.class.getResource("mainStyle.css").toExternalForm());
        // backScene.getRoot().setStyle("-fx-background-color: #CCFF99;");
        backScene.setFill(new LinearGradient(0,0,1,1, true, CycleMethod.NO_CYCLE,
            new Stop[]{
                    new Stop(0,Color.web("#4977A3")),
                    new Stop(0.5, Color.web("#B0C6DA")),
                    new Stop(1,Color.web("#9CB6CF")), } )); 
        // Logo
        Text logo = new Text("AMT");
        logo.setFill(Color.DEEPSKYBLUE);
        Font font = Font.font("Times New Roman", FontWeight.BOLD, FontPosture.ITALIC, 96);
        logo.setFont(font);
        logo.setX(100);
        logo.setY(150);
        backPane.getChildren().add(logo);
        backStage.setScene(backScene);
        backStage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
        backStage.show();
    }
}

1 个答案:

答案 0 :(得分:1)

setFill() API表明这可能是样式表效应。在第一个Control实例化时,将静态安装默认样式表。如果该样式表为Modena,则“默认填充设置为浅灰色。”请按照建议herehere尝试backScene.setFill(),而不是backPane.setBackground()

image

// Scene
backPane = new Pane();
backScene = new Scene(backPane);
LinearGradient linearGradient = new LinearGradient(
    0, 0, 1, 1, true, CycleMethod.NO_CYCLE,
    new Stop(0, Color.web("#4977A3")),
    new Stop(0.5, Color.web("#B0C6DA")),
    new Stop(1, Color.web("#9CB6CF")));
backPane.setBackground(new Background(new BackgroundFill(
    linearGradient, CornerRadii.EMPTY, Insets.EMPTY)));

另外,请注意varargsLinearGradient构造函数参数允许您直接添加Stop的实例,而无需创建新数组。