[英]How to add content to a PopupControl in JavaFX 2.2.6?
我想實現一個簡單的彈出控件,它應該可以用CSS設置樣式。 一切正常,唯一的問題是如何在其中添加內容(JavaFX中的節點)?
在JavaFX 2.2.6中不推薦使用PopupWindow.getContent()
方法而不使用CSS,我能夠看到內容,但CSS選擇器將不起作用。
那么,自己添加內容的最佳解決方案是什么呢,我應該為此目的實現自己的Skin類,還是有一種簡單的方法可以讓它工作?
我准備了一個簡單的用例:
import javafx.scene.control.Label;
import javafx.scene.control.PopupControl;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Rectangle;
public class PopupTest extends PopupControl {
public PopupTest() {
getStyleClass().add("popup"); // not working!?
StackPane pane = new StackPane();
pane.getStyleClass().add("pane");
Rectangle rectangle = new Rectangle(250, 250);
rectangle.getStyleClass().add("rect");
Label text = new Label("popup test");
text.getStyleClass().add("text");
pane.getChildren().addAll(rectangle, text);
// how to display to pane when the popup is shown?
getContent().addAll(pane);
}
}
為了完整性,這里是我的MainApplication和CSS文件:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class MainApplication extends Application {
@Override
public void start(Stage stage) throws Exception {
Group root = new Group();
final Scene scene = new Scene(root);
scene.getStylesheets().add(MainApplication.class.getResource("style.css").toExternalForm());
final Button button = new Button("show popup");
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
PopupTest popup = new PopupTest();
popup.show(scene.getWindow());
}
});
root.getChildren().add(button);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
style.css中:
.popup {
-fx-font-size: 24px;
}
.popup .rect {
-fx-fill: green;
}
.popup .text {
-fx-text-fill: white;
-fx-font-weight: bold;
}
“.popup”選擇器在這里不起作用,如果我將它設置為“窗格”,它將設置彈出窗口的樣式,使css正確: pane.getStyleClass().add("popup"); // working with this "fix"
pane.getStyleClass().add("popup"); // working with this "fix"
。
這似乎有效:
getScene().setRoot(pane);
關於樣式類不起作用:PopupControl沒有getStylesheets()
方法。 也許它只能由setStyle(...)
直接設置樣式? 您可以通過簡單的樣式pane
或通過將pane
包裝在根窗格中並設置樣式來解決此問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.