使用CSS在javaFX中使用样式按钮

时间:2014-07-30 18:11:41

标签: css intellij-idea javafx-2 javafx-8 fxml

我在使用CSS的javaFX中使用样式按钮时遇到问题。 我使用Intellij Idea IDE。 我有CSS.css文件:

#Button {
    -fx-padding: 8 15 15 15;
    -fx-background-insets: 0,0 0 5 0, 0 0 6 0, 0 0 7 0;
    -fx-background-radius: 8;
    -fx-background-color: 
        linear-gradient(from 0% 93% to 0% 100%, #a34313 0%, #903b12 100%),
        #9d4024,
        #d86e3a,
        radial-gradient(center 50% 50%, radius 100%, #d86e3a, #c54e2c);
    -fx-effect: dropshadow( gaussian , rgba(0,0,0,0.75) , 4,0,0,1 );
    -fx-font-weight: bold;
    -fx-font-size: 1.1em;
}
#Button:hover {
    -fx-background-color: 
        linear-gradient(from 0% 93% to 0% 100%, #a34313 0%, #903b12 100%),
        #9d4024,
        #d86e3a,
        radial-gradient(center 50% 50%, radius 100%, #ea7f4b, #c54e2c);
}
#Button:pressed {
    -fx-padding: 10 15 13 15;
    -fx-background-insets: 2 0 0 0,2 0 3 0, 2 0 4 0, 2 0 5 0;
}
#Button Text {
    -fx-fill: white;
    -fx-effect: dropshadow( gaussian , #a30000 , 0,0,0,2 );
}

sample.FXML

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

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="94.0" prefWidth="271.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Button id="Button" layoutX="74.0" layoutY="26.0" mnemonicParsing="false" stylesheets="@../../../../Desktop/CSS.css" text="MyButtonStyle" />
   </children>
</AnchorPane>

这是主要类: 包装样品;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        AnchorPane root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Test");
        primaryStage.setScene(new Scene(root, root.getPrefWidth(), root.getPrefHeight()));
        primaryStage.show();
    }


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

在javaFX场景构建器中,按钮样式已更改:

enter image description here

但是当我编译代码时,按钮会获得默认的CSS样式: enter image description here

为什么我编译代码时按钮样式不会改变?

3 个答案:

答案 0 :(得分:1)

为了解决我的问题,我使用JavaFX Scene Builder将.css文件的目录从Desktop更改为src\sample

enter image description here

答案 1 :(得分:1)

您的css文件没有问题。主要原因是CSS.css文件未与您的项目正确链接,因此现在将CSS.css文件放在项目的目录中,然后重试。 您可以在JavaFX CSS BUtton

了解详情

答案 2 :(得分:0)

这解决了我的问题

.button {
    -fx-padding: 8 15 15 15;
    -fx-background-insets: 0,0 0 5 0, 0 0 6 0, 0 0 7 0;
    -fx-background-radius: 8;
    -fx-background-color:
            linear-gradient(from 0% 93% to 0% 100%, #9b9d9e 0%, #7a7c7d 100%),
            #7a7c7d,
            #9b9d9e,
            radial-gradient(center 50% 50%, radius 100%, #7a7c7d, #9b9d9e);
    -fx-effect: dropshadow( gaussian , rgba(0,0,0,0.75) , 4,0,0,1 );
    -fx-font-weight: bold;
    -fx-font-size: 1.3em;
}
.button:hover {
    -fx-background-color:
            linear-gradient(from 0% 93% to 0% 100%, #9b9d9e 0%, #7a7c7d 100%),
            #7a7c7d,
            #9b9d9e,
            radial-gradient(center 50% 50%, radius 100%, #9b9d9e, #7a7c7d);
}
.button:pressed {
    -fx-padding: 10 15 13 15;
    -fx-background-insets: 2 0 0 0,2 0 3 0, 2 0 4 0, 2 0 5 0;
}
.button Text {
    -fx-fill: white;
    -fx-effect: dropshadow( gaussian , #a30000 , 0,0,0,2 );
}

项目结构

enter image description here

主类

public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("IeltsDatabaseAutomation.fxml"));
        primaryStage.setTitle("IELTS Speaking database automation");
        Scene scene = new Scene(root, 500, 600);
        scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.show();
    }