Unable to style horizontal or vertical boxes
P粉773659687
2023-09-03 10:20:53
<p>Very frustrating as I follow guides and basic tutorials. I can apply CSS styles to different elements, but not to vbox or hbox. </p>
<p>I have the following simple application, using FMXL and CSS to create a simple scene: </p>
<pre class="brush:php;toolbar:false;">import java.net.URL;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
public class BingRen extends Application {
@Override
public void start(Stage primaryStage) {
Parent root = null;
FXMLLoader loader = new FXMLLoader();
URL xmlUrl = getClass().getResource("/BingRen.fxml");
loader.setLocation(xmlUrl);
try {
root = loader.load();
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("BingRen.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}</pre>
<p>Using FXML, create just a BordPane and 2 HBoxes, each containing a label. Almost as easy as HellopApp: </p>
<pre class="brush:php;toolbar:false;"><?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.*?>
<BorderPane fx:id="rootBorderPane"
xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="MainControler">
<top>
<HBox>
<Label text="BingRen app" />
</HBox>
</top>
<bottom>
<HBox>
<Label text="Status bar" />
</HBox>
</bottom>
<center>
</center>
</BorderPane></pre>
<p>There is also CSS to set some basic properties:</p>
<pre class="brush:php;toolbar:false;">.hbox {
-fx-background-color: #00ff00;
-fx-border-color: #00ff00;
-fx-border-width: 2px;
-fx-padding: 10;
-fx-spacing: 8;
}
.label {
-fx-text-fill: #0000ff;
}</pre>
<p>The label correctly turns blue, but the hbox style is not applied</p>
<hr />
<p>In fact, none of these suggestions work. </p>
<p>I tried: </p>
<ul>
<li>Change the .hbox in the css file to .Hbox</li>
<li>Create #allbox in css file and add fx-id="allbox" and fxml file</li>
</ul>
<p>With each change, I change the color of the label to ensure the new version of the CSS can be read.</p>
<p>The labels always change color but I never get the background or padding in the horizontal box</p>
Why your current approach fails
ViewCSS Document一个>.
ForHBox
For tags
Therefore, there is no style class like ".hbox" for HBox unless you add one, which you haven't done yet.
CSS Selectors and JavaFX Backgrounds
Read the section titled "CSS and JavaFX Scene Graph":
Application Example
So you can solve this problem in three ways:
Using type selectors in CSS files:
Apply Style classes in CSS files:
and write in FXML:
Or write in the code:
Apply Style ID in CSS file:
and write in FXML:
Or write in the code:
Selector range difference
The meaning of the standard application for each method is different: