[英]javafx automatic resizing and button padding
我正在嘗試使用Javafx制作屏幕鍵盤進行布局。 我正在使用Scene Builder制作FXML文件。
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.text.*?>
<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="186.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2">
<children>
<VBox layoutX="0.0" layoutY="0.0" prefHeight="186.0" prefWidth="600.0" rotate="0.0" spacing="2.0">
<children>
<HBox minHeight="33.0" prefHeight="33.0" prefWidth="600.0" spacing="2.0">
<children>
<Label maxWidth="-Infinity" prefHeight="33.0" prefWidth="35.0" text="Milk" textAlignment="CENTER" />
<Label maxWidth="-Infinity" prefHeight="33.0" text="Mister" />
<Label maxWidth="-Infinity" prefHeight="35.0" text="Minimum" />
</children>
<padding>
<Insets left="5.0" />
</padding>
</HBox>
<HBox prefHeight="29.0" prefWidth="600.0">
<children>
<Button minWidth="29.0" mnemonicParsing="false" prefHeight="27.0" prefWidth="29.0" text="Esc" textAlignment="CENTER" underline="false">
<font>
<Font size="9.0" />
</font>
</Button>
<Button mnemonicParsing="false" text="`" textAlignment="CENTER" />
<Button mnemonicParsing="false" text="1" />
<Button mnemonicParsing="false" text="2" />
<Button mnemonicParsing="false" text="3" />
<Button mnemonicParsing="false" text="4" />
<Button mnemonicParsing="false" text="5" />
<Button mnemonicParsing="false" text="6" />
<Button mnemonicParsing="false" text="7" />
<Button mnemonicParsing="false" text="9" />
<Button mnemonicParsing="false" text="0" />
<Button mnemonicParsing="false" text="-" />
<Button mnemonicParsing="false" text="=" />
<Button mnemonicParsing="false" text="Backspace" />
</children>
</HBox>
<HBox prefHeight="57.0" prefWidth="600.0">
<children>
<VBox minHeight="50.0" prefHeight="78.0" prefWidth="505.0" HBox.hgrow="ALWAYS">
<children>
<HBox prefHeight="29.0" prefWidth="-1.0">
<children>
<Button mnemonicParsing="false" prefHeight="27.0" text="Tab" />
<Button mnemonicParsing="false" text="q" />
<Button mnemonicParsing="false" text="w" />
<Button mnemonicParsing="false" text="e" />
<Button mnemonicParsing="false" text="r" />
<Button mnemonicParsing="false" text="t" />
<Button mnemonicParsing="false" text="y" />
<Button mnemonicParsing="false" text="u" />
<Button mnemonicParsing="false" text="i" />
<Button mnemonicParsing="false" text="o" />
<Button mnemonicParsing="false" text="p" />
<Button mnemonicParsing="false" text="[" />
<Button mnemonicParsing="false" text="]" />
</children>
</HBox>
<HBox minHeight="-1.0" prefHeight="28.0" prefWidth="-1.0">
<children>
<Button mnemonicParsing="false" prefWidth="71.0" text="Caps" />
<Button mnemonicParsing="false" text="a" />
<Button mnemonicParsing="false" text="s" />
<Button mnemonicParsing="false" text="d" />
<Button mnemonicParsing="false" text="f" />
<Button mnemonicParsing="false" text="g" />
<Button mnemonicParsing="false" text="h" />
<Button mnemonicParsing="false" text="j" />
<Button mnemonicParsing="false" text="k" />
<Button mnemonicParsing="false" text="l" />
<Button mnemonicParsing="false" text=";" />
<Button mnemonicParsing="false" text="'" />
<Button mnemonicParsing="false" text="\\" />
</children>
</HBox>
</children>
</VBox>
<Button mnemonicParsing="false" prefHeight="57.0" prefWidth="132.0" text="Enter" />
</children>
</HBox>
<HBox prefHeight="27.000099999997474" prefWidth="600.0">
<children>
<Button mnemonicParsing="false" prefWidth="85.0" text="Shift" />
<Button mnemonicParsing="false" text="z" />
<Button mnemonicParsing="false" text="x" />
<Button mnemonicParsing="false" text="c" />
<Button mnemonicParsing="false" text="v" />
<Button mnemonicParsing="false" text="b" />
<Button mnemonicParsing="false" text="n" />
<Button mnemonicParsing="false" text="m" />
<Button mnemonicParsing="false" text="," />
<Button mnemonicParsing="false" text="." />
<Button mnemonicParsing="false" text="/" />
<Button mnemonicParsing="false" text="^" />
<Button mnemonicParsing="false" prefWidth="56.0" text="Shift" />
<Button mnemonicParsing="false" text="Del" />
</children>
</HBox>
<HBox prefHeight="29.0" prefWidth="600.0" VBox.vgrow="ALWAYS">
<children>
<Button mnemonicParsing="false" text="Fn" />
<Button mnemonicParsing="false" text="Ctrl">
<HBox.margin>
<Insets />
</HBox.margin>
</Button>
<Button mnemonicParsing="false" text="Win" />
<Button mnemonicParsing="false" text="Alt" />
<Button mnemonicParsing="false" prefWidth="105.0" text="Space" HBox.hgrow="ALWAYS" />
<Button mnemonicParsing="false" text="Alt" />
<Button mnemonicParsing="false" prefWidth="-1.0" text="Ctrl" textAlignment="LEFT" />
<Button mnemonicParsing="false" text="<" />
<Button mnemonicParsing="false" text="v" />
<Button mnemonicParsing="false" text=">" />
<Button mnemonicParsing="false" text="Menu" />
</children>
</HBox>
</children>
</VBox>
</children>
</AnchorPane>
看起來像這樣:
但是當調整窗口大小時,內容不會。 我希望按鈕增大/減小尺寸,直到它適合窗口。 打開Hgrow和Vgrow不起作用。
我找不到如何在按鈕上設置填充。 我想在不丟失文本的情況下制作像Ctrl這樣的按鈕。
根據字體大小調整大小
對於您的特定情況,請嘗試調整用於虛擬鍵盤的父布局容器的字體大小( -fx-font-size
),而不是嘗試使用填充或其他布局約束來調整按鈕-fx-font-size
。 如果您將字體大小設置得更大,按鈕將自動更改其首選大小以匹配此較大的大小,並且所有文本將自動呈現並適合首選大小,並顯示在該字體大小的建議填充布局中(這可能是你想要的)。
按鈕大小調整規則
基本上,按鈕可調整大小的規則是:
button.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE)
刪除maxSize約束。 這是必需的,因為按鈕的默認約束是其最大大小是其首選大小,因此它不會自動增長以填充可用空間。 -fx-padding
或在代碼中使用button.setPadding(new Insets(...))
。 為了更好地理解JavaFX中的布局管理,我建議使用JavaFX 2.0演示文件查看過去的JavaOne 接口布局 。
可調整大小的按鈕網格示例
如果您希望繼續嘗試創建自己的實現,此示例顏色選擇器可能會有所幫助。 顏色選擇實現基於可調整大小的可調整按鈕網格,因此當您更改可用於顏色選擇器網格的區域時,網格和網格中的按鈕都會展開或收縮。 顏色選擇器的代碼不是基於FXML的,也不是直接實現你想要的鍵盤,但它確實展示了你在問題中詢問的按鈕的自動調整大小。
考慮使用JavaFX虛擬鍵盤
JavaFX已經內置了虛擬鍵盤 。 內置鍵盤沒有官方支持和記錄的公共API,並且不保證在Java版本之間維護。 但是,使用內置虛擬鍵盤可能仍然比嘗試創建自己的更好。 創建高質量的通用虛擬鍵盤是一項非常艱巨的任務(IMO)。
在Oracle JavaFX論壇上就此主題進行了一些討論。
JavaFX是開源的,因此即使您不直接使用內置虛擬鍵盤,也可以查看JavaFX源代碼 ,了解如何實現它。
虛擬鍵盤代碼示例
示例代碼演示在“桌面”環境中使用內置JavaFX虛擬鍵盤。
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class EmbeddedSample extends Application {
@Override public void start(Stage stage) {
stage.setScene(new Scene(new StackPane(new TextField("xyzzy")), 200, 100));
stage.getScene().setOnMouseClicked(e -> stage.hide());
stage.show();
}
public static void main(String[] args) { launch(args); }
}
像這樣運行示例代碼:
java -Dcom.sun.javafx.virtualKeyboard=javafx -Dcom.sun.javafx.touch=true EmbeddedSample
基於字體大小調整大小的虛擬鍵盤代碼示例
keyboard.css
.key {
-fx-base: antiquewhite;
}
.key-row {
-fx-spacing: 0.333333em;
}
.keyboard {
-fx-spacing: 0.333333em;
-fx-padding: 0.333333em;
-fx-font-family: monospace;
}
ResizableKeyboardSample.java
import javafx.application.Application;
import javafx.beans.property.*;
import javafx.geometry.*;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class ResizableKeyboardSample extends Application {
public static void main(String[] args) throws Exception {
launch(args);
}
String[] chars = {
"qwertyuiop",
"asdfghjkl",
"zxcvbnm"
};
public void start(final Stage stage) throws Exception {
Keyboard keyboard = new Keyboard();
VBox layout = new VBox(20);
layout.setPadding(new Insets(10));
layout.getChildren().setAll(
createControls(keyboard),
keyboard
);
Scene scene = new Scene(layout, 1000, 400);
scene.getStylesheets().add(
getClass().getResource(
"keyboard.css"
).toExternalForm()
);
stage.setScene(scene);
stage.show();
}
private Node createControls(Keyboard keyboard) {
Slider fontSize = new Slider(8, 40, Font.getDefault().getSize());
keyboard.fontSizeProperty().bind(fontSize.valueProperty());
fontSize.setShowTickLabels(true);
fontSize.setShowTickMarks(true);
fontSize.setMajorTickUnit(2);
fontSize.setMinorTickCount(0);
Label typedData = new Label();
keyboard.lastKeyTextProperty().addListener((observable, oldText, newText) ->
typedData.setText(typedData.getText() + newText)
);
VBox layout = new VBox(10);
layout.getChildren().setAll(
new Label("Keyboard Size"),
fontSize,
typedData
);
layout.setMinSize(VBox.USE_PREF_SIZE, VBox.USE_PREF_SIZE);
return layout;
}
class Keyboard extends VBox {
private DoubleProperty fontSize = new SimpleDoubleProperty(Font.getDefault().getSize());
public double getFontSize() {
return fontSize.get();
}
public DoubleProperty fontSizeProperty() {
return fontSize;
}
public void setFontSize(double fontSize) {
this.fontSize.set(fontSize);
}
private ReadOnlyStringWrapper lastKeyText = new ReadOnlyStringWrapper();
public String getLastKeyText() {
return lastKeyText.get();
}
public ReadOnlyStringProperty lastKeyTextProperty() {
return lastKeyText.getReadOnlyProperty();
}
public Keyboard() {
setAlignment(Pos.BOTTOM_CENTER);
setMinSize(VBox.USE_PREF_SIZE, VBox.USE_PREF_SIZE);
getStyleClass().add("keyboard");
onFontSizeChange(fontSize.getValue());
fontSize.addListener((observable, oldValue, newValue) ->
onFontSizeChange(newValue)
);
for (String row: chars) {
HBox keyRow = new HBox();
keyRow.getStyleClass().add("key-row");
keyRow.setAlignment(Pos.CENTER);
for (char c: row.toCharArray()) {
KeyButton key = new KeyButton(Character.toString(c));
keyRow.getChildren().add(key);
}
getChildren().add(keyRow);
}
}
private void onFontSizeChange(Number newValue) {
setStyle("-fx-font-size: " + newValue + "px;");
}
class KeyButton extends Button {
public KeyButton(String text) {
super(text);
getStyleClass().add("key");
setMinSize(Button.USE_PREF_SIZE, Button.USE_PREF_SIZE);
setMaxSize(Button.USE_PREF_SIZE, Button.USE_PREF_SIZE);
setOnAction(event -> lastKeyText.set(text));
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.