注意:JavaFX新手
部分文档
Buttton.setPadding
所以每个人都知道填充是什么,但here是Android澄清的一个例子。
问题:
设置(和更改)javafx button上的填充会增加按钮的大小,但不会增加"空间"在它自己和它(邻近)邻居之间。
更多信息:
我想在退出和重置按钮之间留一个空格。
假设:
Button btnExit = new Button("Exit");
填充通常只需设置:
btnExit.setPadding(new Insets(10)) //10 px "buffer" around button
或
btnExit.setPadding(new Insets(0, 10, 0, 0)) //10 px "buffer" on right only
我这样做是全面的填充,但不是创建这个缓冲区,而是放大按钮大小。
无填充(btnExit.setPadding(new Insets(0));
)
PADDING_LARGE=10
使用填充(btnReset.setPadding(new Insets(PADDING_LARGE));
)
代码:
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.Border;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.scene.control.Button;
public class SOS extends Application{
private int GRID_SIZE = 5;
private int PADDING_MEDIUM = 5;
private int PADDING_LARGE = 10;
private int PADDING_XLARGE = 20;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
//create display boxes
VBox vbRoot = new VBox(),
vbGrid = new VBox(),
vbFooter = new VBox(),
vbPlayers = new VBox(),
vbGridInfo = new VBox(),
vbHeader = new VBox();
HBox hbOptions = new HBox(),
hbDone = new HBox(),
hbGameDetails = new HBox(),
hbGameTitle = new HBox();
//=====================HEADER=================================
Label title = new Label("SOS Game");
title.setFont(new Font(16));
hbGameTitle.getChildren().add(title);
hbGameTitle.setAlignment(Pos.CENTER);
Label player1 = new Label("Player 1 = ");
Label player2 = new Label("Player 2 = ");
vbPlayers.getChildren().addAll(player1,player2);
vbPlayers.setAlignment(Pos.CENTER);
Label lblGridPosOpen = new Label("Places Open = ");
vbGridInfo.getChildren().add(lblGridPosOpen);
vbGridInfo.setAlignment(Pos.CENTER);
//add player and grid info boxes to game details box
hbGameDetails.getChildren().addAll(vbGridInfo, vbPlayers);
//add game title and game info boxes to header
vbHeader.getChildren().addAll(hbGameTitle, hbGameDetails);
//=====================GRID====================================
GridPane grid = getGrid(GRID_SIZE);
//add grid object to grid pane
vbGrid.getChildren().addAll(grid);
vbGrid.setPadding(new Insets(PADDING_XLARGE));
vbGrid.setAlignment(Pos.CENTER);
//======================FOOTER================================
//set DONE button with padding and callback
Button btnDone = new Button("DONE");
btnDone.setMinWidth(130);
btnDone.setMinHeight(50);
btnDone.setOnAction(event -> checkSOS());
btnDone.setPadding(new Insets(PADDING_MEDIUM));
//add button done to box
hbDone.getChildren().add(btnDone);
hbDone.setPadding(new Insets(PADDING_MEDIUM));
hbDone.setAlignment(Pos.CENTER);
//set Exit and Reset buttons with padding and callbacks
Button btnExit = new Button("Exit");
Button btnReset = new Button("Reset");
btnExit.setMinWidth(60);
btnExit.setMinHeight(25);
btnExit.setPadding(new Insets(PADDING_LARGE));
btnExit.setOnAction(event -> System.exit(0));
btnReset.setMinWidth(60);
btnReset.setMinHeight(25);
btnReset.setPadding(new Insets(PADDING_LARGE));
btnReset.setOnAction(event -> reset());
//add exit and reset buttons to options box
hbOptions.getChildren().addAll(btnExit, btnReset);
hbOptions.setPadding(new Insets(PADDING_MEDIUM));
hbOptions.setAlignment(Pos.CENTER);
//add done and options boxes to footer
vbFooter.getChildren().addAll(hbDone, hbOptions);
//=====================COMBINE ALL======================
//combine all sections with padding
vbRoot.getChildren().addAll(vbHeader, vbGrid, vbFooter);
hbOptions.setPadding(new Insets(PADDING_MEDIUM));
//=====================SET STAGE===========================
primaryStage.setTitle("SOS by :)");
primaryStage.setScene(new Scene(vbRoot));
primaryStage.setResizable(false);
primaryStage.show();
}
/**
* Returns a new grid with buttons
* @param size Size of grid
* @return new grid
*/
private GridPane getGrid(int size) {
GridPane grid = new GridPane();
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
grid.add(getSOSButton(), i, j);
}
}
return grid;
}
/**
* Returns a button with callback to change text
* @return
*/
private Button getSOSButton(){
//create button
Button b = new Button();
b.setMinHeight(50);
b.setMaxHeight(50);
b.setMinWidth(50);
b.setMaxWidth(50);
//set callback
b.setOnAction(event -> b.setText((b.getText().equals("O") || b.getText().isEmpty()) ? "S" : "O"));
return b;
}
private void reset(){
}
private void checkSOS(){
}
}
答案 0 :(得分:0)
JavaFX中的填充应该增加按钮大小,但它不会影响按钮外的区域。填充是指按钮内部的区域,但在文本周围,因此,文本的“填充”。如果要在按钮外部填充,则必须在构造函数中更改包含节点(在本例中为hbOptions),如下所示:
HBox hbOptions = new HBox(numberValueInPointsByWhichYouWantToSeparateYourButtons);
或致电
hbOptions.setSpacing(numberValueInPointsByWhichYouWantToSeparateYourButtons);
此setSpacing(双倍间距)方法指的是HBox的子节点的间距,在本例中是两个按钮。
(此答案的帮助来自Adding space between buttons in VBox和the HBox documentation)。