我正在使用包含字体选择器窗口的JavaFX制作记事本应用程序。字体选择器包括网格窗格布局中的文本字段,该布局用作所选字体选项的样本。
但是,如果选择的字体高于文本字段,则字段会拉伸以适合文本,从而扭曲窗口。
我希望示例文本字段保持相同的大小,即使包含的文本高于字段,如下面显示的字体选择器:
我尝试使用'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'来使文本字段按照我的意愿运行。
答案 0 :(得分:3)
这对我来说很好。
<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”
答案 1 :(得分:0)
所有TextField
的每一侧都有首选填充。
提示:要将TextField
更改为最小尺寸,请确保同时更改TextField
的填充以更改为所需大小。
在JavaFx中:
TextField textDisplay = new TextField();
textDisplay.setPrefSize(100,5);
textDisplay.setPadding(new Insets(1,1,1,1));
如果要基于textField中的fontSize设置textField的大小,只需将其保留为空“ DONT USE” .setPrefSize(double,double);
即可设置textField的静态大小,但可以使用以下方法设置文本字段的最大值css / javafx
textDisplay.setMaxWidth(double);
textDisplay.setMaxHeiht(double);
或
textDisplay.setSize(double,double);
但是,如果您使用CSS更容易设计GUI,就不要忘记设置节点的ID。
textDisplay.setStyle("-fx-pref-width: 100"
+"\n-fx-pref-height: 100"
+"-fx-padding: 5 5 5 5;");
或
textDisplay.setId("idTextDisplay");
#idTextDisplay{
-fx-pref-width: 100;
-fx-pref-height: 100;
-fx-padding: 5 5 5 5;
}
答案 2 :(得分:-1)
在你的情况下是:
TextField sample = new TextField("AaBbYyZz");
sample.setEditable(false);
sample.getStyleClass().add("sample");
sample.setAlignment(Pos.CENTER);
sample.setStyle(getCSS());
sample.setPrefSize(200, 60);
sample.prefHeight(Region.USE_COMPUTED_SIZE);
sample.prefwidth(Region.USE_COMPUTED_SIZE);