如何用css自定义工具提示?

时间:2014-09-10 12:23:41

标签: css javafx-2 tooltip

我正在使用版本javafx 2和kepler,我尝试定制工具提示但没有成功:

.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: " ";
}

1 个答案:

答案 0 :(得分:1)

我认为这是因为Tooltip出现在自己的窗口中(TooltipPopupControl的子类,最终是Window的子类。由于您已将样式表应用于AnchorPane,因此Tooltip无法看到该样式表,因为它位于该层次结构之外。

而是将样式表添加到Scene类中的MainApp

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