【发布时间】:2014-10-15 15:55:03
【问题描述】:
我开始为我的一个应用程序设计样式,但我发现了一些奇怪的东西。 这是一个反映这种行为的例子。您可能会注意到,有一个包含三个项目的菜单。我设置了上下文菜单(橙色)的样式,但似乎上下文菜单位于其他更宽的容器中。那个容器是什么以及如何设计它以获得自然的菜单。
这里也是一个代码:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class TestMenu extends Application {
@Override
public void start(Stage stage) throws Exception {
BorderPane borderPane = new BorderPane();
MenuBar menuBar = new MenuBar();
Menu menu = new Menu("File");
MenuItem menuItemSettings = new MenuItem("Settings");
MenuItem menuItemOpen = new MenuItem("Open");
MenuItem menuItemFile = new MenuItem("Exit");
TextField textField = new TextField();
textField.setPrefColumnCount(25);
menuBar.getMenus().addAll(menu);
menu.getItems().addAll(menuItemSettings, menuItemOpen, menuItemFile);
borderPane.setTop(menuBar);
borderPane.setCenter(textField);
Scene scene = new Scene(borderPane, 320, 200);
scene.getStylesheets().addAll("/testcss/style.css");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
还有样式表:
.root {
-fx-background-color: #222;
}
.text {
-fx-fill: white;
}
.menu-bar {
-fx-background-color: #007fa8;
}
.menu-item {
-fx-background-color: #222;
}
.menu-item:hover {
-fx-background-color: #3399B9;
}
.menu-item:pressed {
-fx-background-color: #004C65;
}
.context-menu {
-fx-background-color: orange;
}
提前致谢。
【问题讨论】:
-
您的问题可能与Tooltip backgrounds 的问题类似(我不确定)。您可能还想使用ScenicView 来分析节点层次结构。
-
或者使用当前的JavaFX Scene Builder。您可以在 View > Show CSS Analyzer 中找到 CSS 工具。在上半部分,您可以浏览样式化元素并复制 CSS 选择器。此外,您还可以看到,默认样式和 CSS 文件设置了哪些样式属性。
-
@jewelsea 是的!这是一个“工具提示背景”问题!我做了 Thomas Mikula 在他的回答中建议的事情,并且奏效了。非常感谢。