如何通过属性更改组件的样式?

我想在javafx中显示不同的标签,我想根据它们的文本设置它们的样式。我添加了一个css文件并设置了标签的类。然后,我检查了fxml,发现文本保存在text属性中。


我研究了正常的css,发现你可以通过属性来改变风格。为此,您需要使用 []。我在代码中尝试了一下,但它不起作用。


我的代码: FXML:


<?xml version="1.0" encoding="UTF-8"?>


<?import java.net.URL?>

<?import javafx.scene.control.Label?>

<?import javafx.scene.layout.HBox?>


<HBox xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1"

      fx:controller="controller">

    <stylesheets>

        <URL value="@../css/loadingScreen.css"/>

    </stylesheets>

    <Label styleClass="field" text="1" />

    <Label styleClass="field" text="2" />

    <Label styleClass="field" text="3" />

</HBox>

CSS:


.field {

    -fx-text-alignment: center;

    -fx-pref-height: 64px;

    -fx-min-width: 64px;

    -fx-pref-width: 64px;

    -fx-min-height: 64px;

    -fx-background-color: blue;

}


.field[text="1"]{

    -fx-background-color: red;

}


.field[text="2"]{

    -fx-background-color: yellow;

}


.field[text="3"]{

    -fx-background-color: green;

}

我尝试了与正常的css和html相同的方法,它在那里工作。网页:


<!DOCTYPE html>

<html>

<head>

<style>

.field[text="1"]{

    background-color: red;

}


.field[text="2"]{

    background-color: yellow;

}


.field[text="3"]{

    background-color: green;

}

</style>

</head>

<body>


<div class="field" text="1" >1</div>

<div class="field" text="2" >2</div>

<div class="field" text="3" >3</div>


</body>

</html>

我必须做些什么才能在fxml中做到这一点?


斯蒂芬大帝
浏览 85回答 2
2回答

噜噜哒

如果我改变文本,它也会自动改变风格选项 1:按 id控制样式 可以通过使用自定义标签来实现此目的,该标签在更改文本时更改样式。我将通过更改标签的 ID 来演示它。这个简化的示例使用文本作为 id:import javafx.geometry.Pos;import javafx.scene.control.Label;public class CustomLabel extends Label{&nbsp; &nbsp; public CustomLabel() {&nbsp; &nbsp; &nbsp; &nbsp; setAlignment(Pos.CENTER);&nbsp; &nbsp; &nbsp; &nbsp; setPrefSize(50, 25);&nbsp; &nbsp; }&nbsp; &nbsp; void setTextAndId(String s){&nbsp; &nbsp; &nbsp; &nbsp; super.setText(s);&nbsp; &nbsp; &nbsp; &nbsp; /*To keep this demo simple and clear id is changed.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If used, care must be taken to keep id unique.&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Using setStyle() or PseudoClass should be preferred&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; */&nbsp; &nbsp; &nbsp; &nbsp; setId(s);&nbsp;&nbsp; &nbsp; }}自定义标签可用于 fxml ():Root.fxml<?xml version="1.0" encoding="UTF-8"?><?import javafx.scene.control.Label?><?import javafx.scene.layout.StackPane?><?import tests.CustomLabel?><StackPane fx:id="root" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1"&nbsp;fx:controller="tests.Controller">&nbsp; &nbsp;<children>&nbsp; &nbsp; &nbsp; <CustomLabel fx:id="cLabel" text="&quot;&quot;" />&nbsp; &nbsp;</children></StackPane>一个简单的 css,它根据 id () 更改背景颜色:Root.css#1{&nbsp; &nbsp; -fx-background-color: red;&nbsp; &nbsp; -fx-text-fill: white;}#2{&nbsp; &nbsp; -fx-background-color: yellow;&nbsp; &nbsp; -fx-text-fill: red;}#3{&nbsp; &nbsp; -fx-background-color: green;&nbsp; &nbsp; -fx-text-fill: yellow;}测试类:import java.io.IOException;import javafx.application.Application;import javafx.fxml.FXMLLoader;import javafx.scene.Parent;import javafx.scene.Scene;import javafx.stage.Stage;public class LabelCssTest extends Application {&nbsp; &nbsp; @Override public void start(Stage stage) throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; Parent root = FXMLLoader.load(getClass().getResource("Root.fxml"));&nbsp; &nbsp; &nbsp; &nbsp; stage.setScene(new Scene(root));&nbsp; &nbsp; &nbsp; &nbsp; stage.show();&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; launch(args);&nbsp; &nbsp; }}和测试控制器:import javafx.animation.PauseTransition;import javafx.fxml.FXML;import javafx.scene.Parent;import javafx.util.Duration;public class Controller {&nbsp; &nbsp; @FXML&nbsp; &nbsp; CustomLabel cLabel;&nbsp; &nbsp; @FXML Parent root;&nbsp; &nbsp; private static final int MIN_VALUE = 1, MAX_VALUE = 3;&nbsp; &nbsp; private int counter = MIN_VALUE;&nbsp; &nbsp; @FXML&nbsp; &nbsp; private void initialize() {&nbsp; &nbsp; &nbsp; &nbsp; root.getStylesheets().add(getClass().getResource("Root.css").toExternalForm());&nbsp; &nbsp; &nbsp; &nbsp; cLabel.setTextAndId(String.valueOf(counter++));&nbsp; &nbsp; &nbsp; &nbsp; PauseTransition pause = new PauseTransition(Duration.seconds(2));&nbsp; &nbsp; &nbsp; &nbsp; pause.setOnFinished(event ->{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cLabel.setTextAndId(String.valueOf(counter++));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(counter > MAX_VALUE) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counter = MIN_VALUE;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pause.play();&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; pause.play();&nbsp; &nbsp; }}选项 2:通过更改样式类来控制样式 使用与选项 1 相同的测试类。Root.fxml:<?xml version="1.0" encoding="UTF-8"?><?import javafx.scene.control.Label?><?import javafx.scene.layout.StackPane?><StackPane fx:id="root" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="tests.Controller">&nbsp; &nbsp;<children>&nbsp; &nbsp; &nbsp; <Label fx:id="label" alignment="CENTER" contentDisplay="CENTER" prefHeight="20.0" prefWidth="70.0" text="&quot; &quot;" />&nbsp; &nbsp;</children></StackPane>Root.css:.style1{&nbsp; &nbsp; -fx-background-color: red;&nbsp; &nbsp; -fx-text-fill: white;}.style2{&nbsp; &nbsp; -fx-background-color: yellow;&nbsp; &nbsp; -fx-text-fill: red;}.style3{&nbsp; &nbsp; -fx-background-color: green;&nbsp; &nbsp; &nbsp;-fx-text-fill: yellow;}和控制器:import javafx.animation.PauseTransition;import javafx.beans.binding.Bindings;import javafx.beans.property.SimpleIntegerProperty;import javafx.beans.value.ChangeListener;import javafx.fxml.FXML;import javafx.scene.Parent;import javafx.scene.control.Label;import javafx.util.Duration;public class Controller {&nbsp; &nbsp; @FXML Label label;&nbsp; &nbsp; @FXML Parent root;&nbsp; &nbsp; private static final int MIN_VALUE = 1, MAX_VALUE = 3;&nbsp; &nbsp; private SimpleIntegerProperty counter = new SimpleIntegerProperty();&nbsp; &nbsp; @FXML&nbsp; &nbsp; private void initialize() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;root.getStylesheets().add(getClass().getResource("Root.css").toExternalForm());&nbsp; &nbsp; &nbsp; &nbsp; counter = new SimpleIntegerProperty();&nbsp; &nbsp; &nbsp; &nbsp; counter.addListener((ChangeListener<Number>) (observable, oldValue, newValue) -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; label.getStyleClass().clear();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; label.getStyleClass().add("style"+counter.get());&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; label.textProperty().bind(Bindings.createStringBinding(()->String.valueOf(counter.get()), counter));&nbsp; &nbsp; &nbsp; &nbsp; counter.set(1);&nbsp; &nbsp; &nbsp; &nbsp; PauseTransition pause = new PauseTransition(Duration.seconds(2));&nbsp; &nbsp; &nbsp; &nbsp; pause.setOnFinished(event ->{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counter.set(counter.get() >= MAX_VALUE ? MIN_VALUE : counter.get()+1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pause.play();&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; pause.play();&nbsp; &nbsp; }}Option 3: control style by using :Changes from option 2::PseudoClassRoot.css.root:style1 #label{&nbsp; &nbsp; -fx-background-color: red;&nbsp; &nbsp; -fx-text-fill: white;}.root:style2 #label{&nbsp; &nbsp; -fx-background-color: yellow;&nbsp; &nbsp; -fx-text-fill: red;}.root:style3 #label{&nbsp; &nbsp; -fx-background-color: green;&nbsp; &nbsp; -fx-text-fill: yellow;}And controller:import javafx.animation.PauseTransition;import javafx.beans.binding.Bindings;import javafx.beans.property.SimpleIntegerProperty;import javafx.beans.value.ChangeListener;import javafx.css.PseudoClass;import javafx.fxml.FXML;import javafx.scene.Parent;import javafx.scene.control.Label;import javafx.util.Duration;public class Controller {&nbsp; &nbsp; @FXML Label label;&nbsp; &nbsp; @FXML Parent root;&nbsp; &nbsp; private static final int MAX_VALUE = 3;&nbsp; &nbsp; private SimpleIntegerProperty counter = new SimpleIntegerProperty(1);&nbsp; &nbsp; @FXML&nbsp; &nbsp; private void initialize() {&nbsp; &nbsp; &nbsp; &nbsp; root.getStylesheets().add(getClass().getResource("Root.css").toExternalForm());&nbsp; &nbsp; &nbsp; &nbsp; counter = new SimpleIntegerProperty();&nbsp; &nbsp; &nbsp; &nbsp; counter.addListener((ChangeListener<Number>) (observable, oldValue, newValue) -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; updateStates();&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; label.textProperty().bind(Bindings.createStringBinding(()->String.valueOf(counter.get()), counter));&nbsp; &nbsp; &nbsp; &nbsp; counter.set(1);&nbsp; &nbsp; &nbsp; &nbsp; PauseTransition pause = new PauseTransition(Duration.seconds(2));&nbsp; &nbsp; &nbsp; &nbsp; pause.setOnFinished(event ->{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counter.set(counter.get() >= MAX_VALUE ? 1 : counter.get()+1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pause.play();&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; pause.play();&nbsp; &nbsp; }&nbsp; &nbsp; private void updateStates() {&nbsp; &nbsp; &nbsp; &nbsp; for( int index = 1; index <= MAX_VALUE; index++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PseudoClass pc = PseudoClass.getPseudoClass("style"+String.valueOf(index));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; root.pseudoClassStateChanged(pc, index == counter.get()&nbsp; ? true : false);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

jeck猫

如果您只想根据文本更改 的背景颜色,也可以创建一个这样的方法,并在需要时为每个方法调用它。LabelLabelimport javafx.scene.control.Label;import javafx.geometry.Insets;import javafx.scene.layout.Background;import javafx.scene.layout.BackgroundFill;import javafx.scene.layout.CornerRadii;import javafx.scene.paint.Color;&nbsp; &nbsp;&nbsp;private void backgroundColorTextDependent(Label label) {&nbsp; &nbsp; &nbsp; &nbsp; if (label.getText().equals("1")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; label.setBackground(new Background(new BackgroundFill(Color.BLACK, CornerRadii.EMPTY, Insets.EMPTY)));&nbsp; &nbsp; &nbsp; &nbsp; } else if (label.getText().equals("2")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; label.setBackground(new Background(new BackgroundFill(Color.YELLOW, CornerRadii.EMPTY, Insets.EMPTY)));&nbsp; &nbsp; &nbsp; &nbsp; } else if (label.getText().equals("3")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; label.setBackground(new Background(new BackgroundFill(Color.GREEN, CornerRadii.EMPTY, Insets.EMPTY)));&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; label.setBackground(new Background(new BackgroundFill(Color.BLUE, CornerRadii.EMPTY, Insets.EMPTY)));&nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java