菜单栏中的 JavaFX ChoiceBox

标签 java javafx

我使用 JavaFX 制作了一个应用程序。

我想在我的顶部 MenuBar 中有一个 ChoiceBox 来选择语言。 我想在选择语言控件时禁用蓝色周围。我该怎么做?

image[1]

最佳答案

将包含 ChoiceBoxMenuId 设置为 透明 并使用以下 CSS:

#transparent:hover,
#transparent:focused,
#transparent:showing {
    -fx-background: transparent;
}

不幸的是,我还不知道如何使用 transparent 作为一个类来做到这一点。

<小时/>

示例

输出: enter image description here

代码:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class Foo extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Menu fileMenu = new Menu("File");

        ChoiceBox<String> languageBox = new ChoiceBox<String>();
        languageBox.getItems().addAll("English", "Deutsch");

        Menu languageMenu = new Menu();
        languageMenu.setId("transparent");
        languageMenu.setGraphic(languageBox);

        MenuBar menuBar = new MenuBar();
        menuBar.getMenus().addAll(fileMenu, languageMenu);

        BorderPane root = new BorderPane();
        root.setTop(menuBar);

        Scene scene = new Scene(root);
        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());

        stage.setScene(scene);
        stage.setTitle("MCVE");
        stage.setWidth(640);
        stage.setHeight(480);
        stage.show();
    }

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

}

关于菜单栏中的 JavaFX ChoiceBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38817465/

相关文章:

java - 空白窗口 JavaFX

JavaFX 新场景或新 Pane

java - 如何在 JavaFX 中使代码定期刷新?

java - 如何更改 CSS 第 7 系列上方的 javaFX LineChart 中的图标?

java - JSF 和 Facelets 流程

java - Hello World ,Java Applet,问题

java - 谷歌应用引擎 session

JavaFx 无法执行 javascript 函数

java - 在java中调用类,使用字符串中的类名

java - SwingWorker、Thread.sleep() 还是 javax.swing.timer?我需要 "insert a pause"