尽管我确实有一些小型编程背景,但是我对Stackoverflow和Java编程还是陌生的。

我正在使用JavaFX 8.0.0-b132和JavaSE 8构建一个小程序...

该程序具有一个GUI,并且此GUI具有ChoiceBox。布局的架构为:
AnchorPane-VBox-TitledPane-GridPane-HBox-ChoiceBox

我已经广泛使用CSS修改了ChoiceBox。

一切正常,除了一个问题:

当我启动程序并第一次打开ChoiceBox时(通过单击鼠标或使用show()),它将在其底部边缘显示它的ContextMenu。但是,当我关闭并再次打开它(使用鼠标或以编程方式)时,它将在ChoiceBox打开按钮(错误的y位置)顶部显示ContextMenu,以使当前所选单元格的底部与打开的底部重合-按钮(这意味着在我选择另一个单元格并再次打开它之后它将改变位置)。

如何防止这种情况发生,并使ContextMenu始终像一开始一样始终显示在底部?
这是带有受影响的控件的程序的GUI部分的代码片段:

编辑:问题已解决:显然,此行为是预期的ChoiceBox行为;我在JavaFX论坛上发帖说,最好让弹出窗口从一开始就表现得那样,即第一次打开弹出窗口时看起来不像是有问题的。

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.input.MouseEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.*;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.geometry.Insets;
import javafx.collections.*;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Side;

public class bugdemo extends Application {

    // Elements needed in both threads

    RadioButton radioBoxersOne = new RadioButton("One");
    RadioButton radioBoxersTwo = new RadioButton("Two");

    TextField warmupText = new TextField("skip warm up");
    TextField noCyclesText = new TextField("3");
    TextField cycleTimeText = new TextField("45");
    TextField restingTimeText = new TextField("30");

    String[] warmupTFtext = {"skip warm up", "\u00FCberspringen"};

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

    @Override
    public void start(Stage primaryStage) {

        // root container
        AnchorPane anchor = new AnchorPane();

        // Options TitledPane
        GridPane trainingOptions = new GridPane();
        ToggleGroup toggleGroup = new ToggleGroup();
        radioBoxersOne.setToggleGroup(toggleGroup);
        radioBoxersTwo.setToggleGroup(toggleGroup);
        radioBoxersOne.setSelected(true);
        HBox hbToggle = new HBox(15, radioBoxersOne, radioBoxersTwo);
        hbToggle.setPadding(new Insets(5,0,0,0));

        ObservableList<String> langList = FXCollections.observableArrayList("English", "Deutsch");
        ChoiceBox<String> langBox = new ChoiceBox<String>(langList);
        langBox.setValue("English");
        HBox hbLangBox = new HBox(0, langBox);
        hbLangBox.setPadding(new Insets(5,0,0,0));

        Label warmupOptionsLabel = new Label("Warm up time (min): ");
        Label noCyclesOptionsLabel = new Label("Number of cycles: ");
        Label cycleOptionsLabel = new Label("Cycle time (sec): ");
        Label restingOptionsLabel = new Label("Resting time (sec): ");
        Label noBoxersOptionsLabel = new Label("Number of boxers: ");
        Label langOptionsLabel = new Label("Language: ");

        trainingOptions.add(warmupOptionsLabel, 0, 0);
        trainingOptions.add(noCyclesOptionsLabel, 0, 1);
        trainingOptions.add(cycleOptionsLabel, 0, 2);
        trainingOptions.add(restingOptionsLabel, 0, 3);
        trainingOptions.add(noBoxersOptionsLabel, 0, 4);
        trainingOptions.add(langOptionsLabel, 0, 5);
        trainingOptions.add(warmupText, 1, 0);
        trainingOptions.add(noCyclesText, 1, 1);
        trainingOptions.add(cycleTimeText, 1, 2);
        trainingOptions.add(restingTimeText, 1, 3);
        trainingOptions.add(hbToggle, 1, 4);
        trainingOptions.add(hbLangBox, 1, 5);

        TitledPane TPane_in = new TitledPane("Options", trainingOptions);

        // Options TPane and Buttons together as UI (input)
        VBox UI = new VBox(10, /* hbButtons ,*/ TPane_in);
        anchor.getChildren().add(UI);
        AnchorPane.setLeftAnchor(UI, 25.0);
        AnchorPane.setTopAnchor(UI, 100.0);

        // EventHandlers for the radio-buttons
        radioBoxersOne.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent e) {
/*              if (!singleBoxer){
                    trainingProgress.getChildren().remove(boxerLabel);
                    trainingProgress.getChildren().remove(boxerText);
                }
                changeTextFlow();
                singleBoxer = true; */
            }
        });

        radioBoxersTwo.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent e) {
/*              singleBoxer = false;
                trainingProgress.add(boxerLabel, 0, 3);
                trainingProgress.add(boxerText, 1, 3);
                changeTextFlow(); */
            }
        });

        // "EventHandler" for the language ChoiceBox
        langBox.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
            public void changed(ObservableValue ov, Number value, Number new_value) {
/*              language = (int)new_value;
                changeLanguage(); */
            }
        });

        // initialize scene and show stage
        Scene scene = new Scene(anchor, 800, 650);
        primaryStage.setScene(scene);
        primaryStage.setResizable(false);       // resizeable on/off
        // import CSS file
        // scene.getStylesheets().add(Letsbox.class.getResource("Training.css").toExternalForm());
        primaryStage.show();
        langBox.show();
        langBox.hide();
        langBox.show();
    }
}


在此示例中,未使用CSS,但是问题仍然存在...

我试图用以下方法修复它:

/*      langBox.setOnMousePressed(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent e) {
                ContextMenu cm = langBox.getContextMenu();
                cm.show(cm.getOwnerNode(), Side.BOTTOM, 0.0, 0.0);
            }
        });  */


但它抛出NullPointerException ...

请告诉我如何解决这个问题:)

最佳答案

public class Main  extends Application{

    public static void main(String[] args) {
        launch(args);
      }
      @Override
      public void start(Stage stage) {
        Scene scene = new Scene(new Group(), 450, 250);
        Button notification = new Button();
        notification.setPrefSize(80, 60);
        notification.setAlignment(Pos.CENTER);
        MenuItem item1 = new MenuItem("About");
        item1.setOnAction(new EventHandler<ActionEvent>() {
          public void handle(ActionEvent e) {
            System.out.println("About");
          }
        });
        MenuItem item2 = new MenuItem("Preferences");
        item2.setOnAction(new EventHandler<ActionEvent>() {
          public void handle(ActionEvent e) {
            System.out.println("Preferences");
          }
        });

        MenuItem item3 = new MenuItem("About");
        item1.setOnAction(new EventHandler<ActionEvent>() {
          public void handle(ActionEvent e) {
            System.out.println("About");
          }
        });
        MenuItem item4 = new MenuItem("Preferences");
        item2.setOnAction(new EventHandler<ActionEvent>() {
          public void handle(ActionEvent e) {
            System.out.println("Preferences");
          }
        });
        MenuItem item5 = new MenuItem("About");
        item1.setOnAction(new EventHandler<ActionEvent>() {
          public void handle(ActionEvent e) {
            System.out.println("About");
          }
        });
        MenuItem item6 = new MenuItem("Preferences");
        item2.setOnAction(new EventHandler<ActionEvent>() {
          public void handle(ActionEvent e) {
            System.out.println("Preferences");
          }
        });

        MenuItem item7 = new MenuItem("About");
        item1.setOnAction(new EventHandler<ActionEvent>() {
          public void handle(ActionEvent e) {
            System.out.println("About");
          }
        });
        MenuItem item8 = new MenuItem("Preferences");
        item2.setOnAction(new EventHandler<ActionEvent>() {
          public void handle(ActionEvent e) {
            System.out.println("Preferences");
          }
        });
        final ContextMenu contextMenu = new ContextMenu(item1, item2,item3, item4,item5, item6,item7, item8);
        contextMenu.setMaxSize(50, 50);

        contextMenu.setOnShowing(new EventHandler<WindowEvent>() {
          public void handle(WindowEvent e) {
            System.out.println("showing");
          }
        });
        contextMenu.setOnShown(new EventHandler<WindowEvent>() {
          public void handle(WindowEvent e) {
            System.out.println("shown");
          }
        });

       // contextMenu.hide();


        notification.setContextMenu(contextMenu);
        GridPane grid = new GridPane();
        grid.setVgap(4);
        grid.setHgap(10);
        grid.setPadding(new Insets(5, 5, 5, 5));
        grid.add(new Label("To: "), 0, 0);
        grid.add(notification, 1, 0);

        Group root = (Group) scene.getRoot();
        root.getChildren().add(grid);
        stage.setScene(scene);
        stage.show();
        notification.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent me)->{
            if(me.getButton()==MouseButton.PRIMARY ){
                System.out.println("Mouse Left Pressed");
                System.out.println(notification.getScaleX());
                System.out.println(notification.getScaleY());
                System.out.println(me.getScreenX());
                System.out.println(me.getScreenY());
                contextMenu.show(notification,Side.TOP,0,0);
                //contextMenu.show(notification,me.getScreenX(),me.getScreenY());
            }else{
                contextMenu.hide();
            }
        });
      }

}

关于java - JavaFX ChoiceBox ContextMenu位置,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22834516/

10-11 14:16