gpt4 book ai didi

JavaFX ChoiceBox 上下文菜单位置

转载 作者:行者123 更新时间:2023-11-28 15:29:29 25 4
gpt4 key购买 nike

我是 stackoverflow 和 Java 编程的新手,尽管我确实有一些小的编程背景。

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

该程序有一个 GUI,这个 GUI 有一个选择框。布局的架构是:AnchorPane - VBox - TitledPane - GridPane - HBox - ChoiceBox

我已经使用 CSS 大量修改了 ChoiceBox。

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

当我启动程序并第一次打开 ChoiceBox(通过鼠标单击或使用 show())时,它会在底边显示它的上下文菜单;但是,当我再次关闭并打开它(使用鼠标或以编程方式)时,它将在 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();
}
});
}

}

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com