gpt4 book ai didi

java - 如何为 JavaFX 文本字段设置固定高度而不考虑其内容?

转载 作者:行者123 更新时间:2023-11-30 07:49:58 29 4
gpt4 key购买 nike

我正在使用 JavaFX 制作一个记事本应用程序,其中包括一个字体选择器窗口。字体选择器在网格 Pane 布局中包含一个文本字段,用作所选字体选项外观的示例。

字体选择器:
font selector

但是,如果所选字体比文本字段高,则该字段会拉伸(stretch)以适合文本,从而扭曲窗口。

扭曲的字体选择器:
distorted font selector

我希望示例文本字段保持相同大小,即使包含的文本比字段高,如下所示的字体选择器:
font selector

我尝试过使用“setPrefSize”和“setMaxSize”文本字段方法强制设置最大高度。

public static final String display(String savedFamily, String savedStyle, String savedSize, String stylesheet) {

fontFamily = savedFamily;
fontSize = savedSize.substring(0, savedSize.length() - 2);
fontWeight = getChosenWeight(savedStyle);
fontStyle = getChosenStyle(savedStyle);

// Add stage
Stage window = new Stage();
window.initModality(Modality.APPLICATION_MODAL);

TextField sample = new TextField("AaBbYyZz");
sample.setEditable(false);
sample.getStyleClass().add("sample");
sample.setAlignment(Pos.CENTER);
sample.setStyle(getCSS());
sample.setPrefSize(200, 60);
sample.setMaxHeight(60);

// Add list view title labels
Label fontLabel = new Label("Font:");
Label fontStyleLabel = new Label("Font Style:");
Label fontSizeLabel = new Label("Font Size: ");

// Add font list view
ListView<String> fontView = new ListView<>();
String fonts[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
for (int i = 0; i < fonts.length; i++) {
fontView.getItems().add(fonts[i]);
}
fontView.getSelectionModel().select(fontFamily);
fontView.setMaxSize(200, 150);
fontView.getSelectionModel().selectedItemProperty().addListener((v) -> {
fontFamily = fontView.getSelectionModel().getSelectedItem();
sample.setStyle(getCSS());
});

// Add font style list view
ListView<String> fontStyleView = new ListView<>();
fontStyleView.getItems().addAll("Regular", "Italic", "Bold", "Bold Italic");
fontStyleView.getSelectionModel().select(savedStyle);
fontStyleView.setMaxSize(80, 150);
fontStyleView.getSelectionModel().selectedItemProperty().addListener((v) -> {
fontStyle = getChosenStyle(fontStyleView.getSelectionModel().getSelectedItem());
fontWeight = getChosenWeight(fontStyleView.getSelectionModel().getSelectedItem());
sample.setStyle(getCSS());
});

// Add font size list view
ListView<String> fontSizeView = new ListView<>();
fontSizeView.getItems().addAll("6", "7", "8", "9", "10", "11", "12", "14", "16", "18", "20", "22", "24", "26", "28");
fontSizeView.getSelectionModel().select(fontSize);
fontSizeView.setMaxSize(80, 150);
fontSizeView.getSelectionModel().selectedItemProperty().addListener((v) -> {
fontSize = fontSizeView.getSelectionModel().getSelectedItem();
sample.setStyle(getCSS());
});

// Add OK button
Button okButton = new Button("OK");
okButton.setOnAction(e -> {
fontCssString = getCSS();
window.close();
});

// Add cancel button
Button cancelButton = new Button("Cancel");
cancelButton.setOnAction(e -> {
window.close();
});

// Add and configure grid pane
GridPane layout = new GridPane();
layout.setPadding(new Insets(15, 15, 15, 15));
layout.setVgap(20);
layout.setHgap(20);

// Set grid constraints for font list view and title
GridPane.setConstraints(fontLabel, 0, 0);
GridPane.setConstraints(fontView, 0, 1);

// Set grid constraints for font style list view and title
GridPane.setConstraints(fontStyleLabel, 1, 0);
GridPane.setConstraints(fontStyleView, 1, 1);
GridPane.setValignment(fontStyleView, VPos.TOP);

// Set grid constraints for font size list view and title
GridPane.setConstraints(fontSizeLabel, 2, 0);
GridPane.setConstraints(fontSizeView, 2, 1);

// Set grid constraints for sample title
GridPane.setConstraints(sample, 0, 2);
GridPane.setHalignment(sample, HPos.CENTER);

// set grid constraints and alignments for buttons
GridPane.setConstraints(okButton, 1, 2);
GridPane.setHalignment(okButton, HPos.RIGHT);
GridPane.setConstraints(cancelButton, 2, 2);

// Add items to grid pane layout
layout.getChildren().addAll(fontLabel, fontView, fontStyleLabel, fontStyleView, fontSizeLabel, fontSizeView,
sample, okButton, cancelButton);

// configure the scene and stage
Scene scene = new Scene(layout, 450, 300);
scene.getStylesheets().add(FontWindow.class.getResource(stylesheet).toExternalForm());
window.setScene(scene);
window.setTitle("Font Options");
window.showAndWait();

return fontCssString;
}

public static final String getChosenStyle(String s) {
String result = "normal";
if (s.contains("Italic")) {
result = "italic";
}
return result;
}

public static final String getChosenWeight(String s) {
String result = "normal";
if (s.contains("Bold")) {
result = "bold";
}
return result;
}

public static String getCSS() {
return "-fx-font-family: " + fontFamily + "; -fx-font-weight: " + fontWeight + "; -fx-font-style: " + fontStyle
+ "; -fx-font-size: " + Float.valueOf(fontSize) / 9.7 + "em;";
}
}

编辑:这已经解决了!通过设置最大和最小尺寸以及将首选高度和宽度设置为“region.USE_COMPUTED_SIZE”,我能够让文本字段按我想要的方式运行。

最佳答案

这对我来说很不错。

<TextField layoutX="151.0" layoutY="53.0" maxHeight="50.0" maxWidth="255.0" minHeight="50.0" minWidth="255.0" text="AaZz">

...和 ​​Pref Height/Pref Width 是“USE_COMPUTED_SIZE”

关于java - 如何为 JavaFX 文本字段设置固定高度而不考虑其内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48082124/

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