大家好我是GUI全新的...到目前为止我的所有程序都以文本为基础。我刚刚开始使用JAVAFX。 我想重新创建一个电话键盘。它应该有4行,每行3个按钮,每个按钮应该有相关的数字及其字母。确实如此。但按钮的大小不一样。
我的意思是......因为我正在使用一个tilePane,按钮占用相同数量的像素(或者至少我猜它就是这样),但是按钮的实际可见尺寸是不同的,因为每个按钮只占用它的大小需要以显示其内容。我将按钮存储在一个数组中。有没有办法让它们与最大的一样大小相同?
public class PhoneKeyboard extends Application
{
Button buttons [];
@Override
public void start(Stage stage) throws Exception
{
// Create the set of necessary buttons.
buttons = new Button[12];
fillButtons(buttons);
//Create the grid for the buttons.
TilePane pane = new TilePane();
pane.setPadding(new Insets(10,10,10,10));
pane.setPrefColumns(3);
pane.setMaxWidth(Region.USE_PREF_SIZE);// Use the pref size as max size to make sure there will be the expected size
pane.setVgap(5.0);// to set ome spacing between each tile of the pane.
pane.setHgap(5.0);
//Put the buttons on the pane (layout);
pane.getChildren().addAll(buttons);
// JavaFX must have a Scene (window content) inside a Stage (window)
Scene scene = new Scene(new StackPane(pane), 300,400);// In order to use the pfer size of the pane, the tile Pane doesn"t have to be the root in the scene. Therefore we created a scene with a stack pane containing our tile pane as the root.
stage.setTitle("Keyboard");
stage.setScene(scene);
// Show the Stage (window)
stage.show();
}
Button具有最大宽度和最大高度,足以容纳Button的内容。大多数布局(包括TilePane)都不会使子节点大于其最大大小。
如果您希望每个按钮都能够变大,请在调用fillButtons
后设置其最大宽度和高度:
for (Button button : buttons) {
button.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
}