C CSS样例
本附录列出了以下JavaFX样例的代码:
有关这些源文件的描述,请参见使用CSS为JavaFX应用程序设置外观。
DownloadButton.java
/* * 版权所有 (c) 2011, 2014, Oracle及其附属公司。 * 保留所有权利。使用受许可条款约束。 * * 此文件可在以下许可下使用和许可: * * 源代码的重新发布和使用,无论是否进行修改,只要满足以下条件: * * - 必须保留上述版权声明、此条件列表和以下免责声明。 * - 以二进制形式重新发布的代码必须在文档和/或其他提供的材料中复制上述版权声明、此条件列表和以下免责声明。 * - 未经特定事先书面许可,不得使用Oracle或其贡献者的名称来认可或推广从本软件派生的产品。 * * 本软件由版权持有人和贡献者“按原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性和适用于特定目的的保证。 * 在任何情况下,版权所有人或贡献者均不对任何直接、间接、偶然、特殊、惩罚性或后果性损害(包括但不限于替代商品或服务的采购、使用、数据或利润损失;或业务中断)承担任何责任,无论是合同责任、严格责任还是侵权行为(包括疏忽或其他)。 * 即使已被告知此类损害的可能性。 */ package uicontrolcss; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.HBox; import javafx.stage.Stage; import uicontrolcss.StyleStage.SceneCreator; /** * * 作者:Alexander Kouznetsov */ public class DownloadButton extends Application implements SceneCreator { public static void main(String[] args) { Application.launch(args); } @Override public void start(Stage stage) { StyleStage styleStage = new StyleStage(stage); styleStage.add("Style1", "DownloadButtonStyle1.css"); styleStage.add("Style2", "DownloadButtonStyle2.css"); stage.show(); styleStage.setSceneCreator(this); } @Override public Scene createScene() { Button download = new Button("下载"); download.getStyleClass().add("button1"); Button go = new Button("前往"); Button submit = new Button("提交"); submit.getStyleClass().add("button2"); HBox hBox = new HBox(10); hBox.getChildren().addAll(download, go, submit); return new Scene(hBox, 400, 100); } }
DownloadButtonStyle1.css
/* * 版权所有 (c) 2011, 2014, Oracle及其附属公司。 * 保留所有权利。使用受许可条款约束。 * * 此文件可在以下许可下使用和分发: * * 源代码的再分发和使用,无论是否进行修改,都是允许的,前提是满足以下条件: * * - 必须保留上述版权声明、此条件列表和以下免责声明。 * - 以二进制形式再分发时,必须在文档和/或其他提供的材料中复制上述版权声明、此条件列表和以下免责声明。 * - 未经特定事先书面许可,不得使用Oracle或其贡献者的名称来认可或推广从本软件派生的产品。 * * 本软件由版权持有人和贡献者“按原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性和适用于特定用途的保证。 * 在任何情况下,版权持有人或贡献者均不对任何直接、间接、偶然、特殊、惩罚性或后果性损害(包括但不限于替代商品或服务的采购、使用、数据或利润损失,或业务中断)承担任何责任,无论是合同责任、严格责任还是侵权行为(包括疏忽或其他)。 */ /* 文档:controlStyle */ .button { -fx-text-fill: #e4f3fc; -fx-font: bold 20pt "Tahoma"; -fx-padding: 10; -fx-color: #2d4b8e } .button:hover{ -fx-color: #395bae; }
DownloadButtonStyle2.css
/* * 版权所有 (c) 2011, 2014, Oracle和/或其关联公司。 * 保留所有权利。使用受许可条款约束。 * * 此文件可在以下许可下使用和许可: * * 源代码的重新分发和使用,无论是否进行修改,只要满足以下条件: * * - 必须保留上述版权声明、此条件列表和以下免责声明。 * - 在二进制形式的重新分发中,必须复制上述版权声明、此条件列表和以下免责声明到文档和/或其他提供的材料中。 * - 未经特定事先书面许可,不得使用Oracle或其贡献者的名称来认可或推广从本软件派生的产品。 * * 本软件由版权持有人和贡献者“按原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性和适用于特定目的的保证。 * 在任何情况下,版权持有人或贡献者均不对任何直接、间接、偶然、特殊、惩罚性或后果性损害(包括但不限于替代商品或服务的采购、使用损失、数据或利润损失、业务中断)承担责任,无论是合同责任、严格责任还是侵权行为(包括疏忽或其他)。 */ /* 文档 : controlStyleLong */ .button { -fx-text-fill: #e4f3fc; -fx-font: bold 20pt "Tahoma"; -fx-padding: 10; -fx-color: #2d4b8e } .button:hover{ -fx-color: #395bae; } .button1 { -fx-font: 20pt "Verdana"; -fx-text-fill: #5086bb; -fx-padding: 10; -fx-background-color: linear-gradient(#cbd0d7, white); -fx-background-radius: 23; -fx-border-radius: 23; -fx-border-color: #767676; } .button1:hover { -fx-background-color: linear-gradient(white, #cbd0d7); } .button2 { -fx-background-color: #a0b616; -fx-text-fill: white; -fx-font: bold 22pt "Courier New"; -fx-padding: 10; -fx-background-radius: 10; -fx-border-radius: 10; -fx-border-style: dotted; -fx-border-color: #67644e; -fx-border-width: 2; } .button2:pressed { -fx-scale-x: 0.8; -fx-scale-y: 0.8; }
StyleStage.java
/* * Copyright (c) 2011, 2014, Oracle and/or its affiliates. * All rights reserved. Use is subject to license terms. * * This file is available and licensed under the following license: * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the distribution. * - Neither the name of Oracle nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package uicontrolcss; import javafx.beans.Observable; import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.StringProperty; import javafx.beans.value.ObservableValue; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.ToggleButton; import javafx.scene.control.ToggleGroup; import javafx.scene.layout.HBox; import javafx.stage.Stage; import javafx.stage.WindowEvent; /** * * @author Alexander Kouznetsov */ public class StyleStage { private final StylePanel stylePanel; private final Stage stage; private Stage demoStage; public StyleStage(Stage stage) { this.stage = stage; stylePanel = new StylePanel(); Scene scene = new Scene(stylePanel); stage.setScene(scene); stage.setTitle("Choose style"); stage.setOnCloseRequest((WindowEvent t) -> { demoStage.close(); }); } public void add(String name, String styleSheetName) { stylePanel.add(name, styleSheetName); } public void setSceneCreator(final SceneCreator sceneCreator) { if (demoStage == null) { demoStage = new Stage(); demoStage.setTitle("Demo"); demoStage.setX(stage.getX()); demoStage.setY(stage.getY() + stage.getHeight()); } demoStage.setScene(sceneCreator.createScene()); demoStage.show(); stylePanel.selected.addListener((ObservableValue<? extends String> ov, String t, String t1) -> { demoStage.setScene(sceneCreator.createScene()); if (t1 != null) { demoStage.getScene().getStylesheets().setAll( UIControlCSS.class.getResource(t1).toString()); } }); } public static interface SceneCreator { Scene createScene(); } } class StylePanel extends HBox { public StringProperty selected = new SimpleStringProperty(); ToggleGroup stylesheetToggleGroup = new ToggleGroup(); public StylePanel() { super(5); StyleButton defaultStylesheetButton = new StyleButton("Default", null); defaultStylesheetButton.setSelected(true); defaultStylesheetButton.setToggleGroup(stylesheetToggleGroup); setPadding(new Insets(0, 0, 30, 0)); setAlignment(Pos.BOTTOM_LEFT); getChildren().addAll(defaultStylesheetButton); } public void add(String name, String styleSheetName) { StyleButton styleButton = new StyleButton(name, styleSheetName); styleButton.setToggleGroup(stylesheetToggleGroup); getChildren().addAll(styleButton); } class StyleButton extends ToggleButton { public StyleButton(String text, final String styleSheetName) { super(text); selectedProperty().addListener((Observable ov) -> { selected.set(styleSheetName); }); } } }
UIControlCSS.java
/* * Copyright (c) 2011, 2014, Oracle and/or its affiliates. * All rights reserved. Use is subject to license terms. * * This file is available and licensed under the following license: * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the distribution. * - Neither the name of Oracle nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package uicontrolcss; import javafx.application.Application; import javafx.beans.binding.Bindings; import javafx.collections.FXCollections; import javafx.event.ActionEvent; import javafx.geometry.Insets; import javafx.geometry.Orientation; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.CheckBox; import javafx.scene.control.ChoiceBox; import javafx.scene.control.Label; import javafx.scene.control.PasswordField; import javafx.scene.control.ProgressIndicator; import javafx.scene.control.RadioButton; import javafx.scene.control.ScrollPane; import javafx.scene.control.Separator; import javafx.scene.control.Slider; import javafx.scene.control.TextField; import javafx.scene.control.ToggleGroup; import javafx.scene.control.Tooltip; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.stage.Stage; import uicontrolcss.StyleStage.SceneCreator; /** * * @author Alexander Kouznetsov */ public class UIControlCSS extends Application implements SceneCreator { /** * @param args the command line arguments */ public static void main(String[] args) { Application.launch(args); } @Override public void start(Stage stage) { StyleStage styleStage = new StyleStage(stage); styleStage.add("controlStyle1", "controlStyle1.css"); styleStage.add("controlStyle2", "controlStyle2.css"); stage.show(); styleStage.setSceneCreator(this); } @Override public Scene createScene() { ToggleGroup toggleGroup = new ToggleGroup(); RadioButton radioButton1 = new RadioButton("High"); radioButton1.setToggleGroup(toggleGroup); radioButton1.setSelected(true); RadioButton radioButton2 = new RadioButton("Medium"); radioButton2.setToggleGroup(toggleGroup); RadioButton radioButton3 = new RadioButton("Low"); radioButton3.setToggleGroup(toggleGroup); VBox vBox1 = new VBox(2); vBox1.getChildren().addAll(radioButton1, radioButton2, radioButton3); TextField textField = new TextField(); textField.setPrefColumnCount(10); textField.setPromptText("Your name"); PasswordField passwordField = new PasswordField(); passwordField.setPrefColumnCount(10); passwordField.setPromptText("Your password"); VBox vBox2 = new VBox(); vBox2.getChildren().addAll(textField, passwordField); ChoiceBox<String> choiceBox = new ChoiceBox<>( FXCollections.observableArrayList("English", "???????", "Fran\u00E7ais")); choiceBox.setTooltip(new Tooltip("Your language")); choiceBox.getSelectionModel().select(0); HBox hBox1 = new HBox(5); hBox1.setAlignment(Pos.BOTTOM_LEFT); hBox1.getChildren().addAll(vBox1, vBox2, choiceBox); final Label label1 = new Label("Not Available"); label1.getStyleClass().add("borders"); Button button1 = new Button("Accept"); button1.getStyleClass().add("button1"); button1.setOnAction((ActionEvent t) -> { label1.setText("Accepted"); }); Button button2 = new Button("Decline"); button2.getStyleClass().add("button2"); button2.setOnAction((ActionEvent t) -> { label1.setText("Declined"); }); HBox hBox2 = new HBox(10); hBox2.setAlignment(Pos.CENTER_LEFT); hBox2.getChildren().addAll(button1, button2, label1); CheckBox checkBox1 = new CheckBox("Normal"); Separator separator = new Separator(Orientation.VERTICAL); separator.setPrefSize(1, 15); CheckBox checkBox2 = new CheckBox("Checked"); checkBox2.setSelected(true); CheckBox checkBox3 = new CheckBox("Undefined"); checkBox3.setIndeterminate(true); checkBox3.setAllowIndeterminate(true); HBox hBox3 = new HBox(12); hBox3.getChildren().addAll(checkBox1, separator, checkBox2, checkBox3); Label label2 = new Label("Progress:"); label2.getStyleClass().add("borders"); Slider slider = new Slider(); ProgressIndicator progressIndicator = new ProgressIndicator(0); progressIndicator.progressProperty().bind(Bindings.divide( slider.valueProperty(), slider.maxProperty())); HBox hBox4 = new HBox(10); hBox4.getChildren().addAll(label2, slider, progressIndicator); final VBox vBox = new VBox(20); vBox.setPadding(new Insets(30, 10, 30, 10)); vBox.setAlignment(Pos.TOP_LEFT); vBox.getChildren().setAll(hBox1, hBox2, hBox3, hBox4); ScrollPane scrollPane = new ScrollPane(); scrollPane.setContent(vBox); return new Scene(scrollPane, 500, 350); } }
controlStyle1.css
/* * 版权所有 (c) 2011, 2014, Oracle及其附属公司。 * 保留所有权利。使用受许可条款约束。 * * 此文件可在以下许可下使用和许可: * * 源代码的重新发布和使用,无论是否进行修改,只要满足以下条件: * * - 必须保留上述版权声明、此条件列表和以下免责声明。 * - 以二进制形式重新发布的代码必须在文档和/或其他提供的材料中复制上述版权声明、此条件列表和以下免责声明。 * - 未经特定事先书面许可,不得使用Oracle或其贡献者的名称来认可或推广从本软件派生的产品。 * * 本软件由版权持有人和贡献者“按原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性和适用于特定目的的保证。 * 在任何情况下,版权持有人或贡献者均不对任何直接、间接、偶然、特殊、惩罚性或后果性损害(包括但不限于替代商品或服务的采购、使用、数据或利润损失,或业务中断)承担责任,无论是在合同、严格责任或侵权行为(包括疏忽或其他)的任何理论下,即使事先已被告知此类损害的可能性。 */ /* 文档:controlStyle1 */ .root{ -fx-font-size: 14pt; -fx-font-family: "Tahoma"; -fx-base: #DFB951; -fx-background: #A78732; -fx-focus-color: #B6A678; } .button1{ -fx-text-fill: #006464; -fx-background-color: #DFB951; -fx-border-radius: 20; -fx-background-radius: 20; -fx-padding: 5; } .button2{ -fx-text-fill: #c10000; -fx-background-color: #DFB951; -fx-border-radius: 20; -fx-background-radius: 20; -fx-padding: 5; } .slider{ -fx-border-color: white; -fx-border-style: dashed; -fx-border-width: 2; }
controlStyle2.css
/* * 版权所有 (c) 2011, 2014, Oracle及其附属公司。 * 保留所有权利。使用受许可条款约束。 * * 此文件可在以下许可下使用和许可: * * 源代码的重新分发和使用,无论是否进行修改,只要满足以下条件: * * - 必须保留上述版权声明、此条件列表和以下免责声明。 * - 以二进制形式重新分发时,必须在文档和/或其他提供的材料中复制上述版权声明、此条件列表和以下免责声明。 * - 未经特定事先书面许可,不得使用Oracle或其贡献者的名称来认可或推广从本软件派生的产品。 * * 本软件由版权持有人和贡献者“按原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性和适用性的暗示保证。 * 在任何情况下,版权所有人或贡献者均不对任何直接、间接、偶然、特殊、惩罚性或后果性损害(包括但不限于替代商品或服务的采购、使用、数据或利润损失,或业务中断)承担任何责任,无论是合同责任、严格责任还是侵权行为(包括疏忽或其他)。 */ /* 文档 : controlStyle2 */ .root{ -fx-font-size: 16pt; -fx-font-family: "Courier New"; -fx-base: rgb(132, 145, 47); -fx-background: rgb(225, 228, 203); } .button{ -fx-text-fill: rgb(49, 89, 23); -fx-border-color: rgb(49, 89, 23); -fx-border-radius: 5; -fx-padding: 3 6 6 6; } .borders{ -fx-border-color: rgb(103, 100, 78); -fx-border-style: dotted; -fx-border-width: 1.5; -fx-border-insets: -5; }