当窗口调整大小时,如何自动调整BorderPane上的按钮大小? [英] How to automatically resize buttons on BorderPane as the window is resized?

查看:828
本文介绍了当窗口调整大小时,如何自动调整BorderPane上的按钮大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java的中间人,而且是JavaFX的新手。我正在开发一个在JavaFX 8中使用BorderPane的应用程序。我在BorderPane底部有两个按钮。

I am intermediate in Java and fairly new to JavaFX. I'm developing an application that makes use of BorderPane in JavaFX 8. I have two buttons at the bottom of the BorderPane.


  1. 我想要将按钮放置/对齐在BorderPane底部的中心,但我不知道执行该作业的功能。我正在尝试:




Bpane.setAlignment(bttmBttn,Pos.BOTTOM_CENTER)

Bpane.setAlignment(bttmBttn, Pos.BOTTOM_CENTER)

但不起作用。我希望他们始终是一个中心。它看起来像这样:

but didn't work. I want them an the center at all times. It looks like this :


  1. 我希望能够在窗口调整大小时自动调整按钮大小。截至目前,按钮在窗口扩展时保持相同的间隙,这使得它看起来像这样:

推荐答案

要对齐BorderPane底部中心的按钮,一种简单方便的方法是使用HBox作为两个按钮的父容器。

To align the buttons at the center of the bottom section of the BorderPane, an easy and convenient way to do it is to use a HBox as the parent containers of the two buttons.

HBox box = new HBox(10, button1, button2); // 10 is spacing
box.setAlignment(Pos.CENTER);
borderPane.setBottom(box);

由于您希望在展开屏幕时按钮展开,您可以为这些按钮制作HGROW按钮到 Priority.ALWAYS

Since, you want the buttons to expand when you expand the screen, you can make the HGROW for these buttons to Priority.ALWAYS.

HBox.setHgrow(button1, Priority.ALWAYS);
HBox.setHgrow(button2, Priority.ALWAYS);

您还必须删除 maxSize 来自按钮的约束通过调用:

You would also have to remove the maxSize constraint from the buttons by calling :

button1.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
button2.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);

这种方法存在一个小问题。按钮将捕获整个可用区域,我们不希望这样。摆脱它的一个简单方法是在HBox的开头和结尾添加两个固定长度的透明矩形。

There is one small issue with this approach. The buttons will capture the whole available area and we do not want that. An easy way to get rid of it is to add two fixed length, transparent rectangles at the beginning and end of the HBox.

MCVE

MCVE

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class Main extends Application {


    @Override
    public void start(Stage primaryStage) {

        Button button1 = new Button("Button 1");
        Button button2 = new Button("Button 2");

        button1.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
        button2.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);

        Rectangle rect1 = new Rectangle(60, 20);
        rect1.setFill(Color.TRANSPARENT);
        Rectangle rect2 = new Rectangle(60, 20);
        rect2.setFill(Color.TRANSPARENT);

        HBox box = new HBox(10, rect1, button1, button2, rect2);
        box.setAlignment(Pos.CENTER);
        HBox.setHgrow(button1, Priority.ALWAYS);
        HBox.setHgrow(button2, Priority.ALWAYS);

        BorderPane root = new BorderPane();
        root.setBottom(box);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Main Stage");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

这篇关于当窗口调整大小时,如何自动调整BorderPane上的按钮大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆