【问题标题】:Buttons of the same size in tilepane javafxtilepane javafx中相同大小的按钮
【发布时间】:2019-03-01 23:19:53
【问题描述】:

大家好,我对 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();
}

【问题讨论】:

  • 如果您提供的代码包含fillButtons 方法,我们将更容易重现您的问题。

标签: java button javafx size


【解决方案1】:

Button 的最大宽度和最大高度刚好足以容纳 Button 的内容。大多数布局(包括 TilePane)不会使子节点大于其最大大小。

如果您希望每个按钮都能够变大,请在调用fillButtons 后设置其最大宽度和高度:

for (Button button : buttons) {
    button.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多