gpt4 book ai didi

javafx - 鼠标悬停时显示下拉菜单

转载 作者:行者123 更新时间:2023-12-04 16:26:01 26 4
gpt4 key购买 nike

我想创建这样的下拉菜单:

enter image description here

我希望当我将鼠标放在文本上时可以看到我可以用来选择一个值的组合框。当我移除鼠标时,我想看到简单的标签。我怎么能做到这一点?

最佳答案

悬停:

xyzzy

悬停时:

xyzzy hover

单击并选择:

foobar select

选择完成:

foobar

import javafx.application.Application;
import javafx.geometry.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;

public class Hoverboard extends Application {

public class TextChooser extends StackPane {
private Label label = new Label();
private ComboBox<String> combo = new ComboBox<>();

public TextChooser(String... options) {
StackPane.setAlignment(label, Pos.CENTER_LEFT);
StackPane.setAlignment(combo, Pos.CENTER_LEFT);

label.textProperty().bind(
combo.getSelectionModel().selectedItemProperty()
);
label.visibleProperty().bind(
combo.visibleProperty().not()
);
label.setPadding(new Insets(0, 0, 0, 9));

combo.getItems().setAll(options);
combo.getSelectionModel().select(0);
combo.setVisible(false);

label.setOnMouseEntered(event -> combo.setVisible(true));
combo.showingProperty().addListener(observable -> {
if (!combo.isShowing()) {
combo.setVisible(false);
}
});
combo.setOnMouseExited(event -> {
if (!combo.isShowing()) {
combo.setVisible(false);
}
});

getChildren().setAll(label, combo);
}
}

@Override
public void start(Stage stage) throws Exception {
TextChooser textChooser = new TextChooser(
"xyzzy", "frobozz", "foobar"
);

VBox layout = new VBox(textChooser);
layout.setPadding(new Insets(10));

stage.setScene(new Scene(layout));
stage.show();
}

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

}

关于javafx - 鼠标悬停时显示下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20412962/

26 4 0