2014-09-10 58 views
1

我使用的版本的JavaFX 2开普勒,我试图自定义工具提示没有成功:如何用CSS自定义工具提示?

.tooltip{ 
    -fx-background-color: linear-gradient(blue,lightskyblue); 
    -fx-background-radius: 5; 
} 
.page-corner { 
    -fx-shape: " "; 
} 

这是行不通的。你有什么想法 ?

更新:

这里的MCVE:

import java.io.IOException; 
import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Scene; 
import javafx.scene.layout.AnchorPane; 
import javafx.stage.Stage; 

public class MainApp extends Application { 

    @Override 
    public void start(Stage stage) { 
    AnchorPane root; 
    try { 
     root = FXMLLoader.load(getClass().getResource("Sample.fxml")); 
     Scene scene = new Scene(root, 600, 400); 
     stage.setTitle("FXML Welcome"); 
     stage.setScene(scene); 
     stage.show(); 
    } catch (IOException e) { 
    // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 


    public static void main(String[] args) { 
    Application.launch(MainApp.class, args); 
    } 
} 

Sample.fxml文件:

<?xml version="1.0" encoding="UTF-8"?> 
<?import javafx.scene.text.*?> 
<?import javafx.scene.image.*?> 
<?import javafx.geometry.*?> 
<?import javafx.scene.effect.*?> 
<?import javafx.scene.*?> 
<?import java.lang.*?> 
<?import javafx.scene.control.*?> 
<?import javafx.scene.layout.*?> 
<?import javafx.scene.layout.AnchorPane?> 

<AnchorPane prefHeight="400.0" prefWidth="600.0" stylesheets="MCVE/application.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Sample"> 
    <children> 
     <TextField layoutX="66.0" layoutY="49.0"> 
     <tooltip> 
      <Tooltip text="tooltip message" /> 
     </tooltip> 
     </TextField> 
    </children> 
</AnchorPane> 

控制器:

public class Sample { 
//controller  
} 

application.css文件:

.tooltip{ 
    -fx-background-color: linear-gradient(blue,lightskyblue); 
    -fx-background-radius: 5.0; 
} 
.page-corner { 
    -fx-shape: " "; 
} 
+0

这对我来说工作正常。你可以发布[MCVE](http://stackoverflow.com/help/mcve)(即使用你的css文件的完整可执行示例)。 – 2014-09-10 13:11:24

回答

1

我想这是因为Tooltip出现在自己的窗口(TooltipPopupControl一个子类,最终是Window一个子类)。由于您已将样式表应用于AnchorPane,因此Tooltip不在该层次结构中。

相反,加在你MainApp类的样式表到Scene

scene.getStylesheets().add(getClass().getResource("/mcve/application.css").toExternalForm());