JavaFX2:不能混合一些" setStyle"在窗格上

时间:2015-04-14 15:39:30

标签: background javafx styles pane

描述

在windows7和JDK1.8.0_20上,我只是尝试显示一些黑色边框和给定背景颜色的窗格。我正在使用" setStyle"方法,使用以下文档http://docs.oracle.com/javafx/2/api/javafx/scene/doc-files/cssref.html#region。问题代码在TestPane类中。 请参阅下面的完整运行代码:

package pane;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class BorderPaneApp extends Application {

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

@Override
public void start(Stage primaryStage) {
    primaryStage.setTitle("BorderPane");

    MainPane mainPane = new MainPane();

    Scene scene = new Scene( mainPane, 400, 300, Color.ORANGE);

    // do it for the layout
    mainPane.prefHeightProperty().bind(scene.heightProperty());
    mainPane.prefWidthProperty().bind(scene.widthProperty());

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

}


public class MainPane extends BorderPane{
    public TestPane topPane = new TestPane("top", Color.LIGHTSKYBLUE);
    public TestPane leftPane = new TestPane("left", Color.AQUA);
    public TestPane bottomPane = new TestPane("bottom", Color.AZURE);
    public TestPane centerPane = new TestPane("center", Color.LIGHTBLUE);

    public MainPane(){
        this.setTop(this.topPane);
        this.setLeft(this.leftPane);
        this.setCenter(this.centerPane);
        this.setBottom(this.bottomPane);
    }
}

public class TestPane extends BorderPane {
    public TestPane(String name, Color color){

        // first style part - start 
        this.setStyle("-fx-border-color: #FFFFFF;");
        this.setStyle("-fx-border-width: 1px;");
        this.setStyle("-fx-border-style: solid;");
        // first style part - end

        // second style part - start 
        this.setStyle("-fx-background-color: " + color.toString().replace("0x", "#") + ";");
        // second style part - end

        this.setCenter(new Text(name));
    }
}
}

经过一些尝试,我不能混合这段代码:

        // first style part - start 
        this.setStyle("-fx-border-color: #FFFFFF;");
        this.setStyle("-fx-border-width: 1px;");
        this.setStyle("-fx-border-style: solid;");
        // first style part - end

这一个:

        // second style part - start 
        this.setStyle("-fx-background-color: " + color.toString().replace("0x", "#") + ";");
        // second style part - end

最后一个似乎接管了第一个并且不显示它。第一张图片显示背景前的边框线设置,第二张图片显示边框线之前的背景设置。

enter image description here enter image description here

问题

如何同时显示两种风格? 欢呼声,

Jakez

1 个答案:

答案 0 :(得分:1)

setStyle()是一个 setter 方法,因此它不会附加..

您希望将所有样式合并为一个String

setStyle("-fx-border-color: #FFFFFF;-fx-border-width: 1px;-fx-border-style: solid;-fx-background-color: " + color.toString().replace("0x", "#") + ";");