问题
DESCRIPTION
On windows7 and with JDK1.8.0_20, I simply try to display some panes with a black border and with a given background color. I am using "setStyle" method for that, with the following documentation http://docs.oracle.com/javafx/2/api/javafx/scene/doc-files/cssref.html#region. The problem code is within the TestPane class. See below the full running code :
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));
}
}
}
After some tries, I can not mix this code :
// 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
with this one :
// second style part - start
this.setStyle("-fx-background-color: " + color.toString().replace("0x", "#") + ";");
// second style part - end
The last one seems to take over the first and do not display it. The first picture shows the border line sets before the background and the second shows the background sets before the border line.
QUESTION
How to display both style together? Cheers,
Jakez
回答1:
setStyle()
is a setter method, thus it does not append..
You want to combine all styles into one String
:
setStyle("-fx-border-color: #FFFFFF;-fx-border-width: 1px;-fx-border-style: solid;-fx-background-color: " + color.toString().replace("0x", "#") + ";");
来源:https://stackoverflow.com/questions/29631663/javafx2-can-not-mix-some-setstyle-on-pane