【问题标题】:JavaFX Css Context Menu not workingJavaFX Css上下文菜单不起作用
【发布时间】:2015-09-20 05:34:39
【问题描述】:

我正在使用带有上下文菜单的 JavaFX 创建一个软件,我想将 css 应用到该菜单。显然css没有显示。我想知道为什么这不起作用,我想知道这是api中的错误还是我犯的错误。

/*
 * BlockEdit, a general purpose software to edit Minecraft
 * Copyright (c) 2015. Jeff Chen and others
 *
 * This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program. If not, see <http://www.gnu.org/licenses/>
 */

package org.blockedit.utils;

import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.io.File;
import java.io.PrintStream;
import java.util.Optional;

import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.control.TextArea;

public class MiniConsole {

    private TextArea area;
    private ConsoleOutputStream output;
    private PrintStream printStream;
    private ContextMenu rightClick;

    public void start() {
        this.area = new TextArea();
        this.area.setWrapText(true);
        this.area.setEditable(false);
        this.area.setPrefColumnCount(150);
        this.output = new ConsoleOutputStream(this.area);
        this.printStream = new PrintStream(this.output, true);
        System.setOut(this.printStream);
        System.setErr(this.printStream);

        this.rightClick = new ContextMenu();
        this.rightClick.setId("debugMenu");
        MenuItem copyItem = new MenuItem("Copy");
        copyItem.setOnAction(event -> {
            if (this.area.getSelectedText().isEmpty()) {
                event.consume();
            } else {
                StringSelection selection = new StringSelection(this.area.getSelectedText());
                Toolkit.getDefaultToolkit().getSystemClipboard().setContents(selection, selection);
            }
        });
        MenuItem selectAllItem = new MenuItem("Select All");
        selectAllItem.setOnAction(event -> {
            this.area.selectAll();
        });
        MenuItem deselectItem = new MenuItem("Deselect");
        deselectItem.setOnAction(event -> {
            this.area.deselect();
        });
        this.rightClick.getItems().addAll(copyItem, new SeparatorMenuItem(), selectAllItem, deselectItem);
        this.area.setContextMenu(this.rightClick);
        this.area.getStylesheets().add("https://fonts.googleapis.com/css?family=Open+Sans:400,700,800italic,800,700italic,600,600italic,300,300italic&subset=latin,greek-ext,greek,cyrillic,vietnamese,cyrillic-ext,latin-ext");
        this.area.getStylesheets().add("file:///" + new File("src/main/resources/miniconsole.css").getAbsolutePath().replace("\\", "/"));
    }

    public Optional<TextArea> getConsole() {
        if (this.area == null) {
            return Optional.empty();
        }
        return Optional.of(this.area);
    }

    public Optional<ConsoleOutputStream> getConsoleOutputStream() {
        if (this.output == null) {
            return Optional.empty();
        }
        return Optional.of(this.output);
    }

    public Optional<PrintStream> getPrintStream() {
        if (this.printStream == null) {
            return Optional.empty();
        }
        return Optional.of(this.printStream);
    }
}

/*
 * BlockEdit, a general purpose software to edit Minecraft
 * Copyright (c) 2015. Jeff Chen and others
 *
 * This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program. If not, see <http://www.gnu.org/licenses/>
 */

#debugMenu > .menu-item {
    -fx-font-family: "Open Sans", "Garmond", sans-serif;
}

#debugMenu > .menu-item:focused {
    -fx-font-weight: bold;
    -fx-background-color: #21ABCD;
    -fx-effect: innershadow( gaussian , rgba(0,0,0,0.2) , 10,0,0,0 );
}

【问题讨论】:

  • 这里只放理解您的问题所需的代码。

标签: java css user-interface javafx


【解决方案1】:

样式类context-menu 没有名为menu-item 的直系子级。见this link

您应该使用后代选择器。后代选择器由两个或多个用空格分隔的选择器组成。后代一词是指任何级别的孩子(直接或非直接)。

尝试使用以下样式:

#debugMenu .menu-item  {
    -fx-font-family: "Open Sans", "Garmond", sans-serif;
}

#debugMenu  .menu-item:focused   {
    -fx-font-weight: bold;
    -fx-background-color: red;
    -fx-effect: innershadow( gaussian , rgba(0,0,0,0.2) , 10,0,0,0 );
}

更新: 以下代码对我来说很好:

import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

/**
 *
 * @author 
 */
public class Main extends Application {

    private TextArea area;

    private ContextMenu rightClick;

    @Override
    public void start(Stage stage) {
        this.area = new TextArea();
        this.area.setWrapText(true);
        this.area.setEditable(false);
        this.area.setPrefColumnCount(150);
        this.rightClick = new ContextMenu();
        this.rightClick.setId("debugMenu");
        MenuItem copyItem = new MenuItem("Copy");
        copyItem.setOnAction(event -> {
            if (this.area.getSelectedText().isEmpty()) {
                event.consume();
            } else {
                StringSelection selection = new StringSelection(this.area.getSelectedText());
                Toolkit.getDefaultToolkit().getSystemClipboard().setContents(selection, selection);
            }
        });
        MenuItem selectAllItem = new MenuItem("Select All");
        selectAllItem.setOnAction(event -> {
            this.area.selectAll();
        });
        MenuItem deselectItem = new MenuItem("Deselect");
        deselectItem.setOnAction(event -> {
            this.area.deselect();
        });
        this.rightClick.getItems().addAll(copyItem, new SeparatorMenuItem(), selectAllItem, deselectItem);
        this.area.setContextMenu(this.rightClick);
        MenuBar menuBar = new MenuBar();
        Menu file = new Menu("File");
        Menu options = new Menu("Options");
        menuBar.getMenus().addAll(file, options);
        MenuItem newProject = new MenuItem("New");
        MenuItem save = new MenuItem("Save");
        MenuItem delete = new MenuItem("Delete");
        MenuItem quit = new MenuItem("Exit");
        MenuItem option1 = new MenuItem("Option1");
        BorderPane root = new BorderPane();
        file.getItems().addAll(newProject, save, delete, new SeparatorMenuItem(), quit);
        options.getItems().add(option1);
        root.setTop(menuBar);
        root.setCenter(area);
        Scene scene = new Scene(root, 400, 300);
        String contextMenustyle = getClass().getResource("menu-Style.css").toExternalForm();
        scene.getStylesheets().add(contextMenustyle);
        stage.setScene(scene);
        stage.show();

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);

    }

}

【讨论】:

  • 显然风格没有改变。
  • 别忘了这样做:scene.getStylesheets().add("packageName/fileName.css"); 或者您可以使用其他方式:String contextMenustyle = getClass().getResource("fileName.css").toExternalForm(); scene.getStylesheets().add(contextMenustyle);
  • Win.ubuntu 谢谢,但我一开始就这样做了。现在我开始认为这是一个 JavaFX 库错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-05
  • 2014-12-06
相关资源
最近更新 更多